| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.repository; |
|---|
| 19 | |
|---|
| 20 | import java.lang.reflect.Field; |
|---|
| 21 | import java.lang.reflect.Method; |
|---|
| 22 | import java.math.BigInteger; |
|---|
| 23 | import java.util.ArrayList; |
|---|
| 24 | import java.util.Collections; |
|---|
| 25 | import java.util.HashMap; |
|---|
| 26 | import java.util.List; |
|---|
| 27 | import java.util.Map; |
|---|
| 28 | import java.util.Set; |
|---|
| 29 | |
|---|
| 30 | import com.sun.fortress.compiler.IndexBuilder; |
|---|
| 31 | import com.sun.fortress.compiler.index.ApiIndex; |
|---|
| 32 | import com.sun.fortress.exceptions.StaticError; |
|---|
| 33 | import com.sun.fortress.nodes.APIName; |
|---|
| 34 | import com.sun.fortress.nodes.AliasedAPIName; |
|---|
| 35 | import com.sun.fortress.nodes.AliasedSimpleName; |
|---|
| 36 | import com.sun.fortress.nodes.Api; |
|---|
| 37 | import com.sun.fortress.nodes.BaseType; |
|---|
| 38 | import com.sun.fortress.nodes.Decl; |
|---|
| 39 | import com.sun.fortress.nodes.Expr; |
|---|
| 40 | import com.sun.fortress.nodes.FnDecl; |
|---|
| 41 | import com.sun.fortress.nodes.Id; |
|---|
| 42 | import com.sun.fortress.nodes.IdOrOpOrAnonymousName; |
|---|
| 43 | import com.sun.fortress.nodes.Import; |
|---|
| 44 | import com.sun.fortress.nodes.ImportApi; |
|---|
| 45 | import com.sun.fortress.nodes.ImportNames; |
|---|
| 46 | import com.sun.fortress.nodes.Param; |
|---|
| 47 | import com.sun.fortress.nodes.StaticParam; |
|---|
| 48 | import com.sun.fortress.nodes.TraitDecl; |
|---|
| 49 | import com.sun.fortress.nodes.TraitObjectDecl; |
|---|
| 50 | import com.sun.fortress.nodes.TraitTypeWhere; |
|---|
| 51 | import com.sun.fortress.nodes.Type; |
|---|
| 52 | import com.sun.fortress.nodes.WhereClause; |
|---|
| 53 | import com.sun.fortress.nodes_util.Modifiers; |
|---|
| 54 | import com.sun.fortress.nodes_util.NodeFactory; |
|---|
| 55 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 56 | import com.sun.fortress.nodes_util.Span; |
|---|
| 57 | import com.sun.fortress.useful.Bijection; |
|---|
| 58 | import com.sun.fortress.useful.HashBijection; |
|---|
| 59 | import com.sun.fortress.useful.MultiMap; |
|---|
| 60 | import com.sun.fortress.useful.Useful; |
|---|
| 61 | |
|---|
| 62 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 63 | |
|---|
| 64 | public class ForeignJava { |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | MultiMap<APIName, Class> javaImplementedAPIs = new MultiMap<APIName, Class>(); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | MultiMap<APIName, APIName> generatedImports = new MultiMap<APIName, APIName>(); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | MultiMap<APIName, Decl> apiToStaticDecls = new MultiMap<APIName, Decl>(); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | MultiMap<Class, String> itemsFromClasses = new MultiMap<Class, String>(); |
|---|
| 90 | |
|---|
| 91 | Map<APIName, ApiIndex> cachedFakeApis = new HashMap<APIName, ApiIndex>(); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | Bijection<Method, FnDecl> methodToDecl = new HashBijection<Method, FnDecl>(); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | Bijection<Class, TraitDecl> classToTraitDecl = new HashBijection<Class, TraitDecl>(); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | static Span span = NodeFactory.internalSpan; |
|---|
| 122 | |
|---|
| 123 | static Map<Class, Type> specialCases = new HashMap<Class, Type>(); |
|---|
| 124 | static APIName fortLib = |
|---|
| 125 | NodeFactory.makeAPIName(span, "FortressLibrary"); |
|---|
| 126 | |
|---|
| 127 | static void s(Class cl, APIName api, String str) { |
|---|
| 128 | specialCases.put(cl, NodeFactory.makeTraitType(span, false, NodeFactory.makeId(span, api, str))); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | static { |
|---|
| 132 | s(Boolean.class, fortLib, "Boolean"); |
|---|
| 133 | s(Boolean.TYPE, fortLib, "Boolean"); |
|---|
| 134 | s(Integer.class, fortLib, "ZZ32"); |
|---|
| 135 | s(Integer.TYPE, fortLib, "ZZ32"); |
|---|
| 136 | s(Long.class, fortLib, "ZZ64"); |
|---|
| 137 | s(Long.TYPE, fortLib, "ZZ64"); |
|---|
| 138 | s(Float.class, fortLib, "RR32"); |
|---|
| 139 | s(Float.TYPE, fortLib, "RR32"); |
|---|
| 140 | s(Double.class, fortLib, "RR64"); |
|---|
| 141 | s(Double.TYPE, fortLib, "RR64"); |
|---|
| 142 | s(String.class, fortLib, "String"); |
|---|
| 143 | s(BigInteger.class, fortLib, "ZZ"); |
|---|
| 144 | specialCases.put(Void.TYPE, NodeFactory.makeVoidType(span)); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | APIName packageToAPIName(Package p) { |
|---|
| 148 | return NodeFactory.makeAPIName(span, p.getName()); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void processJavaImport(Import i, ImportNames ins) { |
|---|
| 152 | APIName pkg_name = ins.getApiName(); |
|---|
| 153 | String pkg_name_string = pkg_name.getText(); |
|---|
| 154 | List<AliasedSimpleName> names = ins.getAliasedNames(); |
|---|
| 155 | for (AliasedSimpleName name : names) { |
|---|
| 156 | Option<IdOrOpOrAnonymousName> opt_alias = name.getAlias(); |
|---|
| 157 | if (opt_alias.isSome()) { |
|---|
| 158 | throw StaticError.make( |
|---|
| 159 | "Import aliasing not yet implemented for foreign imports ", i); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | IdOrOpOrAnonymousName imported = name.getName(); |
|---|
| 163 | Option<APIName> dotted_prefix = imported.getApiName(); |
|---|
| 164 | String suffix = NodeUtil.nameString(imported); |
|---|
| 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 | int last_dot = suffix.length(); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | Class imported_class = null; |
|---|
| 207 | while (last_dot > 0) { |
|---|
| 208 | String candidate_class = suffix.substring(0,last_dot); |
|---|
| 209 | candidate_class = pkg_name_string + "." + Useful.replace(candidate_class, ".", "$"); |
|---|
| 210 | try { |
|---|
| 211 | imported_class = Class.forName(candidate_class); |
|---|
| 212 | break; |
|---|
| 213 | |
|---|
| 214 | } catch (ClassNotFoundException e) { |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | last_dot = suffix.lastIndexOf('.', last_dot); |
|---|
| 218 | } |
|---|
| 219 | if (imported_class == null) { |
|---|
| 220 | |
|---|
| 221 | throw StaticError.make("Could not find any Java package.class prefix of "+ pkg_name_string + "." + suffix, i); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | String imported_item = suffix.substring(last_dot); |
|---|
| 227 | if (imported_item.length() == 0) { |
|---|
| 228 | |
|---|
| 229 | } else if (imported_item.startsWith(".", 0)) { |
|---|
| 230 | imported_item = imported_item.substring(1); |
|---|
| 231 | } else { |
|---|
| 232 | throw StaticError.make("Internal error processing imported item " + imported_item, i); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | recurOnClass(pkg_name, imported_class, imported_item); |
|---|
| 240 | |
|---|
| 241 | } |
|---|
| 242 | System.err.println("javaImplementedAPIs="+javaImplementedAPIs); |
|---|
| 243 | System.err.println("itemsFromClasses="+itemsFromClasses); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | private Type recurOnOpaqueClass(APIName importing_package, Class imported_class) { |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | Type t = specialCases.get(imported_class); |
|---|
| 253 | if (t != null) |
|---|
| 254 | return t; |
|---|
| 255 | |
|---|
| 256 | Package p = imported_class.getPackage(); |
|---|
| 257 | APIName api_name = packageToAPIName(p); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | generatedImports.putItem(importing_package, api_name); |
|---|
| 264 | |
|---|
| 265 | Id name = NodeFactory.makeId(span, imported_class.getSimpleName()); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | if (!itemsFromClasses.containsKey(imported_class)) { |
|---|
| 274 | itemsFromClasses.putKey(imported_class); |
|---|
| 275 | |
|---|
| 276 | classToTraitType(imported_class, api_name, name, Collections.<Decl>emptyList()); |
|---|
| 277 | }; |
|---|
| 278 | |
|---|
| 279 | name = NodeFactory.makeId(api_name, name); |
|---|
| 280 | |
|---|
| 281 | return NodeFactory.makeTraitType(span, false, name); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | private void classToTraitType(Class imported_class, APIName api_name, |
|---|
| 285 | Id name, List<Decl> decls) { |
|---|
| 286 | List<StaticParam> sparams = Collections.emptyList(); |
|---|
| 287 | List<TraitTypeWhere> extendsC = Collections.emptyList(); |
|---|
| 288 | Modifiers mods = Modifiers.None; |
|---|
| 289 | Option<WhereClause> whereC = Option.none(); |
|---|
| 290 | List<BaseType> excludesC = Collections.emptyList(); |
|---|
| 291 | Option<List<BaseType>> comprisesC = Option.none(); |
|---|
| 292 | TraitDecl td = NodeFactory.makeTraitDecl (span, mods, name, sparams, extendsC, whereC, decls, excludesC, comprisesC); |
|---|
| 293 | |
|---|
| 294 | classToTraitDecl.put(imported_class, td); |
|---|
| 295 | apiToStaticDecls.putItem(api_name, td); |
|---|
| 296 | javaImplementedAPIs.putItem(api_name, imported_class); |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | cachedFakeApis.remove(api_name); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | private void recurOnClass(APIName pkg_name, Class imported_class, String imported_item) { |
|---|
| 305 | Set<String> old = itemsFromClasses.get(imported_class); |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | if (old != null && old.size() > 0) { |
|---|
| 313 | itemsFromClasses.putItem(imported_class, imported_item); |
|---|
| 314 | return; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | itemsFromClasses.putItem(imported_class, imported_item); |
|---|
| 322 | |
|---|
| 323 | Method[] methods = imported_class.getDeclaredMethods(); |
|---|
| 324 | Field[] fields = imported_class.getDeclaredFields(); |
|---|
| 325 | Class[] interfaces = imported_class.getInterfaces(); |
|---|
| 326 | Class super_class = imported_class.getSuperclass(); |
|---|
| 327 | |
|---|
| 328 | ArrayList<Decl> trait_decls = new ArrayList<Decl>(); |
|---|
| 329 | |
|---|
| 330 | for (Method m : methods) { |
|---|
| 331 | if (isPublic(m.getModifiers())) { |
|---|
| 332 | if (isStatic(m.getModifiers())) { |
|---|
| 333 | |
|---|
| 334 | FnDecl decl = recurOnMethod(pkg_name, imported_class, m, true); |
|---|
| 335 | apiToStaticDecls.putItem(pkg_name, decl); |
|---|
| 336 | } else { |
|---|
| 337 | |
|---|
| 338 | FnDecl decl = recurOnMethod(pkg_name, imported_class, m, false); |
|---|
| 339 | trait_decls.add(decl); |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | Id name = NodeFactory.makeId(span, imported_class.getSimpleName()); |
|---|
| 345 | classToTraitType(imported_class, pkg_name, name, trait_decls); |
|---|
| 346 | |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | private FnDecl recurOnMethod(APIName importing_package, Class cl, Method m, boolean is_static) { |
|---|
| 350 | if (methodToDecl.containsKey(m)) |
|---|
| 351 | return methodToDecl.get(m); |
|---|
| 352 | |
|---|
| 353 | Class rt = m.getReturnType(); |
|---|
| 354 | Class[] pts = m.getParameterTypes(); |
|---|
| 355 | |
|---|
| 356 | Type return_type = recurOnOpaqueClass(importing_package, rt); |
|---|
| 357 | |
|---|
| 358 | List<Param> params = new ArrayList<Param>(pts.length); |
|---|
| 359 | int i = 0; |
|---|
| 360 | for (Class pt : pts) { |
|---|
| 361 | Type type = recurOnOpaqueClass(importing_package, pt); |
|---|
| 362 | Id id = NodeFactory.makeId(span, "p"+(i++)); |
|---|
| 363 | Param p = NodeFactory.makeParam(id, type); |
|---|
| 364 | params.add(p); |
|---|
| 365 | } |
|---|
| 366 | Id id = is_static ? |
|---|
| 367 | NodeFactory.makeId(span, cl.getSimpleName()+"."+ m.getName()) : |
|---|
| 368 | NodeFactory.makeId(span, m.getName()); |
|---|
| 369 | FnDecl fndecl = NodeFactory.makeFnDecl(span, Modifiers.None, |
|---|
| 370 | id, params,Option.some(return_type), Option.<Expr>none()); |
|---|
| 371 | methodToDecl.put(m, fndecl); |
|---|
| 372 | return fndecl; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | private boolean isPublic(int modifiers) { |
|---|
| 376 | return 0 != (modifiers & java.lang.reflect.Modifier.PUBLIC); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | private boolean isStatic(int modifiers) { |
|---|
| 380 | return 0 != (modifiers & java.lang.reflect.Modifier.STATIC); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | public boolean definesApi(APIName name) { |
|---|
| 384 | return javaImplementedAPIs.containsKey(name); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | public ApiIndex fakeApi(APIName name) { |
|---|
| 388 | ApiIndex result = cachedFakeApis.get(name); |
|---|
| 389 | if (result == null) { |
|---|
| 390 | |
|---|
| 391 | Set<Class> classes = javaImplementedAPIs.get(name); |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | List<Import> imports = new ArrayList<Import>(); |
|---|
| 396 | Set<APIName> gi = generatedImports.get(name); |
|---|
| 397 | if (gi != null) |
|---|
| 398 | for (APIName a : gi) { |
|---|
| 399 | AliasedAPIName aan = NodeFactory.makeAliasedAPIName(a); |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | ImportApi iapi = NodeFactory.makeImportApi(span, Option |
|---|
| 410 | .<String> none(), Useful.list(aan)); |
|---|
| 411 | imports.add(iapi); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | List<Decl> decls = new ArrayList<Decl>(); |
|---|
| 415 | for (Decl d : apiToStaticDecls.get(name)) { |
|---|
| 416 | decls.add(d); |
|---|
| 417 | } |
|---|
| 418 | Api a = NodeFactory.makeApi(span, name, imports, decls); |
|---|
| 419 | |
|---|
| 420 | result = IndexBuilder.builder.buildApiIndex(a, Long.MIN_VALUE + 2); |
|---|
| 421 | cachedFakeApis.put(name, result); |
|---|
| 422 | } |
|---|
| 423 | return result; |
|---|
| 424 | |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | public Map<APIName, ApiIndex> augmentApiMap(Map<APIName, ApiIndex> map) { |
|---|
| 428 | for (APIName a : javaImplementedAPIs.keySet()) { |
|---|
| 429 | map.put(a, fakeApi(a)); |
|---|
| 430 | } |
|---|
| 431 | return map; |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | } |
|---|