genjava.ml 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. open JData
  23. open Unix
  24. open Ast
  25. open Common
  26. open Gencommon
  27. open Gencommon.SourceWriter
  28. open Type
  29. open Printf
  30. open Option
  31. open ExtString
  32. let is_boxed_type t = match follow t with
  33. | TInst ({ cl_path = (["java";"lang"], "Boolean") }, [])
  34. | TInst ({ cl_path = (["java";"lang"], "Double") }, [])
  35. | TInst ({ cl_path = (["java";"lang"], "Integer") }, [])
  36. | TInst ({ cl_path = (["java";"lang"], "Byte") }, [])
  37. | TInst ({ cl_path = (["java";"lang"], "Short") }, [])
  38. | TInst ({ cl_path = (["java";"lang"], "Character") }, [])
  39. | TInst ({ cl_path = (["java";"lang"], "Float") }, []) -> true
  40. | _ -> false
  41. let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with
  42. | TInst ({ cl_path = (["java";"lang"], "Boolean") }, []) -> gen.gcon.basic.tbool
  43. | TInst ({ cl_path = (["java";"lang"], "Double") }, []) -> gen.gcon.basic.tfloat
  44. | TInst ({ cl_path = (["java";"lang"], "Integer") }, []) -> gen.gcon.basic.tint
  45. | TInst ({ cl_path = (["java";"lang"], "Byte") }, []) -> tbyte
  46. | TInst ({ cl_path = (["java";"lang"], "Short") }, []) -> tshort
  47. | TInst ({ cl_path = (["java";"lang"], "Character") }, []) -> tchar
  48. | TInst ({ cl_path = (["java";"lang"], "Float") }, []) -> tfloat
  49. | _ -> assert false
  50. let rec t_has_type_param t = match follow t with
  51. | TInst({ cl_kind = KTypeParameter _ }, []) -> true
  52. | TEnum(_, params)
  53. | TInst(_, params) -> List.exists t_has_type_param params
  54. | TFun(f,ret) -> t_has_type_param ret || List.exists (fun (_,_,t) -> t_has_type_param t) f
  55. | _ -> false
  56. let rec t_has_type_param_shallow last t = match follow t with
  57. | TInst({ cl_kind = KTypeParameter _ }, []) -> true
  58. | TEnum(_, params)
  59. | TInst(_, params) when not last -> List.exists (t_has_type_param_shallow true) params
  60. | TFun(f,ret) when not last -> t_has_type_param_shallow true ret || List.exists (fun (_,_,t) -> t_has_type_param_shallow true t) f
  61. | _ -> false
  62. let is_java_basic_type t =
  63. match follow t with
  64. | TInst( { cl_path = (["haxe"], "Int32") }, [] )
  65. | TInst( { cl_path = (["haxe"], "Int64") }, [] )
  66. | TInst( { cl_path = ([], "Int") }, [] ) | TAbstract( { a_path = ([], "Int") }, [] )
  67. | TInst( { cl_path = ([], "Float") }, [] ) | TAbstract( { a_path = ([], "Float") }, [] )
  68. | TEnum( { e_path = ([], "Bool") }, [] ) | TAbstract( { a_path = ([], "Bool") }, [] ) ->
  69. true
  70. | _ -> false
  71. let is_bool t =
  72. match follow t with
  73. | TEnum( { e_path = ([], "Bool") }, [] )
  74. | TAbstract ({ a_path = ([], "Bool") },[]) ->
  75. true
  76. | _ -> false
  77. let is_int_float gen t =
  78. match follow (gen.greal_type t) with
  79. | TInst( { cl_path = (["haxe"], "Int64") }, [] )
  80. | TInst( { cl_path = (["haxe"], "Int32") }, [] )
  81. | TInst( { cl_path = ([], "Int") }, [] ) | TAbstract( { a_path = ([], "Int") }, [] )
  82. | TInst( { cl_path = ([], "Float") }, [] ) | TAbstract( { a_path = ([], "Float") }, [] ) ->
  83. true
  84. | (TAbstract _ as t) when like_float t -> true
  85. | _ -> false
  86. let parse_explicit_iface =
  87. let regex = Str.regexp "\\." in
  88. let parse_explicit_iface str =
  89. let split = Str.split regex str in
  90. let rec get_iface split pack =
  91. match split with
  92. | clname :: fn_name :: [] -> fn_name, (List.rev pack, clname)
  93. | pack_piece :: tl -> get_iface tl (pack_piece :: pack)
  94. | _ -> assert false
  95. in
  96. get_iface split []
  97. in parse_explicit_iface
  98. let is_string t =
  99. match follow t with
  100. | TInst( { cl_path = ([], "String") }, [] ) -> true
  101. | _ -> false
  102. (* ******************************************* *)
  103. (* JavaSpecificESynf *)
  104. (* ******************************************* *)
  105. (*
  106. Some Java-specific syntax filters that must run before ExpressionUnwrap
  107. dependencies:
  108. It must run before ExprUnwrap, as it may not return valid Expr/Statement expressions
  109. It must run before ClassInstance, as it will detect expressions that need unchanged TTypeExpr
  110. It must run after CastDetect, as it changes casts
  111. It must run after TryCatchWrapper, to change Std.is() calls inside there
  112. *)
  113. module JavaSpecificESynf =
  114. struct
  115. let name = "java_specific_e"
  116. let priority = solve_deps name [ DBefore ExpressionUnwrap.priority; DBefore ClassInstance.priority; DAfter CastDetect.priority; DAfter TryCatchWrapper.priority ]
  117. let get_cl_from_t t =
  118. match follow t with
  119. | TInst(cl,_) -> cl
  120. | _ -> assert false
  121. let traverse gen runtime_cl =
  122. let basic = gen.gcon.basic in
  123. let float_cl = get_cl ( get_type gen (["java";"lang"], "Double")) in
  124. let bool_md = get_type gen (["java";"lang"], "Boolean") in
  125. let is_var = alloc_var "__is__" t_dynamic in
  126. let rec run e =
  127. match e.eexpr with
  128. (* Math changes *)
  129. | TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "NaN" }) ) ->
  130. mk_static_field_access_infer float_cl "NaN" e.epos []
  131. | TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "NEGATIVE_INFINITY" }) ) ->
  132. mk_static_field_access_infer float_cl "NEGATIVE_INFINITY" e.epos []
  133. | TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "POSITIVE_INFINITY" }) ) ->
  134. mk_static_field_access_infer float_cl "POSITIVE_INFINITY" e.epos []
  135. | TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "isNaN"}) ) ->
  136. mk_static_field_access_infer float_cl "_isNaN" e.epos []
  137. | TCall( ({ eexpr = TField( (_ as ef), FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = ("ffloor" as f) }) ) } as fe), p)
  138. | TCall( ({ eexpr = TField( (_ as ef), FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = ("fceil" as f) }) ) } as fe), p) ->
  139. Type.map_expr run { e with eexpr = TCall({ fe with eexpr = TField(ef, FDynamic (String.sub f 1 (String.length f - 1))) }, p) }
  140. | TCall( ({ eexpr = TField( (_ as ef), FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = ("fround") }) ) } as fe), p) ->
  141. Type.map_expr run { e with eexpr = TCall({ fe with eexpr = TField(ef, FDynamic "rint") }, p) }
  142. | TCall( { eexpr = TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "floor" }) ) }, _)
  143. | TCall( { eexpr = TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "round" }) ) }, _)
  144. | TCall( { eexpr = TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "ceil" }) ) }, _) ->
  145. mk_cast basic.tint (Type.map_expr run e)
  146. | TCall( ( { eexpr = TField( _, FStatic({ cl_path = (["java";"lang"], "Math") }, { cf_name = "isFinite" }) ) } as efield ), [v]) ->
  147. { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "isFinite" efield.epos [], [run v] ) }
  148. (* end of math changes *)
  149. (* Std.is() *)
  150. | TCall(
  151. { eexpr = TField( _, FStatic({ cl_path = ([], "Std") }, { cf_name = "is" })) },
  152. [ obj; { eexpr = TTypeExpr(md) } ]
  153. ) ->
  154. let mk_is obj md =
  155. { e with eexpr = TCall( { eexpr = TLocal is_var; etype = t_dynamic; epos = e.epos }, [
  156. run obj;
  157. { eexpr = TTypeExpr md; etype = t_dynamic (* this is after all a syntax filter *); epos = e.epos }
  158. ] ) }
  159. in
  160. (match follow_module follow md with
  161. | TClassDecl({ cl_path = ([], "Float") })
  162. | TAbstractDecl({ a_path = ([], "Float") }) ->
  163. {
  164. eexpr = TCall(
  165. mk_static_field_access_infer runtime_cl "isDouble" e.epos [],
  166. [ run obj ]
  167. );
  168. etype = basic.tbool;
  169. epos = e.epos
  170. }
  171. | TClassDecl{ cl_path = ([], "Int") }
  172. | TAbstractDecl{ a_path = ([], "Int") } ->
  173. {
  174. eexpr = TCall(
  175. mk_static_field_access_infer runtime_cl "isInt" e.epos [],
  176. [ run obj ]
  177. );
  178. etype = basic.tbool;
  179. epos = e.epos
  180. }
  181. | TAbstractDecl{ a_path = ([], "Bool") }
  182. | TEnumDecl{ e_path = ([], "Bool") } ->
  183. mk_is obj bool_md
  184. | TAbstractDecl{ a_path = ([], "Dynamic") }
  185. | TClassDecl{ cl_path = ([], "Dynamic") } ->
  186. (match obj.eexpr with
  187. | TLocal _ | TConst _ -> { e with eexpr = TConst(TBool true) }
  188. | _ -> { e with eexpr = TBlock([run obj; { e with eexpr = TConst(TBool true) }]) }
  189. )
  190. | _ ->
  191. mk_is obj md
  192. )
  193. (* end Std.is() *)
  194. | _ -> Type.map_expr run e
  195. in
  196. run
  197. let configure gen (mapping_func:texpr->texpr) =
  198. let map e = Some(mapping_func e) in
  199. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  200. end;;
  201. (* ******************************************* *)
  202. (* JavaSpecificSynf *)
  203. (* ******************************************* *)
  204. (*
  205. Some Java-specific syntax filters that can run after ExprUnwrap
  206. dependencies:
  207. Runs after ExprUnwarp
  208. *)
  209. module JavaSpecificSynf =
  210. struct
  211. let name = "java_specific"
  212. let priority = solve_deps name [ DAfter ExpressionUnwrap.priority; DAfter ObjectDeclMap.priority; DAfter ArrayDeclSynf.priority; DBefore IntDivisionSynf.priority ]
  213. let java_hash s =
  214. let h = ref Int32.zero in
  215. let thirtyone = Int32.of_int 31 in
  216. for i = 0 to String.length s - 1 do
  217. h := Int32.add (Int32.mul thirtyone !h) (Int32.of_int (int_of_char (String.unsafe_get s i)));
  218. done;
  219. !h
  220. let rec is_final_return_expr is_switch e =
  221. let is_final_return_expr = is_final_return_expr is_switch in
  222. match e.eexpr with
  223. | TReturn _
  224. | TThrow _ -> true
  225. (* this is hack to not use 'break' on switch cases *)
  226. | TLocal { v_name = "__fallback__" } when is_switch -> true
  227. | TCall( { eexpr = TLocal { v_name = "__goto__" } }, _ ) -> true
  228. | TParenthesis p -> is_final_return_expr p
  229. | TBlock bl -> is_final_return_block is_switch bl
  230. | TSwitch (_, el_e_l, edef) ->
  231. List.for_all (fun (_,e) -> is_final_return_expr e) el_e_l && Option.map_default is_final_return_expr false edef
  232. | TMatch (_, _, il_vl_e_l, edef) ->
  233. List.for_all (fun (_,_,e) -> is_final_return_expr e)il_vl_e_l && Option.map_default is_final_return_expr false edef
  234. | TIf (_,eif, Some eelse) ->
  235. is_final_return_expr eif && is_final_return_expr eelse
  236. | TFor (_,_,e) ->
  237. is_final_return_expr e
  238. | TWhile (_,e,_) ->
  239. is_final_return_expr e
  240. | TFunction tf ->
  241. is_final_return_expr tf.tf_expr
  242. | TTry (e, ve_l) ->
  243. is_final_return_expr e && List.for_all (fun (_,e) -> is_final_return_expr e) ve_l
  244. | _ -> false
  245. and is_final_return_block is_switch el =
  246. match el with
  247. | [] -> false
  248. | final :: [] -> is_final_return_expr is_switch final
  249. | hd :: tl -> is_final_return_block is_switch tl
  250. let is_null e = match e.eexpr with | TConst(TNull) -> true | _ -> false
  251. let rec is_equatable gen t =
  252. match follow t with
  253. | TInst(cl,_) ->
  254. if cl.cl_path = (["haxe";"lang"], "IEquatable") then
  255. true
  256. else
  257. List.exists (fun (cl,p) -> is_equatable gen (TInst(cl,p))) cl.cl_implements
  258. || (match cl.cl_super with | Some(cl,p) -> is_equatable gen (TInst(cl,p)) | None -> false)
  259. | _ -> false
  260. (*
  261. Changing string switch
  262. will take an expression like
  263. switch(str)
  264. {
  265. case "a":
  266. case "b":
  267. }
  268. and modify it to:
  269. {
  270. var execute_def = true;
  271. switch(str.hashCode())
  272. {
  273. case (hashcode of a):
  274. if (str == "a")
  275. {
  276. execute_def = false;
  277. ..code here
  278. } //else if (str == otherVariableWithSameHashCode) {
  279. ...
  280. }
  281. ...
  282. }
  283. if (execute_def)
  284. {
  285. ..default code
  286. }
  287. }
  288. this might actually be slower in some cases than a if/else approach, but it scales well and as a bonus,
  289. hashCode in java are cached, so we only have the performance hit once to cache it.
  290. *)
  291. let change_string_switch gen eswitch e1 ecases edefault =
  292. let basic = gen.gcon.basic in
  293. let is_final_ret = is_final_return_expr false eswitch in
  294. let has_default = is_some edefault in
  295. let block = ref [] in
  296. let local = match e1.eexpr with
  297. | TLocal _ -> e1
  298. | _ ->
  299. let var = mk_temp gen "svar" e1.etype in
  300. let added = { e1 with eexpr = TVars([var, Some(e1)]); etype = basic.tvoid } in
  301. let local = mk_local var e1.epos in
  302. block := added :: !block;
  303. local
  304. in
  305. let execute_def_var = mk_temp gen "executeDef" gen.gcon.basic.tbool in
  306. let execute_def = mk_local execute_def_var e1.epos in
  307. let execute_def_set = { eexpr = TBinop(Ast.OpAssign, execute_def, { eexpr = TConst(TBool false); etype = basic.tbool; epos = e1.epos }); etype = basic.tbool; epos = e1.epos } in
  308. let hash_cache = ref None in
  309. let local_hashcode = ref { local with
  310. eexpr = TCall({ local with
  311. eexpr = TField(local, FDynamic "hashCode");
  312. etype = TFun([], basic.tint);
  313. }, []);
  314. etype = basic.tint
  315. } in
  316. let get_hash_cache () =
  317. match !hash_cache with
  318. | Some c -> c
  319. | None ->
  320. let var = mk_temp gen "hash" basic.tint in
  321. let cond = !local_hashcode in
  322. block := { eexpr = TVars([var, Some cond]); etype = basic.tvoid; epos = local.epos } :: !block;
  323. let local = mk_local var local.epos in
  324. local_hashcode := local;
  325. hash_cache := Some local;
  326. local
  327. in
  328. let has_case = ref false in
  329. (* first we need to reorder all cases so all collisions are close to each other *)
  330. let get_str e = match e.eexpr with | TConst(TString s) -> s | _ -> assert false in
  331. let has_conflict = ref false in
  332. let rec reorder_cases unordered ordered =
  333. match unordered with
  334. | [] -> ordered
  335. | (el, e) :: tl ->
  336. let current = Hashtbl.create 1 in
  337. List.iter (fun e ->
  338. let str = get_str e in
  339. let hash = java_hash str in
  340. Hashtbl.add current hash true
  341. ) el;
  342. let rec extract_fields cases found_cases ret_cases =
  343. match cases with
  344. | [] -> found_cases, ret_cases
  345. | (el, e) :: tl ->
  346. if List.exists (fun e -> Hashtbl.mem current (java_hash (get_str e)) ) el then begin
  347. has_conflict := true;
  348. List.iter (fun e -> Hashtbl.add current (java_hash (get_str e)) true) el;
  349. extract_fields tl ( (el, e) :: found_cases ) ret_cases
  350. end else
  351. extract_fields tl found_cases ( (el, e) :: ret_cases )
  352. in
  353. let found, remaining = extract_fields tl [] [] in
  354. let ret = if found <> [] then
  355. let ret = List.sort (fun (e1,_) (e2,_) -> compare (List.length e2) (List.length e1) ) ( (el, e) :: found ) in
  356. let rec loop ret acc =
  357. match ret with
  358. | (el, e) :: ( (_,_) :: _ as tl ) -> loop tl ( (true, el, e) :: acc )
  359. | (el, e) :: [] -> ( (false, el, e) :: acc )
  360. | _ -> assert false
  361. in
  362. List.rev (loop ret [])
  363. else
  364. (false, el, e) :: []
  365. in
  366. reorder_cases remaining (ordered @ ret)
  367. in
  368. let already_in_cases = Hashtbl.create 0 in
  369. let change_case (has_fallback, el, e) =
  370. let conds, el = List.fold_left (fun (conds,el) e ->
  371. has_case := true;
  372. match e.eexpr with
  373. | TConst(TString s) ->
  374. let hashed = java_hash s in
  375. let equals_test = {
  376. eexpr = TCall({ e with eexpr = TField(local, FDynamic "equals"); etype = TFun(["obj",false,t_dynamic],basic.tbool) }, [ e ]);
  377. etype = basic.tbool;
  378. epos = e.epos
  379. } in
  380. let hashed_expr = { eexpr = TConst(TInt hashed); etype = basic.tint; epos = e.epos } in
  381. let hashed_exprs = if !has_conflict then begin
  382. if Hashtbl.mem already_in_cases hashed then
  383. el
  384. else begin
  385. Hashtbl.add already_in_cases hashed true;
  386. hashed_expr :: el
  387. end
  388. end else hashed_expr :: el in
  389. let conds = match conds with
  390. | None -> equals_test
  391. | Some c ->
  392. (*
  393. if there is more than one case, we should test first if hash equals to the one specified.
  394. This way we can save a heavier string compare
  395. *)
  396. let equals_test = mk_paren {
  397. eexpr = TBinop(Ast.OpBoolAnd, { eexpr = TBinop(Ast.OpEq, get_hash_cache(), hashed_expr); etype = basic.tbool; epos = e.epos }, equals_test);
  398. etype = basic.tbool;
  399. epos = e.epos;
  400. } in
  401. { eexpr = TBinop(Ast.OpBoolOr, equals_test, c); etype = basic.tbool; epos = e1.epos }
  402. in
  403. Some conds, hashed_exprs
  404. | _ -> assert false
  405. ) (None,[]) el in
  406. let e = if has_default then Codegen.concat execute_def_set e else e in
  407. let e = if !has_conflict then Codegen.concat e { e with eexpr = TBreak; etype = basic.tvoid } else e in
  408. let e = {
  409. eexpr = TIf(get conds, e, None);
  410. etype = basic.tvoid;
  411. epos = e.epos
  412. } in
  413. let e = if has_fallback then { e with eexpr = TBlock([ e; mk_local (alloc_var "__fallback__" t_dynamic) e.epos]) } else e in
  414. (el, e)
  415. in
  416. let switch = { eswitch with
  417. eexpr = TSwitch(!local_hashcode, List.map change_case (reorder_cases ecases []), None);
  418. } in
  419. (if !has_case then begin
  420. (if has_default then block := { e1 with eexpr = TVars([execute_def_var, Some({ e1 with eexpr = TConst(TBool true); etype = basic.tbool })]); etype = basic.tvoid } :: !block);
  421. block := switch :: !block
  422. end);
  423. (match edefault with
  424. | None -> ()
  425. | Some edef when not !has_case ->
  426. block := edef :: !block
  427. | Some edef ->
  428. let eelse = if is_final_ret then Some { eexpr = TThrow { eexpr = TConst(TNull); etype = t_dynamic; epos = edef.epos }; etype = basic.tvoid; epos = edef.epos } else None in
  429. block := { edef with eexpr = TIf(execute_def, edef, eelse); etype = basic.tvoid } :: !block
  430. );
  431. { eswitch with eexpr = TBlock(List.rev !block) }
  432. let get_cl_from_t t =
  433. match follow t with
  434. | TInst(cl,_) -> cl
  435. | _ -> assert false
  436. let traverse gen runtime_cl =
  437. let basic = gen.gcon.basic in
  438. let tchar = mt_to_t_dyn ( get_type gen (["java"], "Char16") ) in
  439. let tbyte = mt_to_t_dyn ( get_type gen (["java"], "Int8") ) in
  440. let tshort = mt_to_t_dyn ( get_type gen (["java"], "Int16") ) in
  441. let tsingle = mt_to_t_dyn ( get_type gen ([], "Single") ) in
  442. let string_ext = get_cl ( get_type gen (["haxe";"lang"], "StringExt")) in
  443. let is_string t = match follow t with | TInst({ cl_path = ([], "String") }, []) -> true | _ -> false in
  444. let rec run e =
  445. match e.eexpr with
  446. (* for new NativeArray<T> issues *)
  447. | TNew(({ cl_path = (["java"], "NativeArray") } as cl), [t], el) when t_has_type_param t ->
  448. mk_cast (TInst(cl,[t])) (mk_cast t_dynamic ({ e with eexpr = TNew(cl, [t_empty], List.map run el) }))
  449. (* Std.int() *)
  450. | TCall(
  451. { eexpr = TField( _, FStatic({ cl_path = ([], "Std") }, { cf_name = "int" })) },
  452. [obj]
  453. ) ->
  454. run (mk_cast basic.tint obj)
  455. (* end Std.int() *)
  456. | TField( ef, FInstance({ cl_path = ([], "String") }, { cf_name = "length" }) ) ->
  457. { e with eexpr = TCall(Type.map_expr run e, []) }
  458. | TField( ef, field ) when field_name field = "length" && is_string ef.etype ->
  459. { e with eexpr = TCall(Type.map_expr run e, []) }
  460. | TCall( ( { eexpr = TField(ef, field) } as efield ), args ) when is_string ef.etype && String.get (field_name field) 0 = '_' ->
  461. let field = field_name field in
  462. { e with eexpr = TCall({ efield with eexpr = TField(run ef, FDynamic (String.sub field 1 ( (String.length field) - 1)) )}, List.map run args) }
  463. | TCall( ( { eexpr = TField(ef, FInstance({ cl_path = [], "String" }, field )) } as efield ), args ) ->
  464. let field = field.cf_name in
  465. (match field with
  466. | "charAt" | "charCodeAt" | "split" | "indexOf"
  467. | "lastIndexOf" | "substring" | "substr" ->
  468. { e with eexpr = TCall(mk_static_field_access_infer string_ext field e.epos [], [run ef] @ (List.map run args)) }
  469. | _ ->
  470. { e with eexpr = TCall(run efield, List.map run args) }
  471. )
  472. | TCast(expr, m) when is_boxed_type e.etype ->
  473. (* let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with *)
  474. run { e with etype = unboxed_type gen e.etype tbyte tshort tchar tsingle }
  475. | TCast(expr, _) when is_bool e.etype ->
  476. {
  477. eexpr = TCall(
  478. mk_static_field_access_infer runtime_cl "toBool" expr.epos [],
  479. [ run expr ]
  480. );
  481. etype = basic.tbool;
  482. epos = e.epos
  483. }
  484. | TCast(expr, _) when is_int_float gen e.etype && not (is_int_float gen expr.etype) ->
  485. let needs_cast = match gen.gfollow#run_f e.etype with
  486. | TInst _ -> false
  487. | _ -> true
  488. in
  489. let fun_name = if like_int e.etype then "toInt" else "toDouble" in
  490. let ret = {
  491. eexpr = TCall(
  492. mk_static_field_access_infer runtime_cl fun_name expr.epos [],
  493. [ run expr ]
  494. );
  495. etype = if fun_name = "toDouble" then basic.tfloat else basic.tint;
  496. epos = expr.epos
  497. } in
  498. if needs_cast then mk_cast e.etype ret else ret
  499. (*| TCast(expr, c) when is_int_float gen e.etype ->
  500. (* cases when float x = (float) (java.lang.Double val); *)
  501. (* FIXME: this fix is broken since it will fail on cases where float x = (float) (java.lang.Float val) or similar. FIX THIS *)
  502. let need_second_cast = match gen.gfollow#run_f e.etype with
  503. | TInst _ -> false
  504. | _ -> true
  505. in
  506. if need_second_cast then { e with eexpr = TCast(mk_cast (follow e.etype) (run expr), c) } else Type.map_expr run e*)
  507. | TBinop( (Ast.OpAssignOp OpAdd as op), e1, e2)
  508. | TBinop( (Ast.OpAdd as op), e1, e2) when is_string e.etype || is_string e1.etype || is_string e2.etype ->
  509. let is_assign = match op with Ast.OpAssignOp _ -> true | _ -> false in
  510. let mk_to_string e = { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" e.epos [], [run e] ); etype = gen.gcon.basic.tstring } in
  511. let check_cast e = match gen.greal_type e.etype with
  512. | TDynamic _
  513. | TAbstract({ a_path = ([], "Float") }, [])
  514. | TAbstract({ a_path = ([], "Single") }, []) ->
  515. mk_to_string e
  516. | _ -> run e
  517. in
  518. { e with eexpr = TBinop(op, (if is_assign then run e1 else check_cast e1), check_cast e2) }
  519. | TCast(expr, _) when is_string e.etype ->
  520. { e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [run expr] ) }
  521. | TSwitch(cond, ecases, edefault) when is_string cond.etype ->
  522. (*let change_string_switch gen eswitch e1 ecases edefault =*)
  523. change_string_switch gen e (run cond) (List.map (fun (el,e) -> (el, run e)) ecases) (Option.map run edefault)
  524. | TBinop( (Ast.OpNotEq as op), e1, e2)
  525. | TBinop( (Ast.OpEq as op), e1, e2) when not (is_null e2 || is_null e1) && (is_string e1.etype || is_string e2.etype || is_equatable gen e1.etype || is_equatable gen e2.etype) ->
  526. let static = mk_static_field_access_infer (runtime_cl) "valEq" e1.epos [] in
  527. let eret = { eexpr = TCall(static, [run e1; run e2]); etype = gen.gcon.basic.tbool; epos=e.epos } in
  528. if op = Ast.OpNotEq then { eret with eexpr = TUnop(Ast.Not, Ast.Prefix, eret) } else eret
  529. | _ -> Type.map_expr run e
  530. in
  531. run
  532. let configure gen (mapping_func:texpr->texpr) =
  533. (if java_hash "Testing string hashCode implementation from haXe" <> (Int32.of_int 545883604) then assert false);
  534. let map e = Some(mapping_func e) in
  535. gen.gsyntax_filters#add ~name:name ~priority:(PCustom priority) map
  536. end;;
  537. let connecting_string = "?" (* ? see list here http://www.fileformat.info/info/unicode/category/index.htm and here for C# http://msdn.microsoft.com/en-us/library/aa664670.aspx *)
  538. let default_package = "java" (* I'm having this separated as I'm still not happy with having a cs package. Maybe dotnet would be better? *)
  539. let strict_mode = ref false (* strict mode is so we can check for unexpected information *)
  540. (* reserved c# words *)
  541. let reserved = let res = Hashtbl.create 120 in
  542. List.iter (fun lst -> Hashtbl.add res lst ("_" ^ lst)) ["abstract"; "assert"; "boolean"; "break"; "byte"; "case"; "catch"; "char"; "class";
  543. "const"; "continue"; "default"; "do"; "double"; "else"; "enum"; "extends"; "final";
  544. "false"; "finally"; "float"; "for"; "goto"; "if"; "implements"; "import"; "instanceof"; "int";
  545. "interface"; "long"; "native"; "new"; "null"; "package"; "private"; "protected"; "public"; "return"; "short";
  546. "static"; "strictfp"; "super"; "switch"; "synchronized"; "this"; "throw"; "throws"; "transient"; "true"; "try";
  547. "void"; "volatile"; "while"; ];
  548. res
  549. let dynamic_anon = TAnon( { a_fields = PMap.empty; a_status = ref Closed } )
  550. let rec get_class_modifiers meta cl_type cl_access cl_modifiers =
  551. match meta with
  552. | [] -> cl_type,cl_access,cl_modifiers
  553. (*| (Meta.Struct,[],_) :: meta -> get_class_modifiers meta "struct" cl_access cl_modifiers*)
  554. | (Meta.Protected,[],_) :: meta -> get_class_modifiers meta cl_type "protected" cl_modifiers
  555. | (Meta.Internal,[],_) :: meta -> get_class_modifiers meta cl_type "" cl_modifiers
  556. (* no abstract for now | (":abstract",[],_) :: meta -> get_class_modifiers meta cl_type cl_access ("abstract" :: cl_modifiers)
  557. | (Meta.Static,[],_) :: meta -> get_class_modifiers meta cl_type cl_access ("static" :: cl_modifiers) TODO: support those types *)
  558. | (Meta.Final,[],_) :: meta -> get_class_modifiers meta cl_type cl_access ("final" :: cl_modifiers)
  559. | _ :: meta -> get_class_modifiers meta cl_type cl_access cl_modifiers
  560. let rec get_fun_modifiers meta access modifiers =
  561. match meta with
  562. | [] -> access,modifiers
  563. | (Meta.Protected,[],_) :: meta -> get_fun_modifiers meta "protected" modifiers
  564. | (Meta.Internal,[],_) :: meta -> get_fun_modifiers meta "" modifiers
  565. (*| (Meta.ReadOnly,[],_) :: meta -> get_fun_modifiers meta access ("readonly" :: modifiers)*)
  566. (*| (Meta.Unsafe,[],_) :: meta -> get_fun_modifiers meta access ("unsafe" :: modifiers)*)
  567. | (Meta.Volatile,[],_) :: meta -> get_fun_modifiers meta access ("volatile" :: modifiers)
  568. | (Meta.Transient,[],_) :: meta -> get_fun_modifiers meta access ("transient" :: modifiers)
  569. | _ :: meta -> get_fun_modifiers meta access modifiers
  570. (* this was the way I found to pass the generator context to be accessible across all functions here *)
  571. (* so 'configure' is almost 'top-level' and will have all functions needed to make this work *)
  572. let configure gen =
  573. let basic = gen.gcon.basic in
  574. let fn_cl = get_cl (get_type gen (["haxe";"lang"],"Function")) in
  575. let runtime_cl = get_cl (get_type gen (["haxe";"lang"],"Runtime")) in
  576. (*let string_ref = get_cl ( get_type gen (["haxe";"lang"], "StringRefl")) in*)
  577. let ti64 = match ( get_type gen (["haxe";"_Int64"], "NativeInt64") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
  578. let has_tdynamic params =
  579. List.exists (fun e -> match gen.greal_type e with | TDynamic _ -> true | _ -> false) params
  580. in
  581. (*
  582. The type parameters always need to be changed to their boxed counterparts
  583. *)
  584. let change_param_type md params =
  585. match md with
  586. | TClassDecl( { cl_path = (["java"], "NativeArray") } ) -> params
  587. | _ ->
  588. match params with
  589. | [] -> []
  590. | _ ->
  591. if has_tdynamic params then List.map (fun _ -> t_dynamic) params else
  592. List.map (fun t ->
  593. let f_t = gen.gfollow#run_f t in
  594. match f_t with
  595. | TEnum ({ e_path = ([], "Bool") }, [])
  596. | TAbstract ({ a_path = ([], "Bool") },[])
  597. | TInst ({ cl_path = ([],"Float") },[])
  598. | TAbstract ({ a_path = ([],"Float") },[])
  599. | TInst ({ cl_path = ["haxe"],"Int32" },[])
  600. | TInst ({ cl_path = ["haxe"],"Int64" },[])
  601. | TInst ({ cl_path = ([],"Int") },[])
  602. | TAbstract ({ a_path = ([],"Int") },[])
  603. | TType ({ t_path = ["haxe";"_Int64"], "NativeInt64" },[])
  604. | TAbstract ({ a_path = ["haxe";"_Int64"], "NativeInt64" },[])
  605. | TType ({ t_path = ["java"],"Int8" },[])
  606. | TAbstract ({ a_path = ["java"],"Int8" },[])
  607. | TType ({ t_path = ["java"],"Int16" },[])
  608. | TAbstract ({ a_path = ["java"],"Int16" },[])
  609. | TType ({ t_path = ["java"],"Char16" },[])
  610. | TAbstract ({ a_path = ["java"],"Char16" },[])
  611. | TType ({ t_path = [],"Single" },[])
  612. | TAbstract ({ a_path = [],"Single" },[]) ->
  613. t_dynamic
  614. (*basic.tnull f_t*)
  615. (*| TType ({ t_path = [], "Null"*)
  616. | TInst (cl, ((_ :: _) as p)) ->
  617. TInst(cl, List.map (fun _ -> t_dynamic) p)
  618. | TEnum (e, ((_ :: _) as p)) ->
  619. TEnum(e, List.map (fun _ -> t_dynamic) p)
  620. | _ -> t
  621. ) params
  622. in
  623. let rec change_ns ns = match ns with
  624. | [] -> ["haxe"; "root"]
  625. | _ -> ns
  626. in
  627. let change_clname name =
  628. String.map (function | '$' -> '.' | c -> c) name
  629. in
  630. let change_id name = try Hashtbl.find reserved name with | Not_found -> name in
  631. let change_field = change_id in
  632. let write_id w name = write w (change_id name) in
  633. let write_field w name = write w (change_field name) in
  634. gen.gfollow#add ~name:"follow_basic" (fun t -> match t with
  635. | TEnum ({ e_path = ([], "Bool") }, [])
  636. | TAbstract ({ a_path = ([], "Bool") },[])
  637. | TEnum ({ e_path = ([], "Void") }, [])
  638. | TAbstract ({ a_path = ([], "Void") },[])
  639. | TInst ({ cl_path = ([],"Float") },[])
  640. | TAbstract ({ a_path = ([],"Float") },[])
  641. | TInst ({ cl_path = ([],"Int") },[])
  642. | TAbstract ({ a_path = ([],"Int") },[])
  643. | TInst( { cl_path = (["haxe"], "Int32") }, [] )
  644. | TInst( { cl_path = (["haxe"], "Int64") }, [] )
  645. | TType ({ t_path = ["haxe";"_Int64"], "NativeInt64" },[])
  646. | TAbstract ({ a_path = ["haxe";"_Int64"], "NativeInt64" },[])
  647. | TType ({ t_path = ["java"],"Int8" },[])
  648. | TAbstract ({ a_path = ["java"],"Int8" },[])
  649. | TType ({ t_path = ["java"],"Int16" },[])
  650. | TAbstract ({ a_path = ["java"],"Int16" },[])
  651. | TType ({ t_path = ["java"],"Char16" },[])
  652. | TAbstract ({ a_path = ["java"],"Char16" },[])
  653. | TType ({ t_path = [],"Single" },[])
  654. | TAbstract ({ a_path = [],"Single" },[])
  655. | TType ({ t_path = [],"Null" },[_]) -> Some t
  656. | TAbstract ({ a_impl = Some _ } as a, pl) ->
  657. Some (gen.gfollow#run_f ( Codegen.Abstract.get_underlying_type a pl) )
  658. | TAbstract( { a_path = ([], "EnumValue") }, _ )
  659. | TInst( { cl_path = ([], "EnumValue") }, _ ) -> Some t_dynamic
  660. | _ -> None);
  661. let change_path path = (change_ns (fst path), change_clname (snd path)) in
  662. let path_s path = match path with
  663. | (ns,clname) -> path_s (change_ns ns, change_clname clname)
  664. in
  665. let cl_cl = get_cl (get_type gen (["java";"lang"],"Class")) in
  666. let rec real_type t =
  667. let t = gen.gfollow#run_f t in
  668. match t with
  669. | TAbstract ({ a_impl = Some _ } as a, pl) ->
  670. real_type (Codegen.Abstract.get_underlying_type a pl)
  671. | TInst( { cl_path = (["haxe"], "Int32") }, [] ) -> gen.gcon.basic.tint
  672. | TInst( { cl_path = (["haxe"], "Int64") }, [] ) -> ti64
  673. | TAbstract( { a_path = ([], "Class") }, p )
  674. | TAbstract( { a_path = ([], "Enum") }, p )
  675. | TInst( { cl_path = ([], "Class") }, p )
  676. | TInst( { cl_path = ([], "Enum") }, p ) -> TInst(cl_cl,[t_dynamic])
  677. | TEnum _
  678. | TInst _ -> t
  679. | TType({ t_path = ([], "Null") }, [t]) when is_java_basic_type t -> t_dynamic
  680. | TType({ t_path = ([], "Null") }, [t]) ->
  681. (match follow t with
  682. | TInst( { cl_kind = KTypeParameter _ }, []) -> t_dynamic
  683. | _ -> real_type t
  684. )
  685. | TType _ | TAbstract _ -> t
  686. | TAnon (anon) when (match !(anon.a_status) with | Statics _ | EnumStatics _ | AbstractStatics _ -> true | _ -> false) -> t
  687. | TFun _ -> TInst(fn_cl,[])
  688. | _ -> t_dynamic
  689. in
  690. let scope = ref PMap.empty in
  691. let imports = ref [] in
  692. let clear_scope () =
  693. scope := PMap.empty;
  694. imports := [];
  695. in
  696. let add_scope name =
  697. scope := PMap.add name () !scope
  698. in
  699. let add_import pos path =
  700. let name = snd path in
  701. let rec loop = function
  702. | (pack, n) :: _ when name = n ->
  703. if path <> (pack,n) then
  704. gen.gcon.error ("This expression cannot be generated because " ^ path_s path ^ " is shadowed by the current scope and ") pos
  705. | _ :: tl ->
  706. loop tl
  707. | [] ->
  708. (* add import *)
  709. imports := path :: !imports
  710. in
  711. loop !imports
  712. in
  713. let path_s_import pos path = match path with
  714. | [], name when PMap.mem name !scope ->
  715. gen.gcon.error ("This expression cannot be generated because " ^ name ^ " is shadowed by the current scope") pos;
  716. name
  717. | pack1 :: _, name when PMap.mem pack1 !scope -> (* exists in scope *)
  718. add_import pos path;
  719. (* check if name exists in scope *)
  720. if PMap.mem name !scope then
  721. gen.gcon.error ("This expression cannot be generated because " ^ pack1 ^ " and " ^ name ^ " are both shadowed by the current scope") pos;
  722. name
  723. | _ -> path_s path
  724. in
  725. let is_dynamic t = match real_type t with
  726. | TMono _ | TDynamic _ -> true
  727. | TAnon anon ->
  728. (match !(anon.a_status) with
  729. | EnumStatics _ | Statics _ -> false
  730. | _ -> true
  731. )
  732. | _ -> false
  733. in
  734. let rec t_s pos t =
  735. match real_type t with
  736. (* basic types *)
  737. | TEnum ({ e_path = ([], "Bool") }, [])
  738. | TAbstract ({ a_path = ([], "Bool") },[]) -> "boolean"
  739. | TEnum ({ e_path = ([], "Void") }, [])
  740. | TAbstract ({ a_path = ([], "Void") },[]) ->
  741. path_s_import pos (["java";"lang"], "Object")
  742. | TInst ({ cl_path = ([],"Float") },[])
  743. | TAbstract ({ a_path = ([],"Float") },[]) -> "double"
  744. | TInst ({ cl_path = ([],"Int") },[])
  745. | TAbstract ({ a_path = ([],"Int") },[]) -> "int"
  746. | TType ({ t_path = ["haxe";"_Int64"], "NativeInt64" },[])
  747. | TAbstract ({ a_path = ["haxe";"_Int64"], "NativeInt64" },[]) -> "long"
  748. | TType ({ t_path = ["java"],"Int8" },[])
  749. | TAbstract ({ a_path = ["java"],"Int8" },[]) -> "byte"
  750. | TType ({ t_path = ["java"],"Int16" },[])
  751. | TAbstract ({ a_path = ["java"],"Int16" },[]) -> "short"
  752. | TType ({ t_path = ["java"],"Char16" },[])
  753. | TAbstract ({ a_path = ["java"],"Char16" },[]) -> "char"
  754. | TType ({ t_path = [],"Single" },[])
  755. | TAbstract ({ a_path = [],"Single" },[]) -> "float"
  756. | TInst ({ cl_path = ["haxe"],"Int32" },[])
  757. | TAbstract ({ a_path = ["haxe"],"Int32" },[]) -> "int"
  758. | TInst ({ cl_path = ["haxe"],"Int64" },[])
  759. | TAbstract ({ a_path = ["haxe"],"Int64" },[]) -> "long"
  760. | TInst({ cl_path = (["java"], "NativeArray") }, [param]) ->
  761. let rec check_t_s t =
  762. match real_type t with
  763. | TInst({ cl_path = (["java"], "NativeArray") }, [param]) ->
  764. (check_t_s param) ^ "[]"
  765. | _ -> t_s pos (run_follow gen t)
  766. in
  767. (check_t_s param) ^ "[]"
  768. (* end of basic types *)
  769. | TInst ({ cl_kind = KTypeParameter _; cl_path=p }, []) -> snd p
  770. | TAbstract ({ a_path = [], "Dynamic" },[]) ->
  771. path_s_import pos (["java";"lang"], "Object")
  772. | TMono r -> (match !r with | None -> "java.lang.Object" | Some t -> t_s pos (run_follow gen t))
  773. | TInst ({ cl_path = [], "String" }, []) ->
  774. path_s_import pos (["java";"lang"], "String")
  775. | TAbstract ({ a_path = [], "Class" }, _) | TAbstract ({ a_path = [], "Enum" }, _)
  776. | TInst ({ cl_path = [], "Class" }, _) | TInst ({ cl_path = [], "Enum" }, _) -> assert false (* should have been converted earlier *)
  777. | TEnum (({e_path = p;} as e), params) -> (path_param_s pos (TEnumDecl e) p params)
  778. | TInst (({cl_path = p;} as cl), params) -> (path_param_s pos (TClassDecl cl) p params)
  779. | TType (({t_path = p;} as t), params) -> (path_param_s pos (TTypeDecl t) p params)
  780. | TAnon (anon) ->
  781. (match !(anon.a_status) with
  782. | Statics _ | EnumStatics _ | AbstractStatics _ ->
  783. path_s_import pos (["java";"lang"], "Class")
  784. | _ ->
  785. path_s_import pos (["java";"lang"], "Object"))
  786. | TDynamic _ ->
  787. path_s_import pos (["java";"lang"], "Object")
  788. | TAbstract(a,pl) when a.a_impl <> None ->
  789. t_s pos (Codegen.Abstract.get_underlying_type a pl)
  790. (* No Lazy type nor Function type made. That's because function types will be at this point be converted into other types *)
  791. | _ -> if !strict_mode then begin trace ("[ !TypeError " ^ (Type.s_type (Type.print_context()) t) ^ " ]"); assert false end else "[ !TypeError " ^ (Type.s_type (Type.print_context()) t) ^ " ]"
  792. and param_t_s pos t =
  793. match run_follow gen t with
  794. | TEnum ({ e_path = ([], "Bool") }, [])
  795. | TAbstract ({ a_path = ([], "Bool") },[]) ->
  796. path_s_import pos (["java";"lang"], "Boolean")
  797. | TInst ({ cl_path = ([],"Float") },[])
  798. | TAbstract ({ a_path = ([],"Float") },[]) ->
  799. path_s_import pos (["java";"lang"], "Double")
  800. | TInst ({ cl_path = ([],"Int") },[])
  801. | TAbstract ({ a_path = ([],"Int") },[]) ->
  802. path_s_import pos (["java";"lang"], "Integer")
  803. | TType ({ t_path = ["haxe";"_Int64"], "NativeInt64" },[])
  804. | TAbstract ({ a_path = ["haxe";"_Int64"], "NativeInt64" },[]) ->
  805. path_s_import pos (["java";"lang"], "Long")
  806. | TInst ({ cl_path = ["haxe"],"Int64" },[])
  807. | TAbstract ({ a_path = ["haxe"],"Int64" },[]) ->
  808. path_s_import pos (["java";"lang"], "Long")
  809. | TInst ({ cl_path = ["haxe"],"Int32" },[])
  810. | TAbstract ({ a_path = ["haxe"],"Int32" },[]) ->
  811. path_s_import pos (["java";"lang"], "Integer")
  812. | TType ({ t_path = ["java"],"Int8" },[])
  813. | TAbstract ({ a_path = ["java"],"Int8" },[]) ->
  814. path_s_import pos (["java";"lang"], "Byte")
  815. | TType ({ t_path = ["java"],"Int16" },[])
  816. | TAbstract ({ a_path = ["java"],"Int16" },[]) ->
  817. path_s_import pos (["java";"lang"], "Short")
  818. | TType ({ t_path = ["java"],"Char16" },[])
  819. | TAbstract ({ a_path = ["java"],"Char16" },[]) ->
  820. path_s_import pos (["java";"lang"], "Character")
  821. | TType ({ t_path = [],"Single" },[])
  822. | TAbstract ({ a_path = [],"Single" },[]) ->
  823. path_s_import pos (["java";"lang"], "Float")
  824. | TDynamic _ -> "?"
  825. | TInst (cl, params) -> t_s pos (TInst(cl, change_param_type (TClassDecl cl) params))
  826. | TType (cl, params) -> t_s pos (TType(cl, change_param_type (TTypeDecl cl) params))
  827. | TEnum (e, params) -> t_s pos (TEnum(e, change_param_type (TEnumDecl e) params))
  828. | _ -> t_s pos t
  829. and path_param_s pos md path params =
  830. match params with
  831. | [] -> path_s_import pos path
  832. | _ when has_tdynamic (change_param_type md params) -> path_s_import pos path
  833. | _ -> sprintf "%s<%s>" (path_s_import pos path) (String.concat ", " (List.map (fun t -> param_t_s pos t) (change_param_type md params)))
  834. in
  835. let rett_s pos t =
  836. match t with
  837. | TEnum ({e_path = ([], "Void")}, [])
  838. | TAbstract ({ a_path = ([], "Void") },[]) -> "void"
  839. | _ -> t_s pos t
  840. in
  841. let escape ichar b =
  842. match ichar with
  843. | 92 (* \ *) -> Buffer.add_string b "\\\\"
  844. | 39 (* ' *) -> Buffer.add_string b "\\\'"
  845. | 34 -> Buffer.add_string b "\\\""
  846. | 13 (* \r *) -> Buffer.add_string b "\\r"
  847. | 10 (* \n *) -> Buffer.add_string b "\\n"
  848. | 9 (* \t *) -> Buffer.add_string b "\\t"
  849. | c when c < 32 || c >= 127 -> Buffer.add_string b (Printf.sprintf "\\u%.4x" c)
  850. | c -> Buffer.add_char b (Char.chr c)
  851. in
  852. let escape s =
  853. let b = Buffer.create 0 in
  854. (try
  855. UTF8.validate s;
  856. UTF8.iter (fun c -> escape (UChar.code c) b) s
  857. with
  858. UTF8.Malformed_code ->
  859. String.iter (fun c -> escape (Char.code c) b) s
  860. );
  861. Buffer.contents b
  862. in
  863. let has_semicolon e =
  864. match e.eexpr with
  865. | TLocal { v_name = "__fallback__" }
  866. | TCall ({ eexpr = TLocal( { v_name = "__label__" } ) }, [ { eexpr = TConst(TInt _) } ] ) -> false
  867. | TBlock _ | TFor _ | TSwitch _ | TMatch _ | TTry _ | TIf _ -> false
  868. | TWhile (_,_,flag) when flag = Ast.NormalWhile -> false
  869. | _ -> true
  870. in
  871. let in_value = ref false in
  872. let rec md_s pos md =
  873. let md = follow_module (gen.gfollow#run_f) md in
  874. match md with
  875. | TClassDecl (cl) ->
  876. t_s pos (TInst(cl,[]))
  877. | TEnumDecl (e) ->
  878. t_s pos (TEnum(e,[]))
  879. | TTypeDecl t ->
  880. t_s pos (TType(t, []))
  881. | TAbstractDecl a ->
  882. t_s pos (TAbstract(a, []))
  883. in
  884. (*
  885. it seems that Java doesn't like when you create a new array with the type parameter defined
  886. so we'll just ignore all type parameters, and hope for the best!
  887. *)
  888. let rec transform_nativearray_t t = match real_type t with
  889. | TInst( ({ cl_path = (["java"], "NativeArray") } as narr), [t]) ->
  890. TInst(narr, [transform_nativearray_t t])
  891. | TInst(cl, params) -> TInst(cl, List.map (fun _ -> t_dynamic) params)
  892. | TEnum(e, params) -> TEnum(e, List.map (fun _ -> t_dynamic) params)
  893. | TType(t, params) -> TType(t, List.map (fun _ -> t_dynamic) params)
  894. | _ -> t
  895. in
  896. let expr_s w e =
  897. in_value := false;
  898. let rec expr_s w e =
  899. let was_in_value = !in_value in
  900. in_value := true;
  901. match e.eexpr with
  902. | TConst c ->
  903. (match c with
  904. | TInt i32 ->
  905. print w "%ld" i32;
  906. (match real_type e.etype with
  907. | TType( { t_path = (["haxe";"_Int64"], "NativeInt64") }, [] ) -> write w "L";
  908. | _ -> ()
  909. )
  910. | TFloat s ->
  911. write w s;
  912. (* fix for Int notation, which only fit in a Float *)
  913. (if not (String.contains s '.' || String.contains s 'e' || String.contains s 'E') then write w ".0");
  914. (match real_type e.etype with
  915. | TType( { t_path = ([], "Single") }, [] ) -> write w "f"
  916. | _ -> ()
  917. )
  918. | TString s -> print w "\"%s\"" (escape s)
  919. | TBool b -> write w (if b then "true" else "false")
  920. | TNull ->
  921. (match real_type e.etype with
  922. | TType( { t_path = (["haxe";"_Int64"], "NativeInt64") }, [] )
  923. | TInst( { cl_path = (["haxe"], "Int64") }, [] ) -> write w "0L"
  924. | TInst( { cl_path = (["haxe"], "Int32") }, [] )
  925. | TInst({ cl_path = ([], "Int") },[])
  926. | TAbstract ({ a_path = ([], "Int") },[]) -> expr_s w ({ e with eexpr = TConst(TInt Int32.zero) })
  927. | TInst({ cl_path = ([], "Float") },[])
  928. | TAbstract ({ a_path = ([], "Float") },[]) -> expr_s w ({ e with eexpr = TConst(TFloat "0.0") })
  929. | TEnum({ e_path = ([], "Bool") }, [])
  930. | TAbstract ({ a_path = ([], "Bool") },[]) -> write w "false"
  931. | TAbstract _ when like_int e.etype ->
  932. expr_s w { e with eexpr = TConst(TInt Int32.zero) }
  933. | TAbstract _ when like_float e.etype ->
  934. expr_s w { e with eexpr = TConst(TFloat "0.0") }
  935. | _ -> write w "null")
  936. | TThis -> write w "this"
  937. | TSuper -> write w "super")
  938. | TLocal { v_name = "__fallback__" } -> ()
  939. | TLocal { v_name = "__sbreak__" } -> write w "break"
  940. | TLocal { v_name = "__undefined__" } ->
  941. write w (t_s e.epos (TInst(runtime_cl, List.map (fun _ -> t_dynamic) runtime_cl.cl_types)));
  942. write w ".undefined";
  943. | TLocal var ->
  944. write_id w var.v_name
  945. | TField(_, FEnum(en,ef)) ->
  946. let s = ef.ef_name in
  947. print w "%s." (path_s_import e.epos en.e_path); write_field w s
  948. | TArray (e1, e2) ->
  949. expr_s w e1; write w "["; expr_s w e2; write w "]"
  950. | TBinop ((Ast.OpAssign as op), e1, e2)
  951. | TBinop ((Ast.OpAssignOp _ as op), e1, e2) ->
  952. expr_s w e1; write w ( " " ^ (Ast.s_binop op) ^ " " ); expr_s w e2
  953. | TBinop (op, e1, e2) ->
  954. write w "( ";
  955. expr_s w e1; write w ( " " ^ (Ast.s_binop op) ^ " " ); expr_s w e2;
  956. write w " )"
  957. | TField (e, FStatic(_, cf)) when Meta.has Meta.Native cf.cf_meta ->
  958. let rec loop meta = match meta with
  959. | (Meta.Native, [EConst (String s), _],_) :: _ ->
  960. expr_s w e; write w "."; write_field w s
  961. | _ :: tl -> loop tl
  962. | [] -> expr_s w e; write w "."; write_field w (cf.cf_name)
  963. in
  964. loop cf.cf_meta
  965. | TField (e, s) ->
  966. expr_s w e; write w "."; write_field w (field_name s)
  967. | TTypeExpr (TClassDecl { cl_path = (["haxe"], "Int32") }) ->
  968. write w (path_s_import e.epos (["haxe"], "Int32"))
  969. | TTypeExpr (TClassDecl { cl_path = (["haxe"], "Int64") }) ->
  970. write w (path_s_import e.epos (["haxe"], "Int64"))
  971. | TTypeExpr mt -> write w (md_s e.epos mt)
  972. | TParenthesis e ->
  973. write w "("; expr_s w e; write w ")"
  974. | TArrayDecl el when t_has_type_param_shallow false e.etype ->
  975. print w "( (%s) (new java.lang.Object[] " (t_s e.epos e.etype);
  976. write w "{";
  977. ignore (List.fold_left (fun acc e ->
  978. (if acc <> 0 then write w ", ");
  979. expr_s w e;
  980. acc + 1
  981. ) 0 el);
  982. write w "}) )"
  983. | TArrayDecl el ->
  984. print w "new %s" (param_t_s e.epos (transform_nativearray_t e.etype));
  985. let is_double = match follow e.etype with
  986. | TInst(_,[ t ]) -> if like_float t && not (like_int t) then Some t else None
  987. | _ -> None
  988. in
  989. write w "{";
  990. ignore (List.fold_left (fun acc e ->
  991. (if acc <> 0 then write w ", ");
  992. (* this is a hack so we are able to convert ints to boxed Double / Float when needed *)
  993. let e = if is_some is_double then mk_cast (get is_double) e else e in
  994. expr_s w e;
  995. acc + 1
  996. ) 0 el);
  997. write w "}"
  998. | TCall( ( { eexpr = TField(_, FStatic({ cl_path = ([], "String") }, { cf_name = "fromCharCode" })) } ), [cc] ) ->
  999. write w "Character.toString((char) ";
  1000. expr_s w cc;
  1001. write w ")"
  1002. | TCall ({ eexpr = TLocal( { v_name = "__is__" } ) }, [ expr; { eexpr = TTypeExpr(md) } ] ) ->
  1003. write w "( ";
  1004. expr_s w expr;
  1005. write w " instanceof ";
  1006. write w (md_s e.epos md);
  1007. write w " )"
  1008. | TCall ({ eexpr = TLocal( { v_name = "__java__" } ) }, [ { eexpr = TConst(TString(s)) } ] ) ->
  1009. write w s
  1010. | TCall ({ eexpr = TLocal( { v_name = "__lock__" } ) }, [ eobj; eblock ] ) ->
  1011. write w "synchronized(";
  1012. expr_s w eobj;
  1013. write w ")";
  1014. expr_s w (mk_block eblock)
  1015. | TCall ({ eexpr = TLocal( { v_name = "__goto__" } ) }, [ { eexpr = TConst(TInt v) } ] ) ->
  1016. print w "break label%ld" v
  1017. | TCall ({ eexpr = TLocal( { v_name = "__label__" } ) }, [ { eexpr = TConst(TInt v) } ] ) ->
  1018. print w "label%ld:" v
  1019. | TCall ({ eexpr = TLocal( { v_name = "__typeof__" } ) }, [ { eexpr = TTypeExpr md } as expr ] ) ->
  1020. expr_s w expr;
  1021. write w ".class"
  1022. | TCall (e, el) ->
  1023. let rec extract_tparams params el =
  1024. match el with
  1025. | ({ eexpr = TLocal({ v_name = "$type_param" }) } as tp) :: tl ->
  1026. extract_tparams (tp.etype :: params) tl
  1027. | _ -> (params, el)
  1028. in
  1029. let params, el = extract_tparams [] el in
  1030. expr_s w e;
  1031. (*(match params with
  1032. | [] -> ()
  1033. | params ->
  1034. let md = match e.eexpr with
  1035. | TField(ef, _) -> t_to_md (run_follow gen ef.etype)
  1036. | _ -> assert false
  1037. in
  1038. write w "<";
  1039. ignore (List.fold_left (fun acc t ->
  1040. (if acc <> 0 then write w ", ");
  1041. write w (param_t_s (change_param_type md t));
  1042. acc + 1
  1043. ) 0 params);
  1044. write w ">"
  1045. );*)
  1046. write w "(";
  1047. ignore (List.fold_left (fun acc e ->
  1048. (if acc <> 0 then write w ", ");
  1049. expr_s w e;
  1050. acc + 1
  1051. ) 0 el);
  1052. write w ")"
  1053. | TNew (({ cl_path = (["java"], "NativeArray") } as cl), params, [ size ]) ->
  1054. let rec check_t_s t times =
  1055. match real_type t with
  1056. | TInst({ cl_path = (["java"], "NativeArray") }, [param]) ->
  1057. (check_t_s param (times+1))
  1058. | _ ->
  1059. print w "new %s[" (t_s e.epos (transform_nativearray_t t));
  1060. expr_s w size;
  1061. print w "]";
  1062. let rec loop i =
  1063. if i <= 0 then () else (write w "[]"; loop (i-1))
  1064. in
  1065. loop (times - 1)
  1066. in
  1067. check_t_s (TInst(cl, params)) 0
  1068. | TNew ({ cl_path = ([], "String") } as cl, [], el) ->
  1069. write w "new ";
  1070. write w (t_s e.epos (TInst(cl, [])));
  1071. write w "(";
  1072. ignore (List.fold_left (fun acc e ->
  1073. (if acc <> 0 then write w ", ");
  1074. expr_s w e;
  1075. acc + 1
  1076. ) 0 el);
  1077. write w ")"
  1078. | TNew (cl, params, el) ->
  1079. write w "new ";
  1080. write w (path_param_s e.epos (TClassDecl cl) cl.cl_path params);
  1081. write w "(";
  1082. ignore (List.fold_left (fun acc e ->
  1083. (if acc <> 0 then write w ", ");
  1084. expr_s w e;
  1085. acc + 1
  1086. ) 0 el);
  1087. write w ")"
  1088. | TUnop ((Ast.Increment as op), flag, e)
  1089. | TUnop ((Ast.Decrement as op), flag, e) ->
  1090. (match flag with
  1091. | Ast.Prefix -> write w ( " " ^ (Ast.s_unop op) ^ " " ); expr_s w e
  1092. | Ast.Postfix -> expr_s w e; write w (Ast.s_unop op))
  1093. | TUnop (op, flag, e) ->
  1094. (match flag with
  1095. | Ast.Prefix -> write w ( " " ^ (Ast.s_unop op) ^ " (" ); expr_s w e; write w ") "
  1096. | Ast.Postfix -> write w "("; expr_s w e; write w (") " ^ Ast.s_unop op))
  1097. | TVars (v_eop_l) ->
  1098. ignore (List.fold_left (fun acc (var, eopt) ->
  1099. (if acc <> 0 then write w "; ");
  1100. print w "%s " (t_s e.epos var.v_type);
  1101. write_id w var.v_name;
  1102. (match eopt with
  1103. | None ->
  1104. write w " = ";
  1105. expr_s w (null var.v_type e.epos)
  1106. | Some e ->
  1107. write w " = ";
  1108. expr_s w e
  1109. );
  1110. acc + 1
  1111. ) 0 v_eop_l);
  1112. | TBlock [e] when was_in_value ->
  1113. expr_s w e
  1114. | TBlock el ->
  1115. begin_block w;
  1116. (*let last_line = ref (-1) in
  1117. let line_directive p =
  1118. let cur_line = Lexer.get_error_line p in
  1119. let is_relative_path = (String.sub p.pfile 0 1) = "." in
  1120. let file = if is_relative_path then "../" ^ p.pfile else p.pfile in
  1121. if cur_line <> ((!last_line)+1) then begin print w "//#line %d \"%s\"" cur_line (Ast.s_escape file); newline w end;
  1122. last_line := cur_line in*)
  1123. List.iter (fun e ->
  1124. (*line_directive e.epos;*)
  1125. in_value := false;
  1126. expr_s w e;
  1127. (if has_semicolon e then write w ";");
  1128. newline w
  1129. ) el;
  1130. end_block w
  1131. | TIf (econd, e1, Some(eelse)) when was_in_value ->
  1132. write w "( ";
  1133. expr_s w (mk_paren econd);
  1134. write w " ? ";
  1135. expr_s w (mk_paren e1);
  1136. write w " : ";
  1137. expr_s w (mk_paren eelse);
  1138. write w " )";
  1139. | TIf (econd, e1, eelse) ->
  1140. write w "if ";
  1141. expr_s w (mk_paren econd);
  1142. write w " ";
  1143. in_value := false;
  1144. expr_s w (mk_block e1);
  1145. (match eelse with
  1146. | None -> ()
  1147. | Some e ->
  1148. write w " else ";
  1149. in_value := false;
  1150. expr_s w (mk_block e)
  1151. )
  1152. | TWhile (econd, eblock, flag) ->
  1153. (match flag with
  1154. | Ast.NormalWhile ->
  1155. write w "while ";
  1156. expr_s w (mk_paren econd);
  1157. write w "";
  1158. in_value := false;
  1159. expr_s w (mk_block eblock)
  1160. | Ast.DoWhile ->
  1161. write w "do ";
  1162. in_value := false;
  1163. expr_s w (mk_block eblock);
  1164. write w "while ";
  1165. in_value := true;
  1166. expr_s w (mk_paren econd);
  1167. )
  1168. | TSwitch (econd, ele_l, default) ->
  1169. write w "switch ";
  1170. expr_s w (mk_paren econd);
  1171. begin_block w;
  1172. List.iter (fun (el, e) ->
  1173. List.iter (fun e ->
  1174. write w "case ";
  1175. in_value := true;
  1176. expr_s w e;
  1177. write w ":";
  1178. ) el;
  1179. newline w;
  1180. in_value := false;
  1181. expr_s w (mk_block e);
  1182. newline w;
  1183. newline w
  1184. ) ele_l;
  1185. if is_some default then begin
  1186. write w "default:";
  1187. newline w;
  1188. in_value := false;
  1189. expr_s w (get default);
  1190. newline w;
  1191. end;
  1192. end_block w
  1193. | TTry (tryexpr, ve_l) ->
  1194. write w "try ";
  1195. in_value := false;
  1196. expr_s w (mk_block tryexpr);
  1197. let pos = e.epos in
  1198. List.iter (fun (var, e) ->
  1199. print w "catch (%s %s)" (t_s pos var.v_type) (var.v_name);
  1200. in_value := false;
  1201. expr_s w (mk_block e);
  1202. newline w
  1203. ) ve_l
  1204. | TReturn eopt ->
  1205. write w "return ";
  1206. if is_some eopt then expr_s w (get eopt)
  1207. | TBreak -> write w "break"
  1208. | TContinue -> write w "continue"
  1209. | TThrow e ->
  1210. write w "throw ";
  1211. expr_s w e
  1212. | TCast (e1,md_t) ->
  1213. ((*match gen.gfollow#run_f e.etype with
  1214. | TType({ t_path = ([], "UInt") }, []) ->
  1215. write w "( unchecked ((uint) ";
  1216. expr_s w e1;
  1217. write w ") )"
  1218. | _ ->*)
  1219. (* FIXME I'm ignoring module type *)
  1220. print w "((%s) (" (t_s e.epos e.etype);
  1221. expr_s w e1;
  1222. write w ") )"
  1223. )
  1224. | TFor (_,_,content) ->
  1225. write w "[ for not supported ";
  1226. expr_s w content;
  1227. write w " ]";
  1228. if !strict_mode then assert false
  1229. | TObjectDecl _ -> write w "[ obj decl not supported ]"; if !strict_mode then assert false
  1230. | TFunction _ -> write w "[ func decl not supported ]"; if !strict_mode then assert false
  1231. | TMatch _ -> write w "[ match not supported ]"; if !strict_mode then assert false
  1232. in
  1233. expr_s w e
  1234. in
  1235. let get_string_params cl_types =
  1236. match cl_types with
  1237. | [] ->
  1238. ("","")
  1239. | _ ->
  1240. let params = sprintf "<%s>" (String.concat ", " (List.map (fun (_, tcl) -> match follow tcl with | TInst(cl, _) -> snd cl.cl_path | _ -> assert false) cl_types)) in
  1241. let params_extends = List.fold_left (fun acc (name, t) ->
  1242. match run_follow gen t with
  1243. | TInst (cl, p) ->
  1244. (match cl.cl_implements with
  1245. | [] -> acc
  1246. | _ -> acc) (* TODO
  1247. | _ -> (sprintf " where %s : %s" name (String.concat ", " (List.map (fun (cl,p) -> path_param_s (TClassDecl cl) cl.cl_path p) cl.cl_implements))) :: acc ) *)
  1248. | _ -> trace (t_s Ast.null_pos t); assert false (* FIXME it seems that a cl_types will never be anything other than cl.cl_types. I'll take the risk and fail if not, just to see if that confirms *)
  1249. ) [] cl_types in
  1250. (params, String.concat " " params_extends)
  1251. in
  1252. let rec gen_class_field w ?(is_overload=false) is_static cl is_final cf =
  1253. let is_interface = cl.cl_interface in
  1254. let name, is_new, is_explicit_iface = match cf.cf_name with
  1255. | "new" -> snd cl.cl_path, true, false
  1256. | name when String.contains name '.' ->
  1257. let fn_name, path = parse_explicit_iface name in
  1258. (path_s path) ^ "." ^ fn_name, false, true
  1259. | name -> name, false, false
  1260. in
  1261. (match cf.cf_kind with
  1262. | Var _
  1263. | Method (MethDynamic) when not (Type.is_extern_field cf) ->
  1264. (if is_overload || List.exists (fun cf -> cf.cf_expr <> None) cf.cf_overloads then
  1265. gen.gcon.error "Only normal (non-dynamic) methods can be overloaded" cf.cf_pos);
  1266. if not is_interface then begin
  1267. let access, modifiers = get_fun_modifiers cf.cf_meta "public" [] in
  1268. print w "%s %s%s %s %s" access (if is_static then "static " else "") (String.concat " " modifiers) (t_s cf.cf_pos (run_follow gen cf.cf_type)) (change_field name);
  1269. (match cf.cf_expr with
  1270. | Some e ->
  1271. write w " = ";
  1272. expr_s w e;
  1273. write w ";"
  1274. | None -> write w ";"
  1275. )
  1276. end (* TODO see how (get,set) variable handle when they are interfaces *)
  1277. | Method _ when Type.is_extern_field cf ->
  1278. List.iter (fun cf -> if cl.cl_interface || cf.cf_expr <> None then
  1279. gen_class_field w ~is_overload:true is_static cl (Meta.has Meta.Final cf.cf_meta) cf
  1280. ) cf.cf_overloads
  1281. | Var _ | Method MethDynamic -> ()
  1282. | Method mkind ->
  1283. List.iter (fun cf ->
  1284. if cl.cl_interface || cf.cf_expr <> None then
  1285. gen_class_field w ~is_overload:true is_static cl (Meta.has Meta.Final cf.cf_meta) cf
  1286. ) cf.cf_overloads;
  1287. let is_virtual = is_new || (not is_final && match mkind with | MethInline -> false | _ when not is_new -> true | _ -> false) in
  1288. let is_override = match cf.cf_name with
  1289. | "equals" when not is_static ->
  1290. (match cf.cf_type with
  1291. | TFun([_,_,t], ret) ->
  1292. (match (real_type t, real_type ret) with
  1293. | TDynamic _, TEnum( { e_path = ([], "Bool") }, [])
  1294. | TDynamic _, TAbstract ({ a_path = ([], "Bool") },[])
  1295. | TAnon _, TEnum( { e_path = ([], "Bool") }, [])
  1296. | TAnon _, TAbstract ({ a_path = ([], "Bool") },[]) -> true
  1297. | _ -> List.memq cf cl.cl_overrides
  1298. )
  1299. | _ -> List.memq cf cl.cl_overrides)
  1300. | "toString" when not is_static ->
  1301. (match cf.cf_type with
  1302. | TFun([], ret) ->
  1303. (match real_type ret with
  1304. | TInst( { cl_path = ([], "String") }, []) -> true
  1305. | _ -> gen.gcon.error "A toString() function should return a String!" cf.cf_pos; false
  1306. )
  1307. | _ -> List.memq cf cl.cl_overrides
  1308. )
  1309. | "hashCode" when not is_static ->
  1310. (match cf.cf_type with
  1311. | TFun([], ret) ->
  1312. (match real_type ret with
  1313. | TInst( { cl_path = ([], "Int") }, [])
  1314. | TAbstract ({ a_path = ([], "Int") },[]) ->
  1315. true
  1316. | _ -> gen.gcon.error "A hashCode() function should return an Int!" cf.cf_pos; false
  1317. )
  1318. | _ -> List.memq cf cl.cl_overrides
  1319. )
  1320. | _ -> List.memq cf cl.cl_overrides
  1321. in
  1322. let visibility = if is_interface then "" else "public" in
  1323. let visibility, modifiers = get_fun_modifiers cf.cf_meta visibility [] in
  1324. let visibility, is_virtual = if is_explicit_iface then "",false else visibility, is_virtual in
  1325. let v_n = if is_static then "static " else if is_override && not is_interface then "" else if not is_virtual then "final " else "" in
  1326. let cf_type = if is_override && not is_overload && not (Meta.has Meta.Overload cf.cf_meta) then match field_access gen (TInst(cl, List.map snd cl.cl_types)) cf.cf_name with | FClassField(_,_,_,_,_,actual_t) -> actual_t | _ -> assert false else cf.cf_type in
  1327. let params = List.map snd cl.cl_types in
  1328. let ret_type, args = match follow cf_type, follow cf.cf_type with
  1329. | TFun (strbtl, t), TFun(rargs, _) ->
  1330. (apply_params cl.cl_types params (real_type t), List.map2 (fun(_,_,t) (n,o,_) -> (n,o,apply_params cl.cl_types params (real_type t))) strbtl rargs)
  1331. | _ -> assert false
  1332. in
  1333. (if is_override && not is_interface then write w "@Override ");
  1334. (* public static void funcName *)
  1335. let params, _ = get_string_params cf.cf_params in
  1336. print w "%s %s%s %s %s %s" (visibility) v_n (String.concat " " modifiers) params (if is_new then "" else rett_s cf.cf_pos (run_follow gen ret_type)) (change_field name);
  1337. (* <T>(string arg1, object arg2) with T : object *)
  1338. (match cf.cf_expr with
  1339. | Some { eexpr = TFunction tf } ->
  1340. print w "(%s)" (String.concat ", " (List.map (fun (var,_) -> sprintf "%s %s" (t_s cf.cf_pos (run_follow gen var.v_type)) (change_id var.v_name)) tf.tf_args))
  1341. | _ ->
  1342. print w "(%s)" (String.concat ", " (List.map (fun (name, _, t) -> sprintf "%s %s" (t_s cf.cf_pos (run_follow gen t)) (change_id name)) args))
  1343. );
  1344. if is_interface then
  1345. write w ";"
  1346. else begin
  1347. let rec loop meta =
  1348. match meta with
  1349. | [] ->
  1350. let expr = match cf.cf_expr with
  1351. | None -> mk (TBlock([])) t_dynamic Ast.null_pos
  1352. | Some s ->
  1353. match s.eexpr with
  1354. | TFunction tf ->
  1355. mk_block (tf.tf_expr)
  1356. | _ -> assert false (* FIXME *)
  1357. in
  1358. (if is_new then begin
  1359. (*let rec get_super_call el =
  1360. match el with
  1361. | ( { eexpr = TCall( { eexpr = TConst(TSuper) }, _) } as call) :: rest ->
  1362. Some call, rest
  1363. | ( { eexpr = TBlock(bl) } as block ) :: rest ->
  1364. let ret, mapped = get_super_call bl in
  1365. ret, ( { block with eexpr = TBlock(mapped) } :: rest )
  1366. | _ ->
  1367. None, el
  1368. in*)
  1369. expr_s w expr
  1370. end else begin
  1371. expr_s w expr;
  1372. end)
  1373. | (Meta.Throws, [Ast.EConst (Ast.String t), _], _) :: tl ->
  1374. print w " throws %s" t;
  1375. loop tl
  1376. | (Meta.FunctionCode, [Ast.EConst (Ast.String contents),_],_) :: tl ->
  1377. begin_block w;
  1378. write w contents;
  1379. end_block w
  1380. | _ :: tl -> loop tl
  1381. in
  1382. loop cf.cf_meta
  1383. end);
  1384. newline w;
  1385. newline w
  1386. in
  1387. let gen_class w cl =
  1388. let should_close = match change_ns (fst cl.cl_path) with
  1389. | [] -> false
  1390. | ns ->
  1391. print w "package %s;" (String.concat "." (change_ns ns));
  1392. newline w;
  1393. false
  1394. in
  1395. let rec loop_meta meta acc =
  1396. match meta with
  1397. | (Meta.SuppressWarnings, [Ast.EConst (Ast.String w),_],_) :: meta -> loop_meta meta (w :: acc)
  1398. | _ :: meta -> loop_meta meta acc
  1399. | _ -> acc
  1400. in
  1401. let suppress_warnings = loop_meta cl.cl_meta [ "rawtypes"; "unchecked" ] in
  1402. write w "import haxe.root.*;";
  1403. newline w;
  1404. let w_header = w in
  1405. let w = new_source_writer () in
  1406. clear_scope();
  1407. (* add all haxe.root.* to imports *)
  1408. List.iter (function
  1409. | TClassDecl { cl_path = ([],c) } ->
  1410. imports := ([],c) :: !imports
  1411. | TEnumDecl { e_path = ([],c) } ->
  1412. imports := ([],c) :: !imports
  1413. | TAbstractDecl { a_path = ([],c) } ->
  1414. imports := ([],c) :: !imports
  1415. | _ -> ()
  1416. ) gen.gcon.types;
  1417. newline w;
  1418. write w "@SuppressWarnings(value={";
  1419. let first = ref true in
  1420. List.iter (fun s ->
  1421. (if !first then first := false else write w ", ");
  1422. print w "\"%s\"" (escape s)
  1423. ) suppress_warnings;
  1424. write w "})";
  1425. newline w;
  1426. let clt, access, modifiers = get_class_modifiers cl.cl_meta (if cl.cl_interface then "interface" else "class") "public" [] in
  1427. let is_final = Meta.has Meta.Final cl.cl_meta in
  1428. print w "%s %s %s %s" access (String.concat " " modifiers) clt (change_clname (snd cl.cl_path));
  1429. (* type parameters *)
  1430. let params, _ = get_string_params cl.cl_types in
  1431. let cl_p_to_string (c,p) = path_param_s cl.cl_pos (TClassDecl c) c.cl_path p in
  1432. print w "%s" params;
  1433. (if is_some cl.cl_super then print w " extends %s" (cl_p_to_string (get cl.cl_super)));
  1434. (match cl.cl_implements with
  1435. | [] -> ()
  1436. | _ -> print w " %s %s" (if cl.cl_interface then "extends" else "implements") (String.concat ", " (List.map cl_p_to_string cl.cl_implements))
  1437. );
  1438. (* class head ok: *)
  1439. (* public class Test<A> : X, Y, Z where A : Y *)
  1440. begin_block w;
  1441. (* our constructor is expected to be a normal "new" function *
  1442. if !strict_mode && is_some cl.cl_constructor then assert false;*)
  1443. let rec loop cl =
  1444. List.iter (fun cf -> add_scope cf.cf_name) cl.cl_ordered_fields;
  1445. List.iter (fun cf -> add_scope cf.cf_name) cl.cl_ordered_statics;
  1446. match cl.cl_super with
  1447. | Some(c,_) -> loop c
  1448. | None -> ()
  1449. in
  1450. loop cl;
  1451. let rec loop meta =
  1452. match meta with
  1453. | [] -> ()
  1454. | (Meta.ClassCode, [Ast.EConst (Ast.String contents),_],_) :: tl ->
  1455. write w contents
  1456. | _ :: tl -> loop tl
  1457. in
  1458. loop cl.cl_meta;
  1459. (match gen.gcon.main_class with
  1460. | Some path when path = cl.cl_path ->
  1461. write w "public static void main(String[] args)";
  1462. begin_block w;
  1463. (try
  1464. let t = Hashtbl.find gen.gtypes ([], "Sys") in
  1465. match t with
  1466. | TClassDecl(cl) when PMap.mem "_args" cl.cl_statics ->
  1467. write w "Sys._args = args;"; newline w
  1468. | _ -> ()
  1469. with | Not_found -> ()
  1470. );
  1471. write w "main();";
  1472. end_block w
  1473. | _ -> ()
  1474. );
  1475. (match cl.cl_init with
  1476. | None -> ()
  1477. | Some init ->
  1478. write w "static ";
  1479. expr_s w (mk_block init));
  1480. (if is_some cl.cl_constructor then gen_class_field w false cl is_final (get cl.cl_constructor));
  1481. (if not cl.cl_interface then
  1482. List.iter (gen_class_field w true cl is_final) cl.cl_ordered_statics);
  1483. List.iter (gen_class_field w false cl is_final) cl.cl_ordered_fields;
  1484. end_block w;
  1485. if should_close then end_block w;
  1486. (* add imports *)
  1487. List.iter (function
  1488. | ["haxe";"root"], _ | [], _ -> ()
  1489. | path ->
  1490. write w_header "import ";
  1491. write w_header (path_s path);
  1492. write w_header ";\n"
  1493. ) !imports;
  1494. add_writer w w_header
  1495. in
  1496. let gen_enum w e =
  1497. let should_close = match change_ns (fst e.e_path) with
  1498. | [] -> false
  1499. | ns ->
  1500. print w "package %s;" (String.concat "." (change_ns ns));
  1501. newline w;
  1502. false
  1503. in
  1504. print w "public enum %s" (change_clname (snd e.e_path));
  1505. begin_block w;
  1506. write w (String.concat ", " (List.map (change_id) e.e_names));
  1507. end_block w;
  1508. if should_close then end_block w
  1509. in
  1510. let module_type_gen w md_tp =
  1511. match md_tp with
  1512. | TClassDecl cl ->
  1513. if not cl.cl_extern then begin
  1514. gen_class w cl;
  1515. newline w;
  1516. newline w
  1517. end;
  1518. (not cl.cl_extern)
  1519. | TEnumDecl e ->
  1520. if not e.e_extern then begin
  1521. gen_enum w e;
  1522. newline w;
  1523. newline w
  1524. end;
  1525. (not e.e_extern)
  1526. | TTypeDecl e ->
  1527. false
  1528. | TAbstractDecl a ->
  1529. false
  1530. in
  1531. let module_gen w md =
  1532. module_type_gen w md
  1533. in
  1534. (* generate source code *)
  1535. init_ctx gen;
  1536. Hashtbl.add gen.gspecial_vars "__label__" true;
  1537. Hashtbl.add gen.gspecial_vars "__goto__" true;
  1538. Hashtbl.add gen.gspecial_vars "__is__" true;
  1539. Hashtbl.add gen.gspecial_vars "__typeof__" true;
  1540. Hashtbl.add gen.gspecial_vars "__java__" true;
  1541. Hashtbl.add gen.gspecial_vars "__lock__" true;
  1542. gen.greal_type <- real_type;
  1543. gen.greal_type_param <- change_param_type;
  1544. SetHXGen.run_filter gen SetHXGen.default_hxgen_func;
  1545. let closure_t = ClosuresToClass.DoubleAndDynamicClosureImpl.get_ctx gen 6 in
  1546. (*let closure_t = ClosuresToClass.create gen 10 float_cl
  1547. (fun l -> l)
  1548. (fun l -> l)
  1549. (fun args -> args)
  1550. (fun args -> [])
  1551. in
  1552. ClosuresToClass.configure gen (ClosuresToClass.default_implementation closure_t (fun e _ _ -> e));
  1553. StubClosureImpl.configure gen (StubClosureImpl.default_implementation gen float_cl 10 (fun e _ _ -> e));*)
  1554. AbstractImplementationFix.configure gen;
  1555. IteratorsInterface.configure gen (fun e -> e);
  1556. ClosuresToClass.configure gen (ClosuresToClass.default_implementation closure_t (get_cl (get_type gen (["haxe";"lang"],"Function")) ));
  1557. EnumToClass.configure gen (None) false true (get_cl (get_type gen (["haxe";"lang"],"Enum")) ) false true;
  1558. InterfaceVarsDeleteModf.configure gen;
  1559. let dynamic_object = (get_cl (get_type gen (["haxe";"lang"],"DynamicObject")) ) in
  1560. let object_iface = get_cl (get_type gen (["haxe";"lang"],"IHxObject")) in
  1561. (*fixme: THIS IS A HACK. take this off *)
  1562. let empty_e = match (get_type gen (["haxe";"lang"], "EmptyObject")) with | TEnumDecl e -> e | _ -> assert false in
  1563. (*OverloadingCtor.set_new_create_empty gen ({eexpr=TEnumField(empty_e, "EMPTY"); etype=TEnum(empty_e,[]); epos=null_pos;});*)
  1564. let empty_expr = { eexpr = (TTypeExpr (TEnumDecl empty_e)); etype = (TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics empty_e) }); epos = null_pos } in
  1565. let empty_ef =
  1566. try
  1567. PMap.find "EMPTY" empty_e.e_constrs
  1568. with Not_found -> gen.gcon.error "Required enum field EMPTY was not found" empty_e.e_pos; assert false
  1569. in
  1570. OverloadingConstructor.configure gen (TEnum(empty_e, [])) ({ eexpr=TField(empty_expr, FEnum(empty_e, empty_ef)); etype=TEnum(empty_e,[]); epos=null_pos; }) false;
  1571. let rcf_static_find = mk_static_field_access_infer (get_cl (get_type gen (["haxe";"lang"], "FieldLookup"))) "findHash" Ast.null_pos [] in
  1572. (*let rcf_static_lookup = mk_static_field_access_infer (get_cl (get_type gen (["haxe";"lang"], "FieldLookup"))) "lookupHash" Ast.null_pos [] in*)
  1573. let can_be_float t = like_float (real_type t) in
  1574. let rcf_on_getset_field main_expr field_expr field may_hash may_set is_unsafe =
  1575. let is_float = can_be_float (if is_none may_set then main_expr.etype else (get may_set).etype) in
  1576. let fn_name = if is_some may_set then "setField" else "getField" in
  1577. let fn_name = if is_float then fn_name ^ "_f" else fn_name in
  1578. let pos = field_expr.epos in
  1579. let is_unsafe = { eexpr = TConst(TBool is_unsafe); etype = basic.tbool; epos = pos } in
  1580. let should_cast = match main_expr.etype with | TInst({ cl_path = ([], "Float") }, []) -> false | _ -> true in
  1581. let infer = mk_static_field_access_infer runtime_cl fn_name field_expr.epos [] in
  1582. let first_args =
  1583. [ field_expr; { eexpr = TConst(TString field); etype = basic.tstring; epos = pos } ]
  1584. @ if is_some may_hash then [ { eexpr = TConst(TInt (get may_hash)); etype = basic.tint; epos = pos } ] else []
  1585. in
  1586. let args = first_args @ match is_float, may_set with
  1587. | true, Some(set) ->
  1588. [ if should_cast then mk_cast basic.tfloat set else set ]
  1589. | false, Some(set) ->
  1590. [ set ]
  1591. | _ ->
  1592. [ is_unsafe ]
  1593. in
  1594. let call = { main_expr with eexpr = TCall(infer,args) } in
  1595. let call = if is_float && should_cast then mk_cast main_expr.etype call else call in
  1596. call
  1597. in
  1598. let rcf_on_call_field ecall field_expr field may_hash args =
  1599. let infer = mk_static_field_access_infer runtime_cl "callField" field_expr.epos [] in
  1600. let hash_arg = match may_hash with
  1601. | None -> []
  1602. | Some h -> [ { eexpr = TConst(TInt h); etype = basic.tint; epos = field_expr.epos } ]
  1603. in
  1604. let arr_call = if args <> [] then
  1605. { eexpr = TArrayDecl args; etype = basic.tarray t_dynamic; epos = ecall.epos }
  1606. else
  1607. null (basic.tarray t_dynamic) ecall.epos
  1608. in
  1609. let call_args =
  1610. [field_expr; { field_expr with eexpr = TConst(TString field); etype = basic.tstring } ]
  1611. @ hash_arg
  1612. @ [ arr_call ]
  1613. in
  1614. mk_cast ecall.etype { ecall with eexpr = TCall(infer, call_args); etype = t_dynamic }
  1615. in
  1616. let rcf_ctx = ReflectionCFs.new_ctx gen closure_t object_iface false rcf_on_getset_field rcf_on_call_field (fun hash hash_array ->
  1617. { hash with eexpr = TCall(rcf_static_find, [hash; hash_array]); etype=basic.tint }
  1618. ) (fun hash -> hash ) false in
  1619. ReflectionCFs.UniversalBaseClass.default_config gen (get_cl (get_type gen (["haxe";"lang"],"HxObject")) ) object_iface dynamic_object;
  1620. ReflectionCFs.configure_dynamic_field_access rcf_ctx false;
  1621. (* let closure_func = ReflectionCFs.implement_closure_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"],"Closure")) ) in *)
  1622. let closure_cl = get_cl (get_type gen (["haxe";"lang"],"Closure")) in
  1623. let closure_func = ReflectionCFs.get_closure_func rcf_ctx closure_cl in
  1624. ReflectionCFs.implement_varargs_cl rcf_ctx ( get_cl (get_type gen (["haxe";"lang"], "VarArgsBase")) );
  1625. let slow_invoke = mk_static_field_access_infer (runtime_cl) "slowCallField" Ast.null_pos [] in
  1626. ReflectionCFs.configure rcf_ctx ~slow_invoke:(fun ethis efield eargs -> {
  1627. eexpr = TCall(slow_invoke, [ethis; efield; eargs]);
  1628. etype = t_dynamic;
  1629. epos = ethis.epos;
  1630. } );
  1631. let objdecl_fn = ReflectionCFs.implement_dynamic_object_ctor rcf_ctx dynamic_object in
  1632. ObjectDeclMap.configure gen (ObjectDeclMap.traverse gen objdecl_fn);
  1633. InitFunction.configure gen true;
  1634. TArrayTransform.configure gen (TArrayTransform.default_implementation gen (
  1635. fun e ->
  1636. match e.eexpr with
  1637. | TArray(e1, e2) ->
  1638. ( match run_follow gen e1.etype with
  1639. | TInst({ cl_path = (["java"], "NativeArray") }, _) -> false
  1640. | _ -> true )
  1641. | _ -> assert false
  1642. ) "__get" "__set" );
  1643. let field_is_dynamic t field =
  1644. match field_access gen (gen.greal_type t) field with
  1645. | FClassField (cl,p,_,_,_,t) ->
  1646. is_dynamic (apply_params cl.cl_types p t)
  1647. | FEnumField _ -> false
  1648. | _ -> true
  1649. in
  1650. let is_type_param e = match follow e with
  1651. | TInst( { cl_kind = KTypeParameter _ },[]) -> true
  1652. | _ -> false
  1653. in
  1654. let is_dynamic_expr e = is_dynamic e.etype || match e.eexpr with
  1655. | TField(tf, f) -> field_is_dynamic tf.etype (field_name f)
  1656. | _ -> false
  1657. in
  1658. let may_nullable t = match gen.gfollow#run_f t with
  1659. | TType({ t_path = ([], "Null") }, [t]) ->
  1660. (match follow t with
  1661. | TInst({ cl_path = ([], "String") }, [])
  1662. | TInst({ cl_path = ([], "Float") }, [])
  1663. | TAbstract ({ a_path = ([], "Float") },[])
  1664. | TInst({ cl_path = (["haxe"], "Int32")}, [] )
  1665. | TInst({ cl_path = (["haxe"], "Int64")}, [] )
  1666. | TInst({ cl_path = ([], "Int") }, [])
  1667. | TAbstract ({ a_path = ([], "Int") },[])
  1668. | TEnum({ e_path = ([], "Bool") }, [])
  1669. | TAbstract ({ a_path = ([], "Bool") },[]) -> Some t
  1670. | _ -> None )
  1671. | _ -> None
  1672. in
  1673. let is_double t = like_float t && not (like_int t) in
  1674. let is_int t = like_int t in
  1675. DynamicOperators.configure gen
  1676. (DynamicOperators.abstract_implementation gen (fun e -> match e.eexpr with
  1677. | TBinop (Ast.OpEq, e1, e2)
  1678. | TBinop (Ast.OpAdd, e1, e2)
  1679. | TBinop (Ast.OpNotEq, e1, e2) -> is_dynamic e1.etype or is_dynamic e2.etype or is_type_param e1.etype or is_type_param e2.etype
  1680. | TBinop (Ast.OpLt, e1, e2)
  1681. | TBinop (Ast.OpLte, e1, e2)
  1682. | TBinop (Ast.OpGte, e1, e2)
  1683. | TBinop (Ast.OpGt, e1, e2) -> is_dynamic e.etype or is_dynamic_expr e1 or is_dynamic_expr e2 or is_string e1.etype or is_string e2.etype
  1684. | TBinop (_, e1, e2) -> is_dynamic e.etype or is_dynamic_expr e1 or is_dynamic_expr e2
  1685. | TUnop (_, _, e1) -> is_dynamic_expr e1
  1686. | _ -> false)
  1687. (fun e1 e2 ->
  1688. let is_null e = match e.eexpr with | TConst(TNull) | TLocal({ v_name = "__undefined__" }) -> true | _ -> false in
  1689. if is_null e1 || is_null e2 then
  1690. match e1.eexpr, e2.eexpr with
  1691. | TConst c1, TConst c2 ->
  1692. { e1 with eexpr = TConst(TBool (c1 = c2)); etype = basic.tbool }
  1693. | _ ->
  1694. { e1 with eexpr = TBinop(Ast.OpEq, e1, e2); etype = basic.tbool }
  1695. else begin
  1696. let is_ref = match follow e1.etype, follow e2.etype with
  1697. | TDynamic _, _
  1698. | _, TDynamic _
  1699. | TInst({ cl_path = ([], "Float") },[]), _
  1700. | TAbstract ({ a_path = ([], "Float") },[]) , _
  1701. | TInst( { cl_path = (["haxe"], "Int32") }, [] ), _
  1702. | TInst( { cl_path = (["haxe"], "Int64") }, [] ), _
  1703. | TInst({ cl_path = ([], "Int") },[]), _
  1704. | TAbstract ({ a_path = ([], "Int") },[]) , _
  1705. | TEnum({ e_path = ([], "Bool") },[]), _
  1706. | TAbstract ({ a_path = ([], "Bool") },[]) , _
  1707. | _, TInst({ cl_path = ([], "Float") },[])
  1708. | _, TAbstract ({ a_path = ([], "Float") },[])
  1709. | _, TInst({ cl_path = ([], "Int") },[])
  1710. | _, TAbstract ({ a_path = ([], "Int") },[])
  1711. | _, TInst( { cl_path = (["haxe"], "Int32") }, [] )
  1712. | _, TInst( { cl_path = (["haxe"], "Int64") }, [] )
  1713. | _, TEnum({ e_path = ([], "Bool") },[])
  1714. | _, TAbstract ({ a_path = ([], "Bool") },[])
  1715. | TInst( { cl_kind = KTypeParameter _ }, [] ), _
  1716. | _, TInst( { cl_kind = KTypeParameter _ }, [] ) -> false
  1717. | _, _ -> true
  1718. in
  1719. let static = mk_static_field_access_infer (runtime_cl) (if is_ref then "refEq" else "eq") e1.epos [] in
  1720. { eexpr = TCall(static, [e1; e2]); etype = gen.gcon.basic.tbool; epos=e1.epos }
  1721. end
  1722. )
  1723. (fun e e1 e2 ->
  1724. match may_nullable e1.etype, may_nullable e2.etype with
  1725. | Some t1, Some t2 ->
  1726. let t1, t2 = if is_string t1 || is_string t2 then
  1727. basic.tstring, basic.tstring
  1728. else if is_double t1 || is_double t2 then
  1729. basic.tfloat, basic.tfloat
  1730. else if is_int t1 || is_int t2 then
  1731. basic.tint, basic.tint
  1732. else t1, t2 in
  1733. { eexpr = TBinop(Ast.OpAdd, mk_cast t1 e1, mk_cast t2 e2); etype = e.etype; epos = e1.epos }
  1734. | _ ->
  1735. let static = mk_static_field_access_infer (runtime_cl) "plus" e1.epos [] in
  1736. mk_cast e.etype { eexpr = TCall(static, [e1; e2]); etype = t_dynamic; epos=e1.epos })
  1737. (fun e1 e2 ->
  1738. if is_string e1.etype then begin
  1739. { e1 with eexpr = TCall(mk_field_access gen e1 "compareTo" e1.epos, [ e2 ]); etype = gen.gcon.basic.tint }
  1740. end else begin
  1741. let static = mk_static_field_access_infer (runtime_cl) "compare" e1.epos [] in
  1742. { eexpr = TCall(static, [e1; e2]); etype = gen.gcon.basic.tint; epos=e1.epos }
  1743. end));
  1744. FilterClosures.configure gen (FilterClosures.traverse gen (fun e1 s -> true) closure_func);
  1745. let base_exception = get_cl (get_type gen (["java"; "lang"], "Throwable")) in
  1746. let base_exception_t = TInst(base_exception, []) in
  1747. let hx_exception = get_cl (get_type gen (["haxe";"lang"], "HaxeException")) in
  1748. let hx_exception_t = TInst(hx_exception, []) in
  1749. let rec is_exception t =
  1750. match follow t with
  1751. | TInst(cl,_) ->
  1752. if cl == base_exception then
  1753. true
  1754. else
  1755. (match cl.cl_super with | None -> false | Some (cl,arg) -> is_exception (TInst(cl,arg)))
  1756. | _ -> false
  1757. in
  1758. TryCatchWrapper.configure gen
  1759. (
  1760. TryCatchWrapper.traverse gen
  1761. (fun t -> not (is_exception (real_type t)))
  1762. (fun throwexpr expr ->
  1763. let wrap_static = mk_static_field_access (hx_exception) "wrap" (TFun([("obj",false,t_dynamic)], base_exception_t)) expr.epos in
  1764. { throwexpr with eexpr = TThrow { expr with eexpr = TCall(wrap_static, [expr]) }; etype = gen.gcon.basic.tvoid }
  1765. )
  1766. (fun v_to_unwrap pos ->
  1767. let local = mk_cast hx_exception_t { eexpr = TLocal(v_to_unwrap); etype = v_to_unwrap.v_type; epos = pos } in
  1768. mk_field_access gen local "obj" pos
  1769. )
  1770. (fun rethrow ->
  1771. let wrap_static = mk_static_field_access (hx_exception) "wrap" (TFun([("obj",false,t_dynamic)], base_exception_t)) rethrow.epos in
  1772. { rethrow with eexpr = TThrow { rethrow with eexpr = TCall(wrap_static, [rethrow]) }; }
  1773. )
  1774. (base_exception_t)
  1775. (hx_exception_t)
  1776. (fun v e -> e)
  1777. );
  1778. let get_typeof e =
  1779. { e with eexpr = TCall( { eexpr = TLocal( alloc_var "__typeof__" t_dynamic ); etype = t_dynamic; epos = e.epos }, [e] ) }
  1780. in
  1781. ClassInstance.configure gen (ClassInstance.traverse gen (fun e mt -> get_typeof e));
  1782. (*let v = alloc_var "$type_param" t_dynamic in*)
  1783. TypeParams.configure gen (fun ecall efield params elist ->
  1784. { ecall with eexpr = TCall(efield, elist) }
  1785. );
  1786. CastDetect.configure gen (CastDetect.default_implementation gen ~native_string_cast:false (Some (TEnum(empty_e, []))) true);
  1787. (*FollowAll.configure gen;*)
  1788. SwitchToIf.configure gen (SwitchToIf.traverse gen (fun e ->
  1789. match e.eexpr with
  1790. | TSwitch(cond, cases, def) ->
  1791. (match gen.gfollow#run_f cond.etype with
  1792. | TInst( { cl_path = (["haxe"], "Int32") }, [] )
  1793. | TInst({ cl_path = ([], "Int") },[])
  1794. | TAbstract ({ a_path = ([], "Int") },[])
  1795. | TInst({ cl_path = ([], "String") },[]) ->
  1796. (List.exists (fun (c,_) ->
  1797. List.exists (fun expr -> match expr.eexpr with | TConst _ -> false | _ -> true ) c
  1798. ) cases)
  1799. | _ -> true
  1800. )
  1801. | _ -> assert false
  1802. ) true );
  1803. let native_arr_cl = get_cl ( get_type gen (["java"], "NativeArray") ) in
  1804. ExpressionUnwrap.configure gen (ExpressionUnwrap.traverse gen (fun e -> Some { eexpr = TVars([mk_temp gen "expr" e.etype, Some e]); etype = gen.gcon.basic.tvoid; epos = e.epos }));
  1805. UnnecessaryCastsRemoval.configure gen;
  1806. IntDivisionSynf.configure gen (IntDivisionSynf.default_implementation gen true);
  1807. UnreachableCodeEliminationSynf.configure gen (UnreachableCodeEliminationSynf.traverse gen true true true true);
  1808. ArrayDeclSynf.configure gen (ArrayDeclSynf.default_implementation gen native_arr_cl);
  1809. let goto_special = alloc_var "__goto__" t_dynamic in
  1810. let label_special = alloc_var "__label__" t_dynamic in
  1811. SwitchBreakSynf.configure gen (SwitchBreakSynf.traverse gen
  1812. (fun e_loop n api ->
  1813. { e_loop with eexpr = TBlock( { eexpr = TCall( mk_local label_special e_loop.epos, [ mk_int gen n e_loop.epos ] ); etype = t_dynamic; epos = e_loop.epos } :: [e_loop] ) };
  1814. )
  1815. (fun e_break n api ->
  1816. { eexpr = TCall( mk_local goto_special e_break.epos, [ mk_int gen n e_break.epos ] ); etype = t_dynamic; epos = e_break.epos }
  1817. )
  1818. );
  1819. DefaultArguments.configure gen (DefaultArguments.traverse gen);
  1820. JavaSpecificSynf.configure gen (JavaSpecificSynf.traverse gen runtime_cl);
  1821. JavaSpecificESynf.configure gen (JavaSpecificESynf.traverse gen runtime_cl);
  1822. (* add native String as a String superclass *)
  1823. let str_cl = match gen.gcon.basic.tstring with | TInst(cl,_) -> cl | _ -> assert false in
  1824. str_cl.cl_super <- Some (get_cl (get_type gen (["haxe";"lang"], "NativeString")), []);
  1825. let mkdir dir = if not (Sys.file_exists dir) then Unix.mkdir dir 0o755 in
  1826. mkdir gen.gcon.file;
  1827. mkdir (gen.gcon.file ^ "/src");
  1828. (* add resources array *)
  1829. (try
  1830. let res = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
  1831. let cf = PMap.find "content" res.cl_statics in
  1832. let res = ref [] in
  1833. Hashtbl.iter (fun name v ->
  1834. res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res;
  1835. let f = open_out (gen.gcon.file ^ "/src/" ^ name) in
  1836. output_string f v;
  1837. close_out f
  1838. ) gen.gcon.resources;
  1839. cf.cf_expr <- Some ({ eexpr = TArrayDecl(!res); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = Ast.null_pos })
  1840. with | Not_found -> ());
  1841. run_filters gen;
  1842. TypeParams.RenameTypeParameters.run gen;
  1843. let t = Common.timer "code generation" in
  1844. generate_modules_t gen "java" "src" change_path module_gen;
  1845. dump_descriptor gen ("hxjava_build.txt") path_s;
  1846. if ( not (Common.defined gen.gcon Define.NoCompilation) ) then begin
  1847. let old_dir = Sys.getcwd() in
  1848. Sys.chdir gen.gcon.file;
  1849. let cmd = "haxelib run hxjava hxjava_build.txt --haxe-version " ^ (string_of_int gen.gcon.version) in
  1850. print_endline cmd;
  1851. if gen.gcon.run_command cmd <> 0 then failwith "Build failed";
  1852. Sys.chdir old_dir;
  1853. end;
  1854. t()
  1855. (* end of configure function *)
  1856. let before_generate con =
  1857. let java_ver = try
  1858. int_of_string (PMap.find "java_ver" con.defines)
  1859. with | Not_found ->
  1860. Common.define_value con Define.JavaVer "7";
  1861. 7
  1862. in
  1863. if java_ver < 5 then failwith ("Java version is defined to target Java " ^ string_of_int java_ver ^ ", but the compiler can only output code to versions equal or superior to Java 5");
  1864. let rec loop i =
  1865. Common.raw_define con ("java" ^ (string_of_int i));
  1866. if i > 0 then loop (i - 1)
  1867. in
  1868. loop java_ver;
  1869. ()
  1870. let generate con =
  1871. let gen = new_ctx con in
  1872. gen.gallow_tp_dynamic_conversion <- true;
  1873. let basic = con.basic in
  1874. (* make the basic functions in java *)
  1875. let basic_fns =
  1876. [
  1877. mk_class_field "equals" (TFun(["obj",false,t_dynamic], basic.tbool)) true Ast.null_pos (Method MethNormal) [];
  1878. mk_class_field "toString" (TFun([], basic.tstring)) true Ast.null_pos (Method MethNormal) [];
  1879. mk_class_field "hashCode" (TFun([], basic.tint)) true Ast.null_pos (Method MethNormal) [];
  1880. ] in
  1881. List.iter (fun cf -> gen.gbase_class_fields <- PMap.add cf.cf_name cf gen.gbase_class_fields) basic_fns;
  1882. (try
  1883. configure gen
  1884. with | TypeNotFound path -> con.error ("Error. Module '" ^ (path_s path) ^ "' is required and was not included in build.") Ast.null_pos);
  1885. debug_mode := false
  1886. (** Java lib *)
  1887. open JData
  1888. type java_lib_ctx = {
  1889. jcom : Common.context;
  1890. (* current tparams context *)
  1891. mutable jtparams : jtypes list;
  1892. }
  1893. let lookup_jclass com path =
  1894. List.fold_left (fun acc (_,_,_,get_raw_class) ->
  1895. match acc with
  1896. | None -> get_raw_class path
  1897. | Some p -> Some p
  1898. ) None com.java_libs
  1899. exception ConversionError of string * pos
  1900. let error s p = raise (ConversionError (s, p))
  1901. let mk_clsname ctx name =
  1902. (* handle with inner classes *)
  1903. String.map (function | '$' -> '_' | c -> c) name
  1904. let real_java_path ctx (pack,name) =
  1905. path_s (pack, name)
  1906. let mk_type_path ctx path params =
  1907. let name, sub =
  1908. try
  1909. let p, _ = String.split (snd path) "$" in
  1910. p, Some (mk_clsname ctx (snd path))
  1911. with
  1912. | Invalid_string -> mk_clsname ctx (snd path), None
  1913. in
  1914. CTPath {
  1915. tpackage = fst path;
  1916. tname = name;
  1917. tparams = params;
  1918. tsub = sub;
  1919. }
  1920. let has_tparam name params = List.exists(fun (n,_,_) -> n = name) params
  1921. let rec convert_arg ctx p arg =
  1922. match arg with
  1923. | TAny | TType (WSuper, _) -> TPType (mk_type_path ctx ([], "Dynamic") [])
  1924. | TType (_, jsig) -> TPType (convert_signature ctx p jsig)
  1925. and convert_signature ctx p jsig =
  1926. match jsig with
  1927. | TByte -> mk_type_path ctx (["java"; "types"], "Int8") []
  1928. | TChar -> mk_type_path ctx (["java"; "types"], "Char16") []
  1929. | TDouble -> mk_type_path ctx ([], "Float") []
  1930. | TFloat -> mk_type_path ctx ([], "Single") []
  1931. | TInt -> mk_type_path ctx ([], "Int") []
  1932. | TLong -> mk_type_path ctx (["haxe"], "Int64") []
  1933. | TShort -> mk_type_path ctx (["java"; "types"], "Int16") []
  1934. | TBool -> mk_type_path ctx ([], "Bool") []
  1935. | TObject ( (["haxe";"root"], name), args ) -> mk_type_path ctx ([], name) (List.map (convert_arg ctx p) args)
  1936. (** nullable types *)
  1937. | TObject ( (["java";"lang"], "Integer"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx ([], "Int") []) ]
  1938. | TObject ( (["java";"lang"], "Double"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx ([], "Float") []) ]
  1939. | TObject ( (["java";"lang"], "Single"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx ([], "Single") []) ]
  1940. | TObject ( (["java";"lang"], "Boolean"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx ([], "Bool") []) ]
  1941. | TObject ( (["java";"lang"], "Byte"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["java";"types"], "Int8") []) ]
  1942. | TObject ( (["java";"lang"], "Character"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["java";"types"], "Char16") []) ]
  1943. | TObject ( (["java";"lang"], "Short"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["java";"types"], "Int16") []) ]
  1944. | TObject ( (["java";"lang"], "Long"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["haxe"], "Int64") []) ]
  1945. (** other std types *)
  1946. | TObject ( (["java";"lang"], "Object"), [] ) -> mk_type_path ctx ([], "Dynamic") []
  1947. | TObject ( (["java";"lang"], "String"), [] ) -> mk_type_path ctx ([], "String") []
  1948. | TObject ( (["java";"lang"], "Class"), [] ) -> mk_type_path ctx ([], "Class") [convert_arg ctx p TAny]
  1949. | TObject ( (["java";"lang"], "Class"), args ) -> mk_type_path ctx ([], "Class") (List.map (convert_arg ctx p) args)
  1950. (** other types *)
  1951. | TObject ( path, [] ) ->
  1952. (match lookup_jclass ctx.jcom path with
  1953. | Some (jcl, _, _) -> mk_type_path ctx path (List.map (fun _ -> convert_arg ctx p TAny) jcl.ctypes)
  1954. | None -> mk_type_path ctx path [])
  1955. | TObject ( path, args ) -> mk_type_path ctx path (List.map (convert_arg ctx p) args)
  1956. | TObjectInner (pack, (name, params) :: inners) ->
  1957. let actual_param = match List.rev inners with
  1958. | (_, p) :: _ -> p
  1959. | _ -> assert false in
  1960. mk_type_path ctx (pack, name ^ "$" ^ String.concat "$" (List.map (fun (s,_) -> s) inners)) (List.map (fun param -> convert_arg ctx p param) actual_param)
  1961. | TObjectInner (pack, inners) -> assert false
  1962. | TArray (jsig, _) -> mk_type_path ctx (["java"], "NativeArray") [ TPType (convert_signature ctx p jsig) ]
  1963. | TMethod _ -> JReader.error "TMethod cannot be converted directly into Complex Type"
  1964. | TTypeParameter s -> (match ctx.jtparams with
  1965. | cur :: others ->
  1966. if has_tparam s cur then
  1967. mk_type_path ctx ([], s) []
  1968. else begin
  1969. if ctx.jcom.verbose && not(List.exists (has_tparam s) others) then print_endline ("Type parameter " ^ s ^ " was not found while building type!");
  1970. mk_type_path ctx ([], "Dynamic") []
  1971. end
  1972. | _ ->
  1973. if ctx.jcom.verbose then print_endline ("Empty type parameter stack!");
  1974. mk_type_path ctx ([], "Dynamic") [])
  1975. let convert_constant ctx p const =
  1976. Option.map_default (function
  1977. | ConstString s -> Some (EConst (String s), p)
  1978. | ConstInt i -> Some (EConst (Int (Printf.sprintf "%ld" i)), p)
  1979. | ConstFloat f | ConstDouble f -> Some (EConst (Float (Printf.sprintf "%E" f)), p)
  1980. | _ -> None) None const
  1981. let convert_param ctx p param =
  1982. let name, constraints = match param with
  1983. | (name, Some extends_sig, implem_sig) ->
  1984. name, extends_sig :: implem_sig
  1985. | (name, None, implemem_sig) ->
  1986. name, implemem_sig
  1987. in
  1988. {
  1989. tp_name = name;
  1990. tp_params = [];
  1991. tp_constraints = List.map (convert_signature ctx p) constraints;
  1992. }
  1993. let get_type_path ctx ct = match ct with | CTPath p -> p | _ -> assert false
  1994. let is_override field =
  1995. List.exists (function
  1996. (* TODO: pass anotations as @:meta *)
  1997. | AttrVisibleAnnotations ann ->
  1998. List.exists (function
  1999. | { ann_type = TObject( (["java";"lang"], "Override"), [] ) } ->
  2000. true
  2001. | _ -> false
  2002. ) ann
  2003. | _ -> false
  2004. ) field.jf_attributes
  2005. let mk_override field =
  2006. { field with jf_attributes = ((AttrVisibleAnnotations [{ ann_type = TObject( (["java";"lang"], "Override"), [] ); ann_elements = [] }]) :: field.jf_attributes) }
  2007. let convert_java_enum ctx p pe =
  2008. let meta = ref [Meta.Native, [EConst (String (real_java_path ctx pe.cpath) ), p], p ] in
  2009. let data = ref [] in
  2010. List.iter (fun f ->
  2011. if List.mem JEnum f.jf_flags then
  2012. data := { ec_name = f.jf_name; ec_doc = None; ec_meta = []; ec_args = []; ec_pos = p; ec_params = []; ec_type = None; } :: !data;
  2013. ) pe.cfields;
  2014. EEnum {
  2015. d_name = mk_clsname ctx (snd pe.cpath);
  2016. d_doc = None;
  2017. d_params = []; (* enums never have type parameters *)
  2018. d_meta = !meta;
  2019. d_flags = [EExtern];
  2020. d_data = !data;
  2021. }
  2022. let convert_java_field ctx p jc field =
  2023. let p = { p with pfile = p.pfile ^" (" ^field.jf_name ^")" } in
  2024. let cff_doc = None in
  2025. let cff_pos = p in
  2026. let cff_meta = ref [] in
  2027. let cff_access = ref [] in
  2028. let cff_name = match field.jf_name with
  2029. | "<init>" -> "new"
  2030. | "<clinit>"-> raise Exit (* __init__ field *)
  2031. | name when String.length name > 5 ->
  2032. (match String.sub name 0 5 with
  2033. | "__hx_" | "this$" -> raise Exit
  2034. | _ -> name)
  2035. | name -> name
  2036. in
  2037. let jf_constant = ref field.jf_constant in
  2038. let readonly = ref false in
  2039. List.iter (function
  2040. | JPublic -> cff_access := APublic :: !cff_access
  2041. | JPrivate -> raise Exit (* private instances aren't useful on externs *)
  2042. | JProtected -> cff_access := APrivate :: !cff_access
  2043. | JStatic -> cff_access := AStatic :: !cff_access
  2044. | JFinal ->
  2045. cff_meta := (Meta.Final, [], p) :: !cff_meta;
  2046. (match field.jf_kind, field.jf_vmsignature, field.jf_constant with
  2047. | JKField, TObject _, _ ->
  2048. jf_constant := None
  2049. | JKField, _, Some _ ->
  2050. readonly := true;
  2051. jf_constant := None;
  2052. | _ -> jf_constant := None)
  2053. | JSynchronized -> cff_meta := (Meta.Synchronized, [], p) :: !cff_meta
  2054. | JVolatile -> cff_meta := (Meta.Volatile, [], p) :: !cff_meta
  2055. | JTransient -> cff_meta := (Meta.Transient, [], p) :: !cff_meta
  2056. | JVarArgs -> cff_meta := (Meta.VarArgs, [], p) :: !cff_meta
  2057. | _ -> ()
  2058. ) field.jf_flags;
  2059. List.iter (function
  2060. | AttrDeprecated -> cff_meta := (Meta.Deprecated, [], p) :: !cff_meta
  2061. (* TODO: pass anotations as @:meta *)
  2062. | AttrVisibleAnnotations ann ->
  2063. List.iter (function
  2064. | { ann_type = TObject( (["java";"lang"], "Override"), [] ) } ->
  2065. cff_access := AOverride :: !cff_access
  2066. | _ -> ()
  2067. ) ann
  2068. | _ -> ()
  2069. ) field.jf_attributes;
  2070. let kind = match field.jf_kind with
  2071. | JKField when !readonly ->
  2072. FProp ("default", "null", Some (convert_signature ctx p field.jf_signature), None)
  2073. | JKField ->
  2074. FVar (Some (convert_signature ctx p field.jf_signature), None)
  2075. | JKMethod ->
  2076. match field.jf_signature with
  2077. | TMethod (args, ret) ->
  2078. let i = ref 0 in
  2079. let args = List.map (fun s ->
  2080. incr i;
  2081. "param" ^ string_of_int !i, false, Some(convert_signature ctx p s), None
  2082. ) args in
  2083. let t = Option.map_default (convert_signature ctx p) (mk_type_path ctx ([], "Void") []) ret in
  2084. cff_meta := (Meta.Overload, [], p) :: !cff_meta;
  2085. let types = List.map (function
  2086. | (name, Some ext, impl) ->
  2087. {
  2088. tp_name = name;
  2089. tp_params = [];
  2090. tp_constraints = List.map (convert_signature ctx p) (ext :: impl);
  2091. }
  2092. | (name, None, impl) ->
  2093. {
  2094. tp_name = name;
  2095. tp_params = [];
  2096. tp_constraints = List.map (convert_signature ctx p) (impl);
  2097. }
  2098. ) field.jf_types in
  2099. FFun ({
  2100. f_params = types;
  2101. f_args = args;
  2102. f_type = Some t;
  2103. f_expr = None
  2104. })
  2105. | _ -> error "Method signature was expected" p
  2106. in
  2107. let cff_name, cff_meta =
  2108. if String.get cff_name 0 = '%' then
  2109. let name = (String.sub cff_name 1 (String.length cff_name - 1)) in
  2110. "_" ^ name,
  2111. (Meta.Native, [EConst (String (name) ), cff_pos], cff_pos) :: !cff_meta
  2112. else
  2113. cff_name, !cff_meta
  2114. in
  2115. {
  2116. cff_name = cff_name;
  2117. cff_doc = cff_doc;
  2118. cff_pos = cff_pos;
  2119. cff_meta = cff_meta;
  2120. cff_access = !cff_access;
  2121. cff_kind = kind
  2122. }
  2123. let convert_java_class ctx p jc =
  2124. match List.mem JEnum jc.cflags with
  2125. | true -> (* is enum *)
  2126. convert_java_enum ctx p jc
  2127. | false ->
  2128. let flags = ref [HExtern] in
  2129. (* todo: instead of JavaNative, use more specific definitions *)
  2130. let meta = ref [Meta.JavaNative, [], p; Meta.Native, [EConst (String (real_java_path ctx jc.cpath) ), p], p] in
  2131. let is_interface = ref false in
  2132. List.iter (fun f -> match f with
  2133. | JFinal -> meta := (Meta.Final, [], p) :: !meta
  2134. | JInterface ->
  2135. is_interface := true;
  2136. flags := HInterface :: !flags
  2137. | JAbstract -> meta := (Meta.Abstract, [], p) :: !meta
  2138. | JAnnotation -> meta := (Meta.Annotation, [], p) :: !meta
  2139. | _ -> ()
  2140. ) jc.cflags;
  2141. (match jc.csuper with
  2142. | TObject( (["java";"lang"], "Object"), _ ) -> ()
  2143. | TObject( (["haxe";"lang"], "HxObject"), _ ) -> meta := (Meta.HxGen,[],p) :: !meta
  2144. | _ -> flags := HExtends (get_type_path ctx (convert_signature ctx p jc.csuper)) :: !flags
  2145. );
  2146. List.iter (fun i ->
  2147. match i with
  2148. | TObject ( (["haxe";"lang"], "IHxObject"), _ ) -> meta := (Meta.HxGen,[],p) :: !meta
  2149. | _ -> flags :=
  2150. if !is_interface then
  2151. HExtends (get_type_path ctx (convert_signature ctx p i)) :: !flags
  2152. else
  2153. HImplements (get_type_path ctx (convert_signature ctx p i)) :: !flags
  2154. ) jc.cinterfaces;
  2155. let fields = ref [] in
  2156. List.iter (fun f ->
  2157. try
  2158. if !is_interface && List.mem JStatic f.jf_flags then
  2159. ()
  2160. else
  2161. fields := convert_java_field ctx p jc f :: !fields
  2162. with
  2163. | Exit -> ()
  2164. ) (jc.cfields @ jc.cmethods);
  2165. EClass {
  2166. d_name = mk_clsname ctx (snd jc.cpath);
  2167. d_doc = None;
  2168. d_params = List.map (convert_param ctx p) jc.ctypes;
  2169. d_meta = !meta;
  2170. d_flags = !flags;
  2171. d_data = !fields;
  2172. }
  2173. let create_ctx com =
  2174. {
  2175. jcom = com;
  2176. jtparams = [];
  2177. }
  2178. let filename_to_clsname f =
  2179. String.map (fun c -> if c = '$' then '_' else c) (String.sub f 0 (String.length f - 6))
  2180. let rec get_classes_dir pack dir ret =
  2181. Array.iter (fun f -> match (Unix.stat (dir ^"/"^ f)).st_kind with
  2182. | S_DIR ->
  2183. get_classes_dir (pack @ [f]) (dir ^"/"^ f) ret
  2184. | _ when (String.sub (String.uncapitalize f) (String.length f - 6) 6) = ".class" ->
  2185. ret := (pack, filename_to_clsname f) :: !ret;
  2186. | _ -> ()
  2187. ) (Sys.readdir dir)
  2188. let get_classes_zip zip =
  2189. let ret = ref [] in
  2190. List.iter (function
  2191. | { Zip.is_directory = false; Zip.filename = f } when (String.sub (String.uncapitalize f) (String.length f - 6) 6) = ".class" ->
  2192. (match List.rev (String.nsplit f "/") with
  2193. | clsname :: pack ->
  2194. ret := (List.rev pack, filename_to_clsname clsname) :: !ret
  2195. | _ ->
  2196. ret := ([], filename_to_clsname f) :: !ret)
  2197. | _ -> ()
  2198. ) (Zip.entries zip);
  2199. !ret
  2200. let add_java_lib com file =
  2201. let get_raw_class, close, list_all_files =
  2202. let file = if Sys.file_exists file then
  2203. file
  2204. else if Sys.file_exists (file ^ ".jar") then
  2205. file ^ ".jar"
  2206. else
  2207. failwith ("Java lib " ^ file ^ " not found")
  2208. in
  2209. (* check if it is a directory or jar file *)
  2210. match (Unix.stat file).st_kind with
  2211. | S_DIR -> (* open classes directly from directory *)
  2212. (fun (pack, name) ->
  2213. let real_path = file ^ "/" ^ (String.concat "." pack) ^ "/" ^ name ^ ".class" in
  2214. try
  2215. let data = Std.input_file ~bin:true real_path in
  2216. Some(JReader.parse_class (IO.input_string data), real_path, real_path)
  2217. with
  2218. | _ -> None), (fun () -> ()), (fun () -> let ret = ref [] in get_classes_dir [] file ret; !ret)
  2219. | _ -> (* open zip file *)
  2220. let zip = Zip.open_in file in
  2221. let closed = ref false in
  2222. (fun (pack, name) ->
  2223. if !closed then failwith ("JAR file " ^ file ^ " already closed");
  2224. try
  2225. let location = (String.concat "/" (pack @ [name]) ^ ".class") in
  2226. let entry = Zip.find_entry zip location in
  2227. let data = Zip.read_entry zip entry in
  2228. Some(JReader.parse_class (IO.input_string data), file, file ^ "@" ^ location)
  2229. with
  2230. | Not_found ->
  2231. None),
  2232. (fun () -> closed := true; Zip.close_in zip),
  2233. (fun () -> get_classes_zip zip)
  2234. in
  2235. let cached_types = Hashtbl.create 12 in
  2236. let get_raw_class path =
  2237. try
  2238. Hashtbl.find cached_types path
  2239. with | Not_found ->
  2240. match get_raw_class path with
  2241. | None ->
  2242. Hashtbl.add cached_types path None;
  2243. None
  2244. | Some (i, p1, p2) ->
  2245. let ret = Some (i, p1, p2) in
  2246. Hashtbl.add cached_types path ret;
  2247. ret
  2248. in
  2249. let rec build ctx path p types =
  2250. try
  2251. if List.mem path !types then
  2252. None
  2253. else begin
  2254. types := path :: !types;
  2255. match get_raw_class path, path with
  2256. | None, ([], c) -> build ctx (["haxe";"root"], c) p types
  2257. | None, _ -> None
  2258. | Some (cls, real_path, pos_path), _ ->
  2259. if com.verbose then print_endline ("Parsed Java class " ^ (path_s cls.cpath));
  2260. let old_types = ctx.jtparams in
  2261. ctx.jtparams <- cls.ctypes :: ctx.jtparams;
  2262. let pos = { pfile = pos_path; pmin = 0; pmax = 0; } in
  2263. (* search static / non-static name clash *)
  2264. let nonstatics = ref [] in
  2265. List.iter (fun f ->
  2266. if not(List.mem JStatic f.jf_flags) then nonstatics := f :: !nonstatics
  2267. ) (cls.cfields @ cls.cmethods);
  2268. let cmethods = ref cls.cmethods in
  2269. let rec loop cls =
  2270. match cls.csuper with
  2271. | TObject ((["java";"lang"],"Object"), _) -> ()
  2272. | TObject (path, _) ->
  2273. (match lookup_jclass com path with
  2274. | None -> ()
  2275. | Some (cls,_,_) ->
  2276. List.iter (fun f -> if not (List.mem JStatic f.jf_flags) then nonstatics := f :: !nonstatics) (cls.cfields @ cls.cmethods);
  2277. cmethods := List.map (fun jm ->
  2278. if not(List.mem JStatic jm.jf_flags) && not (is_override jm) && List.exists (fun msup ->
  2279. msup.jf_name = jm.jf_name && not(List.mem JStatic msup.jf_flags) && match msup.jf_vmsignature, jm.jf_vmsignature with
  2280. | TMethod(a1,_), TMethod(a2,_) -> a1 = a2
  2281. | _ -> false
  2282. ) cls.cmethods then
  2283. mk_override jm
  2284. else
  2285. jm
  2286. ) !cmethods;
  2287. loop cls)
  2288. | _ -> ()
  2289. in
  2290. loop cls;
  2291. (* change field name to not collide with haxe keywords *)
  2292. let map_field f =
  2293. let change = match f.jf_name with
  2294. | "callback" | "cast" | "extern" | "function" | "in" | "typedef" | "using" | "var" | "untyped" | "inline" -> true
  2295. | _ when List.mem JStatic f.jf_flags && List.exists (fun f2 -> f.jf_name = f2.jf_name) !nonstatics -> true
  2296. | _ -> false
  2297. in
  2298. if change then
  2299. { f with jf_name = "%" ^ f.jf_name }
  2300. else
  2301. f
  2302. in
  2303. (* change static fields that have the same name as methods *)
  2304. let cfields = List.map map_field cls.cfields in
  2305. let cmethods = List.map map_field !cmethods in
  2306. (* take off variable fields that have the same name as methods *)
  2307. let filter_field f f2 = f != f2 && (List.mem JStatic f.jf_flags = List.mem JStatic f2.jf_flags) && f.jf_name = f2.jf_name && f2.jf_kind <> f.jf_kind in
  2308. let cfields = List.filter (fun f ->
  2309. if List.mem JStatic f.jf_flags then
  2310. not (List.exists (filter_field f) cmethods)
  2311. else
  2312. not (List.exists (filter_field f) !nonstatics)) cfields
  2313. in
  2314. let cls = { cls with cfields = cfields; cmethods = cmethods } in
  2315. let pack = match fst path with | ["haxe";"root"] -> [] | p -> p in
  2316. let ppath = path in
  2317. let inner = List.fold_left (fun acc (path,out,_,_) ->
  2318. (if out <> Some ppath then
  2319. acc
  2320. else match build ctx path p types with
  2321. | Some(_,(_, classes)) ->
  2322. classes @ acc
  2323. | _ -> acc);
  2324. ) [] cls.cinner_types in
  2325. (* build anonymous classes also *)
  2326. let rec loop inner n =
  2327. match build ctx (fst path, snd path ^ "$" ^ (string_of_int n)) p types with
  2328. | Some(_,(_, classes)) ->
  2329. loop (classes @ inner) (n + 1)
  2330. | _ -> inner
  2331. in
  2332. let inner = loop inner 1 in
  2333. let ret = Some ( real_path, (pack, (convert_java_class ctx pos cls, pos) :: inner) ) in
  2334. ctx.jtparams <- old_types;
  2335. ret
  2336. end
  2337. with JReader.Error_message msg ->
  2338. if com.verbose then prerr_endline ("Class reader failed: " ^ msg);
  2339. None
  2340. | _ -> None
  2341. in
  2342. let build path p = build (create_ctx com) path p (ref [["java";"lang"], "String"]) in
  2343. let cached_files = ref None in
  2344. let list_all_files () = match !cached_files with
  2345. | None ->
  2346. let ret = list_all_files () in
  2347. cached_files := Some ret;
  2348. ret
  2349. | Some r -> r
  2350. in
  2351. (* TODO: add_dependency m mdep *)
  2352. com.load_extern_type <- com.load_extern_type @ [build];
  2353. com.java_libs <- (file, close, list_all_files, get_raw_class) :: com.java_libs