| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.compiler; |
|---|
| 19 | |
|---|
| 20 | import java.lang.StringBuffer; |
|---|
| 21 | import java.math.BigInteger; |
|---|
| 22 | import java.util.ArrayList; |
|---|
| 23 | import java.util.Collections; |
|---|
| 24 | import java.util.HashMap; |
|---|
| 25 | import java.util.List; |
|---|
| 26 | import java.util.Map; |
|---|
| 27 | |
|---|
| 28 | import com.sun.fortress.compiler.environments.TopLevelEnvGen; |
|---|
| 29 | import com.sun.fortress.compiler.index.Function; |
|---|
| 30 | import com.sun.fortress.compiler.phases.OverloadSet; |
|---|
| 31 | import com.sun.fortress.exceptions.CompilerError; |
|---|
| 32 | import com.sun.fortress.nodes.ASTNode; |
|---|
| 33 | import com.sun.fortress.nodes.APIName; |
|---|
| 34 | import com.sun.fortress.nodes.AnyType; |
|---|
| 35 | import com.sun.fortress.nodes.ArrowType; |
|---|
| 36 | import com.sun.fortress.nodes.BaseType; |
|---|
| 37 | import com.sun.fortress.nodes.BottomType; |
|---|
| 38 | import com.sun.fortress.nodes.FnDecl; |
|---|
| 39 | import com.sun.fortress.nodes.FnHeader; |
|---|
| 40 | import com.sun.fortress.nodes.Id; |
|---|
| 41 | import com.sun.fortress.nodes.IdOrOp; |
|---|
| 42 | import com.sun.fortress.nodes.IdOrOpOrAnonymousName; |
|---|
| 43 | import com.sun.fortress.nodes.NamedType; |
|---|
| 44 | import com.sun.fortress.nodes.Param; |
|---|
| 45 | import com.sun.fortress.nodes.TraitType; |
|---|
| 46 | import com.sun.fortress.nodes.TraitTypeWhere; |
|---|
| 47 | import com.sun.fortress.nodes.TupleType; |
|---|
| 48 | import com.sun.fortress.nodes.VarType; |
|---|
| 49 | import com.sun.fortress.nodes.NodeAbstractVisitor; |
|---|
| 50 | import com.sun.fortress.nodes_util.NodeFactory; |
|---|
| 51 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 52 | import com.sun.fortress.nodes_util.Span; |
|---|
| 53 | import com.sun.fortress.repository.ForeignJava; |
|---|
| 54 | import com.sun.fortress.repository.GraphRepository; |
|---|
| 55 | import com.sun.fortress.repository.ProjectProperties; |
|---|
| 56 | import com.sun.fortress.useful.BATree; |
|---|
| 57 | import com.sun.fortress.useful.Debug; |
|---|
| 58 | import com.sun.fortress.useful.Useful; |
|---|
| 59 | |
|---|
| 60 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 61 | |
|---|
| 62 | import org.objectweb.asm.Type; |
|---|
| 63 | |
|---|
| 64 | import static com.sun.fortress.exceptions.InterpreterBug.bug; |
|---|
| 65 | import static com.sun.fortress.exceptions.ProgramError.error; |
|---|
| 66 | import static com.sun.fortress.exceptions.ProgramError.errorMsg; |
|---|
| 67 | |
|---|
| 68 | public class NamingCzar { |
|---|
| 69 | public static final NamingCzar only = new NamingCzar(ForeignJava.only); |
|---|
| 70 | |
|---|
| 71 | private final ForeignJava fj; |
|---|
| 72 | |
|---|
| 73 | private NamingCzar(ForeignJava fj) { |
|---|
| 74 | this.fj = fj; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public static final String springBoard = "$SpringBoard"; |
|---|
| 78 | public static final String make = "make"; |
|---|
| 79 | |
|---|
| 80 | public static final String cache = ProjectProperties.BYTECODE_CACHE_DIR + "/"; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | public static final int ignore = 1; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | public static final String fortressPackage = "fortress"; |
|---|
| 88 | public static final String fortressAny = fortressPackage + "/" + |
|---|
| 89 | WellKnownNames.anyTypeLibrary() + |
|---|
| 90 | "$" + WellKnownNames.anyTypeName; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | public static final String javaObject = "java/lang/Object"; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public static final String fortressExecutable = "com/sun/fortress/runtimeSystem/FortressExecutable"; |
|---|
| 97 | public static final String fortressExecutableRun = "runExecutable"; |
|---|
| 98 | public static final String fortressExecutableRunType = "([Ljava/lang/String;)V"; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | public static final String fortressBaseTask = "com/sun/fortress/runtimeSystem/BaseTask"; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | public static final String fortressComponent = javaObject; |
|---|
| 105 | |
|---|
| 106 | public static final String primordialTask = "com/sun/fortress/runtimeSystem/PrimordialTask"; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | public static final String internalFloat = org.objectweb.asm.Type.getInternalName(float.class); |
|---|
| 111 | public static final String internalInt = org.objectweb.asm.Type.getInternalName(int.class); |
|---|
| 112 | public static final String internalDouble = org.objectweb.asm.Type.getInternalName(double.class); |
|---|
| 113 | public static final String internalLong = org.objectweb.asm.Type.getInternalName(long.class); |
|---|
| 114 | public static final String internalBoolean = org.objectweb.asm.Type.getInternalName(boolean.class); |
|---|
| 115 | public static final String internalChar = org.objectweb.asm.Type.getInternalName(char.class); |
|---|
| 116 | public static final String internalObject = org.objectweb.asm.Type.getInternalName(Object.class); |
|---|
| 117 | public static final String internalString = org.objectweb.asm.Type.getInternalName(String.class); |
|---|
| 118 | |
|---|
| 119 | public static final String descFloat = org.objectweb.asm.Type.getDescriptor(float.class); |
|---|
| 120 | public static final String descInt = org.objectweb.asm.Type.getDescriptor(int.class); |
|---|
| 121 | public static final String descDouble = org.objectweb.asm.Type.getDescriptor(double.class); |
|---|
| 122 | public static final String descLong = org.objectweb.asm.Type.getDescriptor(long.class); |
|---|
| 123 | public static final String descBoolean = org.objectweb.asm.Type.getDescriptor(boolean.class); |
|---|
| 124 | public static final String descChar = org.objectweb.asm.Type.getDescriptor(char.class); |
|---|
| 125 | public static final String descString = internalToDesc(internalString); |
|---|
| 126 | public static final String descVoid = org.objectweb.asm.Type.getDescriptor(void.class); |
|---|
| 127 | public static final String stringArrayToVoid = makeMethodDesc(makeArrayDesc(descString), descVoid); |
|---|
| 128 | public static final String voidToVoid = makeMethodDesc("", descVoid); |
|---|
| 129 | |
|---|
| 130 | public static final String internalFortressZZ32 = makeFortressInternal("ZZ32"); |
|---|
| 131 | public static final String internalFortressZZ64 = makeFortressInternal("ZZ64"); |
|---|
| 132 | public static final String internalFortressRR32 = makeFortressInternal("RR32"); |
|---|
| 133 | public static final String internalFortressRR64 = makeFortressInternal("RR64"); |
|---|
| 134 | public static final String internalFortressBoolean = makeFortressInternal("Boolean"); |
|---|
| 135 | public static final String internalFortressChar = makeFortressInternal("Char"); |
|---|
| 136 | public static final String internalFortressString = makeFortressInternal("String"); |
|---|
| 137 | public static final String internalFortressVoid = makeFortressInternal("Void"); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | public static final String descFortressZZ32 = internalToDesc(internalFortressZZ32); |
|---|
| 141 | public static final String descFortressZZ64 = internalToDesc(internalFortressZZ64); |
|---|
| 142 | public static final String descFortressRR32 = internalToDesc(internalFortressRR32); |
|---|
| 143 | public static final String descFortressRR64 = internalToDesc(internalFortressRR64); |
|---|
| 144 | public static final String descFortressBoolean = internalToDesc(internalFortressBoolean); |
|---|
| 145 | public static final String descFortressChar = internalToDesc(internalFortressChar); |
|---|
| 146 | public static final String descFortressString = internalToDesc(internalFortressString); |
|---|
| 147 | public static final String descFortressVoid = internalToDesc(internalFortressVoid); |
|---|
| 148 | public static final String descFortressAny = internalToDesc(fortressAny); |
|---|
| 149 | |
|---|
| 150 | public static final String voidToFortressVoid = makeMethodDesc("", descFortressVoid); |
|---|
| 151 | |
|---|
| 152 | private static final List<String> extendsObject = |
|---|
| 153 | Collections.singletonList(internalObject); |
|---|
| 154 | |
|---|
| 155 | public static String deCase(String s) { |
|---|
| 156 | return "_" + Integer.toString(s.hashCode()&0x7fffffff,16); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public static String deCaseName(PathTaggedApiName ptan) { |
|---|
| 160 | return deCaseName(ptan.name, ptan.source_path); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public static String deCaseName(APIName s, String sourcePath) { |
|---|
| 164 | return s + "-" + Integer.toString(sourcePath.hashCode()&0x7fffffff,16); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public static String cachedPathNameForApiAst(String passedPwd, String sourcePath, APIName name) { |
|---|
| 168 | return ProjectProperties.apiFileName(passedPwd, deCaseName(name, sourcePath)); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public static String cachedPathNameForCompAst(String passedPwd, String sourcePath, APIName name) { |
|---|
| 172 | return ProjectProperties.compFileName(passedPwd, deCaseName(name, sourcePath)); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | public static String dependenceFileNameForCompAst(APIName name, String sourcePath) { |
|---|
| 176 | return ProjectProperties.compFileName(ProjectProperties.ANALYZED_CACHE_DEPENDS_DIR, deCaseName(name, sourcePath)); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public static String dependenceFileNameForApiAst(APIName name, String sourcePath) { |
|---|
| 180 | return ProjectProperties.apiFileName(ProjectProperties.ANALYZED_CACHE_DEPENDS_DIR, deCaseName(name, sourcePath)); |
|---|
| 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 | static Span span = NodeFactory.internalSpan; |
|---|
| 212 | |
|---|
| 213 | static APIName fortLib = |
|---|
| 214 | NodeFactory.makeAPIName(span, WellKnownNames.fortressBuiltin()); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | public static com.sun.fortress.nodes.Type fortressTypeForForeignJavaType(Type t) { |
|---|
| 224 | return fortressTypeForForeignJavaType(t.getDescriptor()); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | public static com.sun.fortress.nodes.Type fortressTypeForForeignJavaType(String s) { |
|---|
| 236 | return specialForeignJavaTranslations.get(s); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | public static String internalToDesc(String type) { |
|---|
| 243 | return "L" + type + ";"; |
|---|
| 244 | } |
|---|
| 245 | public static String makeMethodDesc(String param, String result) { |
|---|
| 246 | return "(" + param + ")" + result; |
|---|
| 247 | } |
|---|
| 248 | public static String makeMethodDesc(List<String> params, String result) { |
|---|
| 249 | String desc ="("; |
|---|
| 250 | for (String param : params) { |
|---|
| 251 | desc += param; |
|---|
| 252 | } |
|---|
| 253 | desc += ")" + result; |
|---|
| 254 | return desc; |
|---|
| 255 | } |
|---|
| 256 | public static String makeArrayDesc(String element) { |
|---|
| 257 | return "[" + element; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | public static String makeFortressInternal(String type) { |
|---|
| 262 | return "com/sun/fortress/compiler/runtimeValues/F" + type; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | static Map<String, com.sun.fortress.nodes.Type> specialForeignJavaTranslations = new HashMap<String, com.sun.fortress.nodes.Type>(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | static void s(Class cl, APIName api, String str) { |
|---|
| 273 | s(Type.getType(cl), api, str); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | static void s(Type cl, APIName api, String str) { |
|---|
| 277 | s(cl.getDescriptor(), api, str); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | static void s(String cl, APIName api, String str) { |
|---|
| 281 | specialForeignJavaTranslations.put(cl, |
|---|
| 282 | NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, api, str))); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | static { |
|---|
| 286 | s(Boolean.class, fortLib, "Boolean"); |
|---|
| 287 | s(Type.BOOLEAN_TYPE, fortLib, "Boolean"); |
|---|
| 288 | s(Integer.class, fortLib, "ZZ32"); |
|---|
| 289 | s(Type.INT_TYPE, fortLib, "ZZ32"); |
|---|
| 290 | s(Long.class, fortLib, "ZZ64"); |
|---|
| 291 | s(Type.LONG_TYPE, fortLib, "ZZ64"); |
|---|
| 292 | s(Float.class, fortLib, "RR32"); |
|---|
| 293 | s(Type.FLOAT_TYPE, fortLib, "RR32"); |
|---|
| 294 | s(Double.class, fortLib, "RR64"); |
|---|
| 295 | s(Type.DOUBLE_TYPE, fortLib, "RR64"); |
|---|
| 296 | s(Object.class, fortLib, "Any"); |
|---|
| 297 | s(String.class, fortLib, "String"); |
|---|
| 298 | s(BigInteger.class, fortLib, "ZZ"); |
|---|
| 299 | specialForeignJavaTranslations.put("V", NodeFactory.makeVoidType(span)); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | static final String runtimeValues = "com/sun/fortress/compiler/runtimeValues/"; |
|---|
| 303 | |
|---|
| 304 | static final String FValueType = runtimeValues + "FValue"; |
|---|
| 305 | static final String FValueDesc = "L" + FValueType + ";"; |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | static Map<com.sun.fortress.nodes.Type, String> specialFortressDescriptors = new HashMap<com.sun.fortress.nodes.Type, String>(); |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | static Map<com.sun.fortress.nodes.Type, String> specialFortressTypes = new HashMap<com.sun.fortress.nodes.Type, String>(); |
|---|
| 315 | |
|---|
| 316 | static void bl(APIName api, String str, String cl) { |
|---|
| 317 | b(api,str, runtimeValues+cl); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | static void bl(com.sun.fortress.nodes.Type t, String cl) { |
|---|
| 321 | b(t, runtimeValues+cl); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | static void b(APIName api, String str, String cl) { |
|---|
| 325 | b(NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, api, str)), cl); |
|---|
| 326 | b(NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, str)), cl); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | static void b(com.sun.fortress.nodes.Type t, String cl) { |
|---|
| 330 | specialFortressDescriptors.put(t, "L" + cl + ";"); |
|---|
| 331 | specialFortressTypes.put(t, cl ); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | static { |
|---|
| 335 | bl(fortLib, "Boolean", "FBoolean"); |
|---|
| 336 | bl(fortLib, "Char", "FChar"); |
|---|
| 337 | bl(fortLib, "RR32", "FRR32"); |
|---|
| 338 | bl(fortLib, "RR64", "FRR64"); |
|---|
| 339 | bl(fortLib, "ZZ32", "FZZ32"); |
|---|
| 340 | bl(fortLib, "ZZ64", "FZZ64"); |
|---|
| 341 | bl(fortLib, "String", "FString"); |
|---|
| 342 | bl(NodeFactory.makeVoidType(span), "FVoid"); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | public static String boxedImplDesc(com.sun.fortress.nodes.Type t, APIName ifNone) { |
|---|
| 358 | return jvmTypeDesc(t, ifNone); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | public static String boxedImplType( com.sun.fortress.nodes.Type t ) { |
|---|
| 362 | |
|---|
| 363 | String desc = specialFortressTypes.get(t); |
|---|
| 364 | |
|---|
| 365 | if (desc != null) |
|---|
| 366 | return desc; |
|---|
| 367 | |
|---|
| 368 | if (t instanceof ArrowType) { |
|---|
| 369 | |
|---|
| 370 | } else if (t instanceof BaseType) { |
|---|
| 371 | if (t instanceof AnyType) { |
|---|
| 372 | return FValueType; |
|---|
| 373 | } else if (t instanceof BottomType) { |
|---|
| 374 | return bug("Not sure how bottom type translates into Java"); |
|---|
| 375 | } else if (t instanceof NamedType) { |
|---|
| 376 | if (t instanceof TraitType) { |
|---|
| 377 | return FValueType; |
|---|
| 378 | } else if (t instanceof VarType) { |
|---|
| 379 | return bug("Need a binding to translate a VarType into Java"); |
|---|
| 380 | } |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | return bug ("unhandled type translation, Fortress type " + t); |
|---|
| 384 | |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | public String apiNameToPackageName(APIName name) { |
|---|
| 388 | if (fj.definesApi(name)) { |
|---|
| 389 | return name.getText(); |
|---|
| 390 | } else { |
|---|
| 391 | return javaPackageClassForApi(name.getText(), ".").toString(); |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | public String apiAndMethodToMethodOwner(APIName name, Function method) { |
|---|
| 396 | String p; |
|---|
| 397 | String m = method.toUndecoratedName().toString(); |
|---|
| 398 | if (fj.definesApi(name)) { |
|---|
| 399 | p = name.getText(); |
|---|
| 400 | int idot = m.lastIndexOf("."); |
|---|
| 401 | if (idot != -1) { |
|---|
| 402 | p = p + "/" + m.substring(0,idot); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | } else { |
|---|
| 406 | p = javaPackageClassForApi(name.getText(), ".").toString(); |
|---|
| 407 | } |
|---|
| 408 | p = Useful.replace(p, ".", "/") ; |
|---|
| 409 | return p; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | public String apiAndMethodToMethod(APIName name, Function method) { |
|---|
| 413 | String m = method.toUndecoratedName().toString(); |
|---|
| 414 | if (fj.definesApi(name)) { |
|---|
| 415 | int idot = m.lastIndexOf("."); |
|---|
| 416 | if (idot != -1) { |
|---|
| 417 | m = m.substring(idot+1); |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | return m; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | public static String classNameForComponentEnvironment(APIName componentName) { |
|---|
| 429 | return classNameForComponentEnvironment(NodeUtil.nameString(componentName)); |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | public static String classNameForComponentEnvironment(String componentName) { |
|---|
| 438 | componentName = componentName + TopLevelEnvGen.COMPONENT_ENV_SUFFIX; |
|---|
| 439 | componentName = mangleClassIdentifier(componentName); |
|---|
| 440 | return componentName; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | public static String classNameForApiEnvironment(APIName apiName) { |
|---|
| 450 | return classNameForApiEnvironment(NodeUtil.nameString(apiName)); |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | public static String classNameForApiEnvironment(String apiName) { |
|---|
| 460 | apiName = apiName + TopLevelEnvGen.API_ENV_SUFFIX; |
|---|
| 461 | apiName = mangleClassIdentifier(apiName); |
|---|
| 462 | return apiName; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | public static String mangleClassIdentifier(String identifier) { |
|---|
| 466 | String mangledString = identifier.replaceAll("\\.", "\\$"); |
|---|
| 467 | return mangledString+deCase(mangledString); |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | public static String [] extendsClauseToInterfaces(List<TraitTypeWhere> extendsC, APIName ifMissing) { |
|---|
| 476 | String [] result = new String[extendsC.size()]; |
|---|
| 477 | int i = -1; |
|---|
| 478 | for (TraitTypeWhere ttw : extendsC) { |
|---|
| 479 | i++; |
|---|
| 480 | BaseType parentType = ttw.getBaseType(); |
|---|
| 481 | if ( !(parentType instanceof TraitType) ) { |
|---|
| 482 | if ( parentType instanceof AnyType ) { |
|---|
| 483 | result[i] = fortressAny; |
|---|
| 484 | continue; |
|---|
| 485 | } |
|---|
| 486 | throw new CompilerError(NodeUtil.getSpan(parentType), |
|---|
| 487 | errorMsg("Invalid type ",parentType," in extends clause.")); |
|---|
| 488 | } |
|---|
| 489 | Id name = ((TraitType)parentType).getName(); |
|---|
| 490 | Option<APIName> apiName = name.getApiName(); |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | String api = apiName.unwrap(ifMissing).getText(); |
|---|
| 498 | |
|---|
| 499 | StringBuilder parent = javaPackageClassForApi(api, "/"); parent.append("$"); parent.append(name.getText()); |
|---|
| 500 | result[i] = parent.toString(); |
|---|
| 501 | } |
|---|
| 502 | return result; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | public static StringBuilder javaPackageClassForApi(String api, String sep) { |
|---|
| 510 | StringBuilder parent = new StringBuilder(); |
|---|
| 511 | if ( WellKnownNames.exportsDefaultLibrary( api ) ) { |
|---|
| 512 | parent.append(fortressPackage); parent.append(sep); |
|---|
| 513 | } |
|---|
| 514 | if (!(sep.equals("."))) { |
|---|
| 515 | api = Useful.replace(api, ".", sep); |
|---|
| 516 | } |
|---|
| 517 | parent.append(api); |
|---|
| 518 | return parent; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | public static String mangleIdentifier(String identifier) { |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | String mangledString = identifier.replaceAll("\\\\", "\\\\-"); |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | mangledString = mangledString.replaceAll("/", "\\\\|"); |
|---|
| 541 | mangledString = mangledString.replaceAll("\\.", "\\\\,"); |
|---|
| 542 | mangledString = mangledString.replaceAll(";", "\\\\?"); |
|---|
| 543 | mangledString = mangledString.replaceAll("\\$", "\\\\%"); |
|---|
| 544 | mangledString = mangledString.replaceAll("<", "\\\\^"); |
|---|
| 545 | mangledString = mangledString.replaceAll(">", "\\\\_"); |
|---|
| 546 | mangledString = mangledString.replaceAll("\\[", "\\\\{"); |
|---|
| 547 | mangledString = mangledString.replaceAll("\\]", "\\\\}"); |
|---|
| 548 | mangledString = mangledString.replaceAll(":", "\\\\!"); |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | mangledString = mangledString.replaceAll("\\ ", "\\\\~"); |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | if (!mangledString.equals(identifier) && !(mangledString.charAt(0) == '\\')) { |
|---|
| 556 | mangledString = "\\=" + mangledString; |
|---|
| 557 | } |
|---|
| 558 | return mangledString; |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | public String mangleAwayFromOverload(String mname) { |
|---|
| 562 | mname += "$SINGLE"; |
|---|
| 563 | return mname; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | private static int taskCount = 0; |
|---|
| 567 | public static String gensymTaskName(String packageAndClassName) { |
|---|
| 568 | return packageAndClassName + "$" + "task" + taskCount++; |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | public static String makeInnerClassName(Id id) { |
|---|
| 572 | return makeInnerClassName(jvmClassForSymbol(id), id.getText()); |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | public static String makeInnerClassName(String packageAndClassName, String t) { |
|---|
| 576 | return packageAndClassName + "$" + t; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | public static String jvmSignatureFor(com.sun.fortress.nodes.Type domain, |
|---|
| 580 | com.sun.fortress.nodes.Type range, |
|---|
| 581 | APIName ifNone) { |
|---|
| 582 | return makeMethodDesc( |
|---|
| 583 | NodeUtil.isVoidType(domain) ? "" : jvmTypeDesc(domain, ifNone), |
|---|
| 584 | jvmTypeDesc(range, ifNone)); |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | public static String jvmSignatureFor(List<com.sun.fortress.nodes.Param> domain, |
|---|
| 588 | com.sun.fortress.nodes.Type range, |
|---|
| 589 | APIName ifNone) { |
|---|
| 590 | String args = ""; |
|---|
| 591 | |
|---|
| 592 | if (domain.size() == 1) |
|---|
| 593 | return jvmSignatureFor(domain.get(0).getIdType().unwrap(), range, ifNone); |
|---|
| 594 | for (Param p : domain) { |
|---|
| 595 | args += jvmTypeDesc(p.getIdType(), ifNone); |
|---|
| 596 | } |
|---|
| 597 | return makeMethodDesc(args, jvmTypeDesc(range, ifNone)); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | public static String jvmSignatureFor(Function f, APIName ifNone) { |
|---|
| 601 | return jvmSignatureFor(f.parameters(), f.getReturnType(), ifNone); |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | public static String jvmSignatureFor(FnDecl f, APIName ifNone) { |
|---|
| 605 | FnHeader h = f.getHeader(); |
|---|
| 606 | return jvmSignatureFor(h.getParams(), h.getReturnType().unwrap(), ifNone); |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | public static String jvmClassForSymbol(IdOrOp fnName) { |
|---|
| 610 | Option<APIName> maybe_api = fnName.getApiName(); |
|---|
| 611 | String result = ""; |
|---|
| 612 | if (maybe_api.isSome()) { |
|---|
| 613 | APIName apiName = maybe_api.unwrap(); |
|---|
| 614 | if (WellKnownNames.exportsDefaultLibrary(apiName.getText())) |
|---|
| 615 | result = result + "fortress/"; |
|---|
| 616 | result = result + apiName.getText(); |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | Debug.debug(Debug.Type.CODEGEN, 1, |
|---|
| 621 | "jvmClassForSymbol(" + fnName +")=" + result); |
|---|
| 622 | return result; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | public static String jvmTypeDesc(com.sun.fortress.nodes.Type type, |
|---|
| 626 | final APIName ifNone) { |
|---|
| 627 | return type.accept(new NodeAbstractVisitor<String>() { |
|---|
| 628 | public void defaultCase(ASTNode x) { |
|---|
| 629 | throw new CompilerError(NodeUtil.getSpan(x), |
|---|
| 630 | "emitDesc of type failed"); |
|---|
| 631 | } |
|---|
| 632 | public String forArrowType(ArrowType t) { |
|---|
| 633 | if (NodeUtil.isVoidType(t.getDomain())) |
|---|
| 634 | return makeMethodDesc("", jvmTypeDesc(t.getRange(), ifNone)); |
|---|
| 635 | else return makeMethodDesc(jvmTypeDesc(t.getDomain(), ifNone), |
|---|
| 636 | jvmTypeDesc(t.getRange(), ifNone)); |
|---|
| 637 | } |
|---|
| 638 | public String forTupleType(TupleType t) { |
|---|
| 639 | if ( NodeUtil.isVoidType(t) ) |
|---|
| 640 | return descFortressVoid; |
|---|
| 641 | if (t.getVarargs().isSome()) |
|---|
| 642 | throw new CompilerError(NodeUtil.getSpan(t), |
|---|
| 643 | "Can't compile VarArgs yet"); |
|---|
| 644 | if (!t.getKeywords().isEmpty()) |
|---|
| 645 | throw new CompilerError(NodeUtil.getSpan(t), |
|---|
| 646 | "Can't compile Keyword args yet"); |
|---|
| 647 | String res = ""; |
|---|
| 648 | for (com.sun.fortress.nodes.Type ty : t.getElements()) { |
|---|
| 649 | res += jvmTypeDesc(ty, ifNone); |
|---|
| 650 | } |
|---|
| 651 | return res; |
|---|
| 652 | } |
|---|
| 653 | public String forAnyType (AnyType t) { |
|---|
| 654 | return descFortressAny; |
|---|
| 655 | } |
|---|
| 656 | public String forTraitType(TraitType t) { |
|---|
| 657 | Id id = t.getName(); |
|---|
| 658 | String name = id.getText(); |
|---|
| 659 | String result = specialFortressDescriptors.get(t); |
|---|
| 660 | if (result != null) { |
|---|
| 661 | Debug.debug(Debug.Type.CODEGEN, 1, "forTrait Type ", t , |
|---|
| 662 | " builtin ", result); |
|---|
| 663 | return result; |
|---|
| 664 | } |
|---|
| 665 | Option<APIName> maybeApi = id.getApiName(); |
|---|
| 666 | if (ifNone == null && !maybeApi.isSome()) { |
|---|
| 667 | throw new CompilerError(NodeUtil.getSpan(id), |
|---|
| 668 | "no api name given for id"); |
|---|
| 669 | } |
|---|
| 670 | APIName api = maybeApi.unwrap(ifNone); |
|---|
| 671 | result = "L" + makeInnerClassName(api.getText(), name) + ";"; |
|---|
| 672 | |
|---|
| 673 | Debug.debug(Debug.Type.CODEGEN, 1, "forTrait Type ", t, " = ", result); |
|---|
| 674 | |
|---|
| 675 | return result; |
|---|
| 676 | } |
|---|
| 677 | }); |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | public static String jvmTypeDesc(Option<com.sun.fortress.nodes.Type> otype, APIName ifNone) { |
|---|
| 681 | if (!otype.isSome()) { |
|---|
| 682 | throw new CompilerError("Expected type information was absent."); |
|---|
| 683 | } |
|---|
| 684 | return jvmTypeDesc(otype.unwrap(), ifNone); |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | } |
|---|