codegen.ml 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. (*
  2. The Haxe Compiler
  3. Copyright (C) 2005-2015 Haxe Foundation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *)
  16. open Ast
  17. open Type
  18. open Common
  19. open Typecore
  20. (* -------------------------------------------------------------------------- *)
  21. (* TOOLS *)
  22. let field e name t p =
  23. mk (TField (e,try quick_field e.etype name with Not_found -> assert false)) t p
  24. let fcall e name el ret p =
  25. let ft = tfun (List.map (fun e -> e.etype) el) ret in
  26. mk (TCall (field e name ft p,el)) ret p
  27. let mk_parent e =
  28. mk (TParenthesis e) e.etype e.epos
  29. let string com str p =
  30. mk (TConst (TString str)) com.basic.tstring p
  31. let binop op a b t p =
  32. mk (TBinop (op,a,b)) t p
  33. let index com e index t p =
  34. mk (TArray (e,mk (TConst (TInt (Int32.of_int index))) com.basic.tint p)) t p
  35. let maybe_cast e t =
  36. try
  37. type_eq EqDoNotFollowNull e.etype t;
  38. e
  39. with
  40. Unify_error _ -> mk (TCast(e,None)) t e.epos
  41. let type_constant com c p =
  42. let t = com.basic in
  43. match c with
  44. | Int s ->
  45. if String.length s > 10 && String.sub s 0 2 = "0x" then error "Invalid hexadecimal integer" p;
  46. (try mk (TConst (TInt (Int32.of_string s))) t.tint p
  47. with _ -> mk (TConst (TFloat s)) t.tfloat p)
  48. | Float f -> mk (TConst (TFloat f)) t.tfloat p
  49. | String s -> mk (TConst (TString s)) t.tstring p
  50. | Ident "true" -> mk (TConst (TBool true)) t.tbool p
  51. | Ident "false" -> mk (TConst (TBool false)) t.tbool p
  52. | Ident "null" -> mk (TConst TNull) (t.tnull (mk_mono())) p
  53. | Ident t -> error ("Invalid constant : " ^ t) p
  54. | Regexp _ -> error "Invalid constant" p
  55. let rec type_constant_value com (e,p) =
  56. match e with
  57. | EConst c ->
  58. type_constant com c p
  59. | EParenthesis e ->
  60. type_constant_value com e
  61. | EObjectDecl el ->
  62. mk (TObjectDecl (List.map (fun (n,e) -> n, type_constant_value com e) el)) (TAnon { a_fields = PMap.empty; a_status = ref Closed }) p
  63. | EArrayDecl el ->
  64. mk (TArrayDecl (List.map (type_constant_value com) el)) (com.basic.tarray t_dynamic) p
  65. | _ ->
  66. error "Constant value expected" p
  67. let rec has_properties c =
  68. List.exists (fun f ->
  69. match f.cf_kind with
  70. | Var { v_read = AccCall } -> true
  71. | Var { v_write = AccCall } -> true
  72. | _ when Meta.has Meta.Accessor f.cf_meta -> true
  73. | _ -> false
  74. ) c.cl_ordered_fields || (match c.cl_super with Some (c,_) -> has_properties c | _ -> false)
  75. let get_properties fields =
  76. List.fold_left (fun acc f ->
  77. if Meta.has Meta.Accessor f.cf_meta then
  78. (f.cf_name, f.cf_name) :: acc
  79. else
  80. let acc = (match f.cf_kind with
  81. | Var { v_read = AccCall } -> ("get_" ^ f.cf_name , "get_" ^ f.cf_name) :: acc
  82. | _ -> acc) in
  83. match f.cf_kind with
  84. | Var { v_write = AccCall } -> ("set_" ^ f.cf_name , "set_" ^ f.cf_name) :: acc
  85. | _ -> acc
  86. ) [] fields
  87. let add_property_field com c =
  88. let p = c.cl_pos in
  89. let props = get_properties (c.cl_ordered_statics @ c.cl_ordered_fields) in
  90. match props with
  91. | [] -> ()
  92. | _ ->
  93. let fields,values = List.fold_left (fun (fields,values) (n,v) ->
  94. let cf = mk_field n com.basic.tstring p in
  95. PMap.add n cf fields,(n, string com v p) :: values
  96. ) (PMap.empty,[]) props in
  97. let t = mk_anon fields in
  98. let e = mk (TObjectDecl values) t p in
  99. let cf = mk_field "__properties__" t p in
  100. cf.cf_expr <- Some e;
  101. c.cl_statics <- PMap.add cf.cf_name cf c.cl_statics;
  102. c.cl_ordered_statics <- cf :: c.cl_ordered_statics
  103. let is_removable_field ctx f =
  104. Meta.has Meta.Extern f.cf_meta || Meta.has Meta.Generic f.cf_meta
  105. || (match f.cf_kind with
  106. | Var {v_read = AccRequire (s,_)} -> true
  107. | Method MethMacro -> not ctx.in_macro
  108. | _ -> false)
  109. let escape_res_name name allow_dirs =
  110. ExtString.String.replace_chars (fun chr ->
  111. if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' || chr = '.' then
  112. Char.escaped chr
  113. else if chr = '/' && allow_dirs then
  114. "/"
  115. else
  116. "-x" ^ (string_of_int (Char.code chr))) name
  117. (* -------------------------------------------------------------------------- *)
  118. (* REMOTING PROXYS *)
  119. let extend_remoting ctx c t p async prot =
  120. if c.cl_super <> None then error "Cannot extend several classes" p;
  121. (* remove forbidden packages *)
  122. let rules = ctx.com.package_rules in
  123. ctx.com.package_rules <- PMap.foldi (fun key r acc -> match r with Forbidden -> acc | _ -> PMap.add key r acc) rules PMap.empty;
  124. (* parse module *)
  125. let path = (t.tpackage,t.tname) in
  126. let new_name = (if async then "Async_" else "Remoting_") ^ t.tname in
  127. (* check if the proxy already exists *)
  128. let t = (try
  129. Typeload.load_type_def ctx p { tpackage = fst path; tname = new_name; tparams = []; tsub = None }
  130. with
  131. Error (Module_not_found _,p2) when p == p2 ->
  132. (* build it *)
  133. Common.log ctx.com ("Building proxy for " ^ s_type_path path);
  134. let file, decls = (try
  135. Typeload.parse_module ctx path p
  136. with
  137. | Not_found -> ctx.com.package_rules <- rules; error ("Could not load proxy module " ^ s_type_path path ^ (if fst path = [] then " (try using absolute path)" else "")) p
  138. | e -> ctx.com.package_rules <- rules; raise e) in
  139. ctx.com.package_rules <- rules;
  140. let base_fields = [
  141. { cff_name = "__cnx"; cff_pos = p; cff_doc = None; cff_meta = []; cff_access = []; cff_kind = FVar (Some (CTPath { tpackage = ["haxe";"remoting"]; tname = if async then "AsyncConnection" else "Connection"; tparams = []; tsub = None }),None) };
  142. { cff_name = "new"; cff_pos = p; cff_doc = None; cff_meta = []; cff_access = [APublic]; cff_kind = FFun { f_args = ["c",false,None,None]; f_type = None; f_expr = Some (EBinop (OpAssign,(EConst (Ident "__cnx"),p),(EConst (Ident "c"),p)),p); f_params = [] } };
  143. ] in
  144. let tvoid = CTPath { tpackage = []; tname = "Void"; tparams = []; tsub = None } in
  145. let build_field is_public acc f =
  146. if f.cff_name = "new" then
  147. acc
  148. else match f.cff_kind with
  149. | FFun fd when (is_public || List.mem APublic f.cff_access) && not (List.mem AStatic f.cff_access) ->
  150. if List.exists (fun (_,_,t,_) -> t = None) fd.f_args then error ("Field " ^ f.cff_name ^ " type is not complete and cannot be used by RemotingProxy") p;
  151. let eargs = [EArrayDecl (List.map (fun (a,_,_,_) -> (EConst (Ident a),p)) fd.f_args),p] in
  152. let ftype = (match fd.f_type with Some (CTPath { tpackage = []; tname = "Void" }) -> None | _ -> fd.f_type) in
  153. let fargs, eargs = if async then match ftype with
  154. | Some tret -> fd.f_args @ ["__callb",true,Some (CTFunction ([tret],tvoid)),None], eargs @ [EConst (Ident "__callb"),p]
  155. | _ -> fd.f_args, eargs @ [EConst (Ident "null"),p]
  156. else
  157. fd.f_args, eargs
  158. in
  159. let id = (EConst (String f.cff_name), p) in
  160. let id = if prot then id else ECall ((EConst (Ident "__unprotect__"),p),[id]),p in
  161. let expr = ECall (
  162. (EField (
  163. (ECall ((EField ((EConst (Ident "__cnx"),p),"resolve"),p),[id]),p),
  164. "call")
  165. ,p),eargs),p
  166. in
  167. let expr = if async || ftype = None then expr else (EReturn (Some expr),p) in
  168. let fd = {
  169. f_params = fd.f_params;
  170. f_args = fargs;
  171. f_type = if async then None else ftype;
  172. f_expr = Some (EBlock [expr],p);
  173. } in
  174. { cff_name = f.cff_name; cff_pos = p; cff_doc = None; cff_meta = []; cff_access = [APublic]; cff_kind = FFun fd } :: acc
  175. | _ -> acc
  176. in
  177. let decls = List.map (fun d ->
  178. match d with
  179. | EClass c, p when c.d_name = t.tname ->
  180. let is_public = List.mem HExtern c.d_flags || List.mem HInterface c.d_flags in
  181. let fields = List.rev (List.fold_left (build_field is_public) base_fields c.d_data) in
  182. (EClass { c with d_flags = []; d_name = new_name; d_data = fields },p)
  183. | _ -> d
  184. ) decls in
  185. let m = Typeload.type_module ctx (t.tpackage,new_name) file decls p in
  186. add_dependency ctx.m.curmod m;
  187. try
  188. List.find (fun tdecl -> snd (t_path tdecl) = new_name) m.m_types
  189. with Not_found ->
  190. error ("Module " ^ s_type_path path ^ " does not define type " ^ t.tname) p
  191. ) in
  192. match t with
  193. | TClassDecl c2 when c2.cl_params = [] -> ignore(c2.cl_build()); c.cl_super <- Some (c2,[]);
  194. | _ -> error "Remoting proxy must be a class without parameters" p
  195. (* -------------------------------------------------------------------------- *)
  196. (* HAXE.RTTI.GENERIC *)
  197. exception Generic_Exception of string * Ast.pos
  198. type generic_context = {
  199. ctx : typer;
  200. subst : (t * t) list;
  201. name : string;
  202. p : pos;
  203. mutable mg : module_def option;
  204. }
  205. let make_generic ctx ps pt p =
  206. let rec loop l1 l2 =
  207. match l1, l2 with
  208. | [] , [] -> []
  209. | (x,TLazy f) :: l1, _ -> loop ((x,(!f)()) :: l1) l2
  210. | (_,t1) :: l1 , t2 :: l2 -> (t1,t2) :: loop l1 l2
  211. | _ -> assert false
  212. in
  213. let name =
  214. String.concat "_" (List.map2 (fun (s,_) t ->
  215. let s_type_path_underscore (p,s) = match p with [] -> s | _ -> String.concat "_" p ^ "_" ^ s in
  216. let rec loop top t = match follow t with
  217. | TInst(c,tl) -> (s_type_path_underscore c.cl_path) ^ (loop_tl tl)
  218. | TEnum(en,tl) -> (s_type_path_underscore en.e_path) ^ (loop_tl tl)
  219. | TAbstract(a,tl) -> (s_type_path_underscore a.a_path) ^ (loop_tl tl)
  220. | _ when not top -> "_" (* allow unknown/incompatible types as type parameters to retain old behavior *)
  221. | TMono _ -> raise (Generic_Exception (("Could not determine type for parameter " ^ s), p))
  222. | TDynamic _ -> "Dynamic"
  223. | t -> raise (Generic_Exception (("Type parameter must be a class or enum instance (found " ^ (s_type (print_context()) t) ^ ")"), p))
  224. and loop_tl tl = match tl with
  225. | [] -> ""
  226. | tl -> "_" ^ String.concat "_" (List.map (loop false) tl)
  227. in
  228. loop true t
  229. ) ps pt)
  230. in
  231. {
  232. ctx = ctx;
  233. subst = loop ps pt;
  234. name = name;
  235. p = p;
  236. mg = None;
  237. }
  238. let rec generic_substitute_type gctx t =
  239. match t with
  240. | TInst ({ cl_kind = KGeneric } as c2,tl2) ->
  241. (* maybe loop, or generate cascading generics *)
  242. let _, _, f = gctx.ctx.g.do_build_instance gctx.ctx (TClassDecl c2) gctx.p in
  243. let t = f (List.map (generic_substitute_type gctx) tl2) in
  244. (match follow t,gctx.mg with TInst(c,_), Some m -> add_dependency m c.cl_module | _ -> ());
  245. t
  246. | _ ->
  247. try
  248. generic_substitute_type gctx (List.assq t gctx.subst)
  249. with Not_found ->
  250. Type.map (generic_substitute_type gctx) t
  251. let generic_substitute_expr gctx e =
  252. let vars = Hashtbl.create 0 in
  253. let build_var v =
  254. try
  255. Hashtbl.find vars v.v_id
  256. with Not_found ->
  257. let v2 = alloc_var v.v_name (generic_substitute_type gctx v.v_type) in
  258. v2.v_meta <- v.v_meta;
  259. Hashtbl.add vars v.v_id v2;
  260. v2
  261. in
  262. let rec build_expr e =
  263. match e.eexpr with
  264. | TField(e1, FInstance({cl_kind = KGeneric} as c,tl,cf)) ->
  265. let _, _, f = gctx.ctx.g.do_build_instance gctx.ctx (TClassDecl c) gctx.p in
  266. let t = f (List.map (generic_substitute_type gctx) tl) in
  267. build_expr {e with eexpr = TField(e1,quick_field t cf.cf_name)}
  268. | TTypeExpr (TClassDecl ({cl_kind = KTypeParameter _;} as c)) when Meta.has Meta.Const c.cl_meta ->
  269. let rec loop subst = match subst with
  270. | (t1,t2) :: subst ->
  271. begin match follow t1 with
  272. | TInst(c2,_) when c == c2 -> t2
  273. | _ -> loop subst
  274. end
  275. | [] -> raise Not_found
  276. in
  277. begin try
  278. let t = loop gctx.subst in
  279. begin match follow t with
  280. | TInst({cl_kind = KExpr e},_) -> type_expr gctx.ctx e Value
  281. | _ -> error "Only Const type parameters can be used as value" e.epos
  282. end
  283. with Not_found ->
  284. e
  285. end
  286. | _ ->
  287. map_expr_type build_expr (generic_substitute_type gctx) build_var e
  288. in
  289. build_expr e
  290. let has_ctor_constraint c = match c.cl_kind with
  291. | KTypeParameter tl ->
  292. List.exists (fun t -> match follow t with
  293. | TAnon a when PMap.mem "new" a.a_fields -> true
  294. | _ -> false
  295. ) tl;
  296. | _ -> false
  297. let get_short_name =
  298. let i = ref (-1) in
  299. (fun () ->
  300. incr i;
  301. Printf.sprintf "Hx___short___hx_type_%i" !i
  302. )
  303. let rec build_generic ctx c p tl =
  304. let pack = fst c.cl_path in
  305. let recurse = ref false in
  306. let rec check_recursive t =
  307. match follow t with
  308. | TInst (c2,tl) ->
  309. (match c2.cl_kind with
  310. | KTypeParameter tl ->
  311. if not (Typeload.is_generic_parameter ctx c2) && has_ctor_constraint c2 then
  312. error "Type parameters with a constructor cannot be used non-generically" p;
  313. recurse := true
  314. | _ -> ());
  315. List.iter check_recursive tl;
  316. | _ ->
  317. ()
  318. in
  319. List.iter check_recursive tl;
  320. if !recurse then begin
  321. TInst (c,tl) (* build a normal instance *)
  322. end else begin
  323. let gctx = make_generic ctx c.cl_params tl p in
  324. let name = (snd c.cl_path) ^ "_" ^ gctx.name in
  325. try
  326. Typeload.load_instance ctx { tpackage = pack; tname = name; tparams = []; tsub = None } p false
  327. with Error(Module_not_found path,_) when path = (pack,name) ->
  328. let m = (try Hashtbl.find ctx.g.modules (Hashtbl.find ctx.g.types_module c.cl_path) with Not_found -> assert false) in
  329. let ctx = { ctx with m = { ctx.m with module_types = m.m_types @ ctx.m.module_types } } in
  330. ignore(c.cl_build()); (* make sure the super class is already setup *)
  331. let mg = {
  332. m_id = alloc_mid();
  333. m_path = (pack,name);
  334. m_types = [];
  335. m_extra = module_extra (s_type_path (pack,name)) m.m_extra.m_sign 0. MFake;
  336. } in
  337. gctx.mg <- Some mg;
  338. let cg = mk_class mg (pack,name) c.cl_pos in
  339. mg.m_types <- [TClassDecl cg];
  340. Hashtbl.add ctx.g.modules mg.m_path mg;
  341. add_dependency mg m;
  342. add_dependency ctx.m.curmod mg;
  343. (* ensure that type parameters are set in dependencies *)
  344. let dep_stack = ref [] in
  345. let rec loop t =
  346. if not (List.memq t !dep_stack) then begin
  347. dep_stack := t :: !dep_stack;
  348. match t with
  349. | TInst (c,tl) -> add_dep c.cl_module tl
  350. | TEnum (e,tl) -> add_dep e.e_module tl
  351. | TType (t,tl) -> add_dep t.t_module tl
  352. | TAbstract (a,tl) -> add_dep a.a_module tl
  353. | TMono r ->
  354. (match !r with
  355. | None -> ()
  356. | Some t -> loop t)
  357. | TLazy f ->
  358. loop ((!f)());
  359. | TDynamic t2 ->
  360. if t == t2 then () else loop t2
  361. | TAnon a ->
  362. PMap.iter (fun _ f -> loop f.cf_type) a.a_fields
  363. | TFun (args,ret) ->
  364. List.iter (fun (_,_,t) -> loop t) args;
  365. loop ret
  366. end
  367. and add_dep m tl =
  368. add_dependency mg m;
  369. List.iter loop tl
  370. in
  371. List.iter loop tl;
  372. let build_field cf_old =
  373. let cf_new = {cf_old with cf_pos = cf_old.cf_pos} in (* copy *)
  374. let f () =
  375. let t = generic_substitute_type gctx cf_old.cf_type in
  376. ignore (follow t);
  377. begin try (match cf_old.cf_expr with
  378. | None ->
  379. begin match cf_old.cf_kind with
  380. | Method _ when not c.cl_interface && not c.cl_extern ->
  381. display_error ctx (Printf.sprintf "Field %s has no expression (possible typing order issue)" cf_new.cf_name) cf_new.cf_pos;
  382. display_error ctx (Printf.sprintf "While building %s" (s_type_path cg.cl_path)) p;
  383. | _ ->
  384. ()
  385. end
  386. | Some e ->
  387. cf_new.cf_expr <- Some (generic_substitute_expr gctx e)
  388. ) with Unify_error l ->
  389. error (error_msg (Unify l)) cf_new.cf_pos
  390. end;
  391. t
  392. in
  393. let r = exc_protect ctx (fun r ->
  394. let t = mk_mono() in
  395. r := (fun() -> t);
  396. unify_raise ctx (f()) t p;
  397. t
  398. ) "build_generic" in
  399. delay ctx PForce (fun() -> ignore ((!r)()));
  400. cf_new.cf_type <- TLazy r;
  401. cf_new
  402. in
  403. if c.cl_init <> None || c.cl_dynamic <> None then error "This class can't be generic" p;
  404. List.iter (fun cf -> match cf.cf_kind with
  405. | Method MethMacro when not ctx.in_macro -> ()
  406. | _ -> error "A generic class can't have static fields" cf.cf_pos
  407. ) c.cl_ordered_statics;
  408. cg.cl_super <- (match c.cl_super with
  409. | None -> None
  410. | Some (cs,pl) ->
  411. let find_class subst =
  412. let rec loop subst = match subst with
  413. | (TInst(c,[]),t) :: subst when c == cs -> t
  414. | _ :: subst -> loop subst
  415. | [] -> raise Not_found
  416. in
  417. try
  418. if pl <> [] then raise Not_found;
  419. let t = loop subst in
  420. (* extended type parameter: concrete type must have a constructor, but generic base class must not have one *)
  421. begin match follow t,c.cl_constructor with
  422. | TInst(cs,_),None ->
  423. ignore(cs.cl_build());
  424. begin match cs.cl_constructor with
  425. | None -> error ("Cannot use " ^ (s_type_path cs.cl_path) ^ " as type parameter because it is extended and has no constructor") p
  426. | _ -> ()
  427. end;
  428. | _,Some cf -> error "Generics extending type parameters cannot have constructors" cf.cf_pos
  429. | _ -> ()
  430. end;
  431. t
  432. with Not_found ->
  433. apply_params c.cl_params tl (TInst(cs,pl))
  434. in
  435. let ts = follow (find_class gctx.subst) in
  436. let cs,pl = Typeload.check_extends ctx c ts p in
  437. match cs.cl_kind with
  438. | KGeneric ->
  439. (match build_generic ctx cs p pl with
  440. | TInst (cs,pl) -> Some (cs,pl)
  441. | _ -> assert false)
  442. | _ -> Some(cs,pl)
  443. );
  444. Typeload.add_constructor ctx cg false p;
  445. cg.cl_kind <- KGenericInstance (c,tl);
  446. cg.cl_meta <- (Meta.NoDoc,[],p) :: cg.cl_meta;
  447. cg.cl_interface <- c.cl_interface;
  448. cg.cl_constructor <- (match cg.cl_constructor, c.cl_constructor, c.cl_super with
  449. | _, Some cf, _ -> Some (build_field cf)
  450. | Some ctor, _, _ -> Some ctor
  451. | None, None, None -> None
  452. | _ -> error "Please define a constructor for this class in order to use it as generic" c.cl_pos
  453. );
  454. cg.cl_implements <- List.map (fun (i,tl) ->
  455. (match follow (generic_substitute_type gctx (TInst (i, List.map (generic_substitute_type gctx) tl))) with
  456. | TInst (i,tl) -> i, tl
  457. | _ -> assert false)
  458. ) c.cl_implements;
  459. cg.cl_ordered_fields <- List.map (fun f ->
  460. let f = build_field f in
  461. cg.cl_fields <- PMap.add f.cf_name f cg.cl_fields;
  462. f
  463. ) c.cl_ordered_fields;
  464. (* In rare cases the class name can become too long, so let's shorten it (issue #3090). *)
  465. if String.length (snd cg.cl_path) > 254 then begin
  466. let n = get_short_name () in
  467. cg.cl_meta <- (Meta.Native,[EConst(String (n)),p],p) :: cg.cl_meta;
  468. end;
  469. TInst (cg,[])
  470. end
  471. (* -------------------------------------------------------------------------- *)
  472. (* HAXE.XML.PROXY *)
  473. let extend_xml_proxy ctx c t file p =
  474. let t = Typeload.load_complex_type ctx p t in
  475. let file = (try Common.find_file ctx.com file with Not_found -> file) in
  476. add_dependency c.cl_module (create_fake_module ctx file);
  477. let used = ref PMap.empty in
  478. let print_results() =
  479. PMap.iter (fun id used ->
  480. if not used then ctx.com.warning (id ^ " is not used") p;
  481. ) (!used)
  482. in
  483. let check_used = Common.defined ctx.com Define.CheckXmlProxy in
  484. if check_used then ctx.g.hook_generate <- print_results :: ctx.g.hook_generate;
  485. try
  486. let rec loop = function
  487. | Xml.Element (_,attrs,childs) ->
  488. (try
  489. let id = List.assoc "id" attrs in
  490. if PMap.mem id c.cl_fields then error ("Duplicate id " ^ id) p;
  491. let t = if not check_used then t else begin
  492. used := PMap.add id false (!used);
  493. let ft() = used := PMap.add id true (!used); t in
  494. TLazy (ref ft)
  495. end in
  496. let f = {
  497. cf_name = id;
  498. cf_type = t;
  499. cf_public = true;
  500. cf_pos = p;
  501. cf_doc = None;
  502. cf_meta = no_meta;
  503. cf_kind = Var { v_read = AccResolve; v_write = AccNo };
  504. cf_params = [];
  505. cf_expr = None;
  506. cf_overloads = [];
  507. } in
  508. c.cl_fields <- PMap.add id f c.cl_fields;
  509. with
  510. Not_found -> ());
  511. List.iter loop childs;
  512. | Xml.PCData _ -> ()
  513. in
  514. loop (Xml.parse_file file)
  515. with
  516. | Xml.Error e -> error ("XML error " ^ Xml.error e) p
  517. | Xml.File_not_found f -> error ("XML File not found : " ^ f) p
  518. (* -------------------------------------------------------------------------- *)
  519. (* BUILD META DATA OBJECT *)
  520. let build_metadata com t =
  521. let api = com.basic in
  522. let p, meta, fields, statics = (match t with
  523. | TClassDecl c ->
  524. let fields = List.map (fun f -> f.cf_name,f.cf_meta) (c.cl_ordered_fields @ (match c.cl_constructor with None -> [] | Some f -> [{ f with cf_name = "_" }])) in
  525. let statics = List.map (fun f -> f.cf_name,f.cf_meta) c.cl_ordered_statics in
  526. (c.cl_pos, ["",c.cl_meta],fields,statics)
  527. | TEnumDecl e ->
  528. (e.e_pos, ["",e.e_meta],List.map (fun n -> n, (PMap.find n e.e_constrs).ef_meta) e.e_names, [])
  529. | TTypeDecl t ->
  530. (t.t_pos, ["",t.t_meta],(match follow t.t_type with TAnon a -> PMap.fold (fun f acc -> (f.cf_name,f.cf_meta) :: acc) a.a_fields [] | _ -> []),[])
  531. | TAbstractDecl a ->
  532. (a.a_pos, ["",a.a_meta],[],[])
  533. ) in
  534. let filter l =
  535. let l = List.map (fun (n,ml) -> n, ExtList.List.filter_map (fun (m,el,p) -> match m with Meta.Custom s when String.length s > 0 && s.[0] <> ':' -> Some (s,el,p) | _ -> None) ml) l in
  536. List.filter (fun (_,ml) -> ml <> []) l
  537. in
  538. let meta, fields, statics = filter meta, filter fields, filter statics in
  539. let make_meta_field ml =
  540. let h = Hashtbl.create 0 in
  541. mk (TObjectDecl (List.map (fun (f,el,p) ->
  542. if Hashtbl.mem h f then error ("Duplicate metadata '" ^ f ^ "'") p;
  543. Hashtbl.add h f ();
  544. f, mk (match el with [] -> TConst TNull | _ -> TArrayDecl (List.map (type_constant_value com) el)) (api.tarray t_dynamic) p
  545. ) ml)) t_dynamic p
  546. in
  547. let make_meta l =
  548. mk (TObjectDecl (List.map (fun (f,ml) -> f,make_meta_field ml) l)) t_dynamic p
  549. in
  550. if meta = [] && fields = [] && statics = [] then
  551. None
  552. else
  553. let meta_obj = [] in
  554. let meta_obj = (if fields = [] then meta_obj else ("fields",make_meta fields) :: meta_obj) in
  555. let meta_obj = (if statics = [] then meta_obj else ("statics",make_meta statics) :: meta_obj) in
  556. let meta_obj = (try ("obj", make_meta_field (List.assoc "" meta)) :: meta_obj with Not_found -> meta_obj) in
  557. Some (mk (TObjectDecl meta_obj) t_dynamic p)
  558. (* -------------------------------------------------------------------------- *)
  559. (* MACRO TYPE *)
  560. let get_macro_path ctx e args p =
  561. let rec loop e =
  562. match fst e with
  563. | EField (e,f) -> f :: loop e
  564. | EConst (Ident i) -> [i]
  565. | _ -> error "Invalid macro call" p
  566. in
  567. let path = match e with
  568. | (EConst(Ident i)),_ ->
  569. let path = try
  570. if not (PMap.mem i ctx.curclass.cl_statics) then raise Not_found;
  571. ctx.curclass.cl_path
  572. with Not_found -> try
  573. (t_infos (fst (PMap.find i ctx.m.module_globals))).mt_path
  574. with Not_found ->
  575. error "Invalid macro call" p
  576. in
  577. i :: (snd path) :: (fst path)
  578. | _ ->
  579. loop e
  580. in
  581. (match path with
  582. | meth :: cl :: path -> (List.rev path,cl), meth, args
  583. | _ -> error "Invalid macro call" p)
  584. let build_macro_type ctx pl p =
  585. let path, field, args = (match pl with
  586. | [TInst ({ cl_kind = KExpr (ECall (e,args),_) },_)]
  587. | [TInst ({ cl_kind = KExpr (EArrayDecl [ECall (e,args),_],_) },_)] ->
  588. get_macro_path ctx e args p
  589. | _ ->
  590. error "MacroType requires a single expression call parameter" p
  591. ) in
  592. let old = ctx.ret in
  593. let t = (match ctx.g.do_macro ctx MMacroType path field args p with
  594. | None -> mk_mono()
  595. | Some _ -> ctx.ret
  596. ) in
  597. ctx.ret <- old;
  598. t
  599. let build_macro_build ctx c pl cfl p =
  600. let path, field, args = match Meta.get Meta.GenericBuild c.cl_meta with
  601. | _,[ECall(e,args),_],_ -> get_macro_path ctx e args p
  602. | _ -> error "genericBuild requires a single expression call parameter" p
  603. in
  604. let old = ctx.ret,ctx.g.get_build_infos in
  605. ctx.g.get_build_infos <- (fun() -> Some (TClassDecl c, pl, cfl));
  606. let t = (match ctx.g.do_macro ctx MMacroType path field args p with
  607. | None -> mk_mono()
  608. | Some _ -> ctx.ret
  609. ) in
  610. ctx.ret <- fst old;
  611. ctx.g.get_build_infos <- snd old;
  612. t
  613. (* -------------------------------------------------------------------------- *)
  614. (* API EVENTS *)
  615. let build_instance ctx mtype p =
  616. match mtype with
  617. | TClassDecl c ->
  618. if ctx.pass > PBuildClass then ignore(c.cl_build());
  619. let build f s =
  620. let r = exc_protect ctx (fun r ->
  621. let t = mk_mono() in
  622. r := (fun() -> t);
  623. let tf = (f()) in
  624. unify_raise ctx tf t p;
  625. link_dynamic t tf;
  626. t
  627. ) s in
  628. delay ctx PForce (fun() -> ignore ((!r)()));
  629. TLazy r
  630. in
  631. let ft = (fun pl ->
  632. match c.cl_kind with
  633. | KGeneric ->
  634. build (fun () -> build_generic ctx c p pl) "build_generic"
  635. | KMacroType ->
  636. build (fun () -> build_macro_type ctx pl p) "macro_type"
  637. | KGenericBuild cfl ->
  638. build (fun () -> build_macro_build ctx c pl cfl p) "generic_build"
  639. | _ ->
  640. TInst (c,pl)
  641. ) in
  642. c.cl_params , c.cl_path , ft
  643. | TEnumDecl e ->
  644. e.e_params , e.e_path , (fun t -> TEnum (e,t))
  645. | TTypeDecl t ->
  646. t.t_params , t.t_path , (fun tl -> TType(t,tl))
  647. | TAbstractDecl a ->
  648. a.a_params, a.a_path, (fun tl -> TAbstract(a,tl))
  649. let on_inherit ctx c p h =
  650. match h with
  651. | HExtends { tpackage = ["haxe";"remoting"]; tname = "Proxy"; tparams = [TPType(CTPath t)] } ->
  652. extend_remoting ctx c t p false true;
  653. false
  654. | HExtends { tpackage = ["haxe";"remoting"]; tname = "AsyncProxy"; tparams = [TPType(CTPath t)] } ->
  655. extend_remoting ctx c t p true true;
  656. false
  657. | HExtends { tpackage = ["haxe";"xml"]; tname = "Proxy"; tparams = [TPExpr(EConst (String file),p);TPType t] } ->
  658. extend_xml_proxy ctx c t file p;
  659. true
  660. | _ ->
  661. true
  662. let push_this ctx e = match e.eexpr with
  663. | TConst ((TInt _ | TFloat _ | TString _ | TBool _) as ct) ->
  664. (EConst (tconst_to_const ct),e.epos),fun () -> ()
  665. | _ ->
  666. ctx.this_stack <- e :: ctx.this_stack;
  667. let er = EMeta((Meta.This,[],e.epos), (EConst(Ident "this"),e.epos)),e.epos in
  668. er,fun () -> ctx.this_stack <- List.tl ctx.this_stack
  669. (* -------------------------------------------------------------------------- *)
  670. (* ABSTRACT CASTS *)
  671. module AbstractCast = struct
  672. let cast_stack = ref []
  673. let make_static_call ctx c cf a pl args t p =
  674. if cf.cf_kind = Method MethMacro then begin
  675. match args with
  676. | [e] ->
  677. let e,f = push_this ctx e in
  678. ctx.with_type_stack <- (WithType t) :: ctx.with_type_stack;
  679. let e = match ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [e] p with
  680. | Some e -> type_expr ctx e Value
  681. | None -> type_expr ctx (EConst (Ident "null"),p) Value
  682. in
  683. ctx.with_type_stack <- List.tl ctx.with_type_stack;
  684. f();
  685. e
  686. | _ -> assert false
  687. end else
  688. make_static_call ctx c cf (apply_params a.a_params pl) args t p
  689. let do_check_cast ctx tleft eright p =
  690. let recurse cf f =
  691. if cf == ctx.curfield || List.mem cf !cast_stack then error "Recursive implicit cast" p;
  692. cast_stack := cf :: !cast_stack;
  693. let r = f() in
  694. cast_stack := List.tl !cast_stack;
  695. r
  696. in
  697. let find a tl f =
  698. let tcf,cf = f() in
  699. if (Meta.has Meta.MultiType a.a_meta) then
  700. mk_cast eright tleft p
  701. else match a.a_impl with
  702. | Some c -> recurse cf (fun () ->
  703. let ret = make_static_call ctx c cf a tl [eright] tleft p in
  704. { ret with eexpr = TMeta( (Meta.ImplicitCast,[],ret.epos), ret) }
  705. )
  706. | None -> assert false
  707. in
  708. if type_iseq tleft eright.etype then
  709. eright
  710. else begin
  711. let rec loop tleft tright = match follow tleft,follow tright with
  712. | TAbstract(a1,tl1),TAbstract(a2,tl2) ->
  713. begin try find a2 tl2 (fun () -> Abstract.find_to a2 tl2 tleft)
  714. with Not_found -> try find a1 tl1 (fun () -> Abstract.find_from a1 tl1 eright.etype tleft)
  715. with Not_found -> raise Not_found
  716. end
  717. | TAbstract(a,tl),_ ->
  718. begin try find a tl (fun () -> Abstract.find_from a tl eright.etype tleft)
  719. with Not_found ->
  720. let rec loop2 tcl = match tcl with
  721. | tc :: tcl ->
  722. if not (type_iseq tc tleft) then loop (apply_params a.a_params tl tc) tright
  723. else loop2 tcl
  724. | [] -> raise Not_found
  725. in
  726. loop2 a.a_from
  727. end
  728. | _,TAbstract(a,tl) ->
  729. begin try find a tl (fun () -> Abstract.find_to a tl tleft)
  730. with Not_found ->
  731. let rec loop2 tcl = match tcl with
  732. | tc :: tcl ->
  733. if not (type_iseq tc tright) then loop tleft (apply_params a.a_params tl tc)
  734. else loop2 tcl
  735. | [] -> raise Not_found
  736. in
  737. loop2 a.a_to
  738. end
  739. | _ ->
  740. raise Not_found
  741. in
  742. loop tleft eright.etype
  743. end
  744. let cast_or_unify_raise ctx tleft eright p =
  745. try
  746. (* can't do that anymore because this might miss macro calls (#4315) *)
  747. (* if ctx.com.display <> DMNone then raise Not_found; *)
  748. do_check_cast ctx tleft eright p
  749. with Not_found ->
  750. unify_raise ctx eright.etype tleft p;
  751. eright
  752. let cast_or_unify ctx tleft eright p =
  753. try
  754. cast_or_unify_raise ctx tleft eright p
  755. with Error (Unify _ as err,_) ->
  756. if not ctx.untyped then display_error ctx (error_msg err) p;
  757. eright
  758. let find_array_access_raise ctx a pl e1 e2o p =
  759. let is_set = e2o <> None in
  760. let ta = apply_params a.a_params pl a.a_this in
  761. let rec loop cfl = match cfl with
  762. | [] -> raise Not_found
  763. | cf :: cfl ->
  764. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  765. let map t = apply_params a.a_params pl (apply_params cf.cf_params monos t) in
  766. let check_constraints () =
  767. List.iter2 (fun m (name,t) -> match follow t with
  768. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  769. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> Type.unify m (map tc) ) constr
  770. | _ -> ()
  771. ) monos cf.cf_params;
  772. in
  773. match follow (map cf.cf_type) with
  774. | TFun([(_,_,tab);(_,_,ta1);(_,_,ta2)],r) as tf when is_set ->
  775. begin try
  776. Type.unify tab ta;
  777. let e1 = cast_or_unify_raise ctx ta1 e1 p in
  778. let e2o = match e2o with None -> None | Some e2 -> Some (cast_or_unify_raise ctx ta2 e2 p) in
  779. check_constraints();
  780. cf,tf,r,e1,e2o
  781. with Unify_error _ | Error (Unify _,_) ->
  782. loop cfl
  783. end
  784. | TFun([(_,_,tab);(_,_,ta1)],r) as tf when not is_set ->
  785. begin try
  786. Type.unify tab ta;
  787. let e1 = cast_or_unify_raise ctx ta1 e1 p in
  788. check_constraints();
  789. cf,tf,r,e1,None
  790. with Unify_error _ | Error (Unify _,_) ->
  791. loop cfl
  792. end
  793. | _ -> loop cfl
  794. in
  795. loop a.a_array
  796. let find_array_access ctx a tl e1 e2o p =
  797. try find_array_access_raise ctx a tl e1 e2o p
  798. with Not_found -> match e2o with
  799. | None ->
  800. error (Printf.sprintf "No @:arrayAccess function accepts argument of %s" (s_type (print_context()) e1.etype)) p
  801. | Some e2 ->
  802. error (Printf.sprintf "No @:arrayAccess function accepts arguments of %s and %s" (s_type (print_context()) e1.etype) (s_type (print_context()) e2.etype)) p
  803. let find_multitype_specialization com a pl p =
  804. let m = mk_mono() in
  805. let tl = match Meta.get Meta.MultiType a.a_meta with
  806. | _,[],_ -> pl
  807. | _,el,_ ->
  808. let relevant = Hashtbl.create 0 in
  809. List.iter (fun e -> match fst e with
  810. | EConst(Ident s) -> Hashtbl.replace relevant s true
  811. | _ -> error "Type parameter expected" (pos e)
  812. ) el;
  813. let tl = List.map2 (fun (n,_) t -> if Hashtbl.mem relevant n || not (has_mono t) then t else t_dynamic) a.a_params pl in
  814. if com.platform = Js && a.a_path = ([],"Map") then begin match tl with
  815. | t1 :: _ ->
  816. let rec loop stack t =
  817. if List.exists (fun t2 -> fast_eq t t2) stack then
  818. t
  819. else begin
  820. let stack = t :: stack in
  821. match follow t with
  822. | TAbstract ({ a_path = [],"Class" },_) ->
  823. error (Printf.sprintf "Cannot use %s as key type to Map because Class<T> is not comparable" (s_type (print_context()) t1)) p;
  824. | TEnum(en,tl) ->
  825. PMap.iter (fun _ ef -> ignore(loop stack ef.ef_type)) en.e_constrs;
  826. Type.map (loop stack) t
  827. | t ->
  828. Type.map (loop stack) t
  829. end
  830. in
  831. ignore(loop [] t1)
  832. | _ -> assert false
  833. end;
  834. tl
  835. in
  836. let _,cf =
  837. try
  838. Abstract.find_to a tl m
  839. with Not_found ->
  840. let at = apply_params a.a_params pl a.a_this in
  841. let st = s_type (print_context()) at in
  842. if has_mono at then
  843. error ("Type parameters of multi type abstracts must be known (for " ^ st ^ ")") p
  844. else
  845. error ("Abstract " ^ (s_type_path a.a_path) ^ " has no @:to function that accepts " ^ st) p;
  846. in
  847. cf, follow m
  848. let handle_abstract_casts ctx e =
  849. let rec loop ctx e = match e.eexpr with
  850. | TNew({cl_kind = KAbstractImpl a} as c,pl,el) ->
  851. (* a TNew of an abstract implementation is only generated if it is a multi type abstract *)
  852. let cf,m = find_multitype_specialization ctx.com a pl e.epos in
  853. let e = make_static_call ctx c cf a pl ((mk (TConst TNull) (TAbstract(a,pl)) e.epos) :: el) m e.epos in
  854. {e with etype = m}
  855. | TCall({eexpr = TField(_,FStatic({cl_path=[],"Std"},{cf_name = "string"}))},[e1]) when (match follow e1.etype with TAbstract({a_impl = Some _},_) -> true | _ -> false) ->
  856. begin match follow e1.etype with
  857. | TAbstract({a_impl = Some c} as a,tl) ->
  858. begin try
  859. let cf = PMap.find "toString" c.cl_statics in
  860. make_static_call ctx c cf a tl [e1] ctx.t.tstring e.epos
  861. with Not_found ->
  862. e
  863. end
  864. | _ ->
  865. assert false
  866. end
  867. | TCall(e1, el) ->
  868. begin try
  869. let rec find_abstract e = match follow e.etype,e.eexpr with
  870. | TAbstract(a,pl),_ when Meta.has Meta.MultiType a.a_meta -> a,pl,e
  871. | _,TCast(e1,None) -> find_abstract e1
  872. | _ -> raise Not_found
  873. in
  874. let rec find_field e1 =
  875. match e1.eexpr with
  876. | TCast(e2,None) ->
  877. {e1 with eexpr = TCast(find_field e2,None)}
  878. | TField(e2,fa) ->
  879. let a,pl,e2 = find_abstract e2 in
  880. let m = Abstract.get_underlying_type a pl in
  881. let fname = field_name fa in
  882. let el = List.map (loop ctx) el in
  883. begin try
  884. let fa = quick_field m fname in
  885. let get_fun_type t = match follow t with
  886. | TFun(_,tr) as tf -> tf,tr
  887. | _ -> raise Not_found
  888. in
  889. let tf,tr = match fa with
  890. | FStatic(_,cf) -> get_fun_type cf.cf_type
  891. | FInstance(c,tl,cf) -> get_fun_type (apply_params c.cl_params tl cf.cf_type)
  892. | FAnon cf -> get_fun_type cf.cf_type
  893. | _ -> raise Not_found
  894. in
  895. let ef = mk (TField({e2 with etype = m},fa)) tf e2.epos in
  896. let ecall = make_call ctx ef el tr e.epos in
  897. if not (type_iseq ecall.etype e.etype) then
  898. mk (TCast(ecall,None)) e.etype e.epos
  899. else
  900. ecall
  901. with Not_found ->
  902. (* quick_field raises Not_found if m is an abstract, we have to replicate the 'using' call here *)
  903. match follow m with
  904. | TAbstract({a_impl = Some c} as a,pl) ->
  905. let cf = PMap.find fname c.cl_statics in
  906. make_static_call ctx c cf a pl (e2 :: el) e.etype e.epos
  907. | _ -> raise Not_found
  908. end
  909. | _ ->
  910. raise Not_found
  911. in
  912. find_field e1
  913. with Not_found ->
  914. Type.map_expr (loop ctx) e
  915. end
  916. | _ ->
  917. Type.map_expr (loop ctx) e
  918. in
  919. loop ctx e
  920. end
  921. module PatternMatchConversion = struct
  922. type cctx = {
  923. ctx : typer;
  924. mutable eval_stack : ((tvar * pos) * texpr) list list;
  925. dt_lookup : dt array;
  926. }
  927. let is_declared cctx v =
  928. let rec loop sl = match sl with
  929. | stack :: sl ->
  930. List.exists (fun ((v2,_),_) -> v == v2) stack || loop sl
  931. | [] ->
  932. false
  933. in
  934. loop cctx.eval_stack
  935. let group_cases cases =
  936. let dt_eq dt1 dt2 = match dt1,dt2 with
  937. | DTGoto i1, DTGoto i2 when i1 = i2 -> true
  938. (* TODO equal bindings *)
  939. | _ -> false
  940. in
  941. match List.rev cases with
  942. | [] -> []
  943. | [con,dt] -> [[con],dt]
  944. | (con,dt) :: cases ->
  945. let tmp,ldt,cases = List.fold_left (fun (tmp,ldt,acc) (con,dt) ->
  946. if dt_eq dt ldt then
  947. (con :: tmp,dt,acc)
  948. else
  949. ([con],dt,(tmp,ldt) :: acc)
  950. ) ([con],dt,[]) cases in
  951. match tmp with
  952. | [] -> cases
  953. | tmp -> ((tmp,ldt) :: cases)
  954. let replace_locals e =
  955. let v_known = ref IntMap.empty in
  956. let copy v =
  957. let v' = alloc_var v.v_name v.v_type in
  958. v_known := IntMap.add v.v_id v' !v_known;
  959. v'
  960. in
  961. let rec loop e = match e.eexpr with
  962. | TVar(v,e1) ->
  963. let v' = copy v in
  964. let e1 = match e1 with None -> None | Some e -> Some (loop e) in
  965. {e with eexpr = TVar(v',e1)}
  966. | TFor(v,e1,e2) ->
  967. let v' = copy v in
  968. let e1 = loop e1 in
  969. let e2 = loop e2 in
  970. {e with eexpr = TFor(v',e1,e2)}
  971. | TTry(e1,catches) ->
  972. let e1 = loop e1 in
  973. let catches = List.map (fun (v,e) ->
  974. let v' = copy v in
  975. let e = loop e in
  976. v',e
  977. ) catches in
  978. {e with eexpr = TTry(e1,catches)}
  979. | TLocal v ->
  980. let v' = try IntMap.find v.v_id !v_known with Not_found -> v in
  981. {e with eexpr = TLocal v'}
  982. | _ ->
  983. Type.map_expr loop e
  984. in
  985. loop e
  986. let rec convert_dt cctx dt =
  987. match dt with
  988. | DTBind (bl,dt) ->
  989. cctx.eval_stack <- bl :: cctx.eval_stack;
  990. let e = convert_dt cctx dt in
  991. cctx.eval_stack <- List.tl cctx.eval_stack;
  992. let vl,el = List.fold_left (fun (vl,el) ((v,p),e) ->
  993. if is_declared cctx v then
  994. vl, (mk (TBinop(OpAssign,mk (TLocal v) v.v_type p,e)) e.etype e.epos) :: el
  995. else
  996. ((v,p,Some e) :: vl), el
  997. ) ([],[e]) bl in
  998. let el_v = List.map (fun (v,p,eo) -> mk (TVar (v,eo)) cctx.ctx.t.tvoid p) vl in
  999. mk (TBlock (el_v @ el)) e.etype e.epos
  1000. | DTGoto i ->
  1001. convert_dt cctx (cctx.dt_lookup.(i))
  1002. | DTExpr e ->
  1003. e
  1004. | DTGuard(e,dt1,dt2) ->
  1005. let ethen = convert_dt cctx dt1 in
  1006. mk (TIf(e,ethen,match dt2 with None -> None | Some dt -> Some (convert_dt cctx dt))) ethen.etype (punion e.epos ethen.epos)
  1007. | DTSwitch({eexpr = TMeta((Meta.Exhaustive,_,_),_)},[_,dt],None) ->
  1008. convert_dt cctx dt
  1009. | DTSwitch(e_st,cl,dto) ->
  1010. let def = match dto with None -> None | Some dt -> Some (convert_dt cctx dt) in
  1011. let cases = group_cases cl in
  1012. let cases = List.map (fun (cl,dt) ->
  1013. let e = convert_dt cctx dt in
  1014. (* The macro interpreter does not care about unique locals and
  1015. we don't run the analyzer on the output, so let's save some
  1016. time here (issue #3937) *)
  1017. let e = if cctx.ctx.in_macro then e else replace_locals e in
  1018. cl,e
  1019. ) cases in
  1020. mk (TSwitch(e_st,cases,def)) (mk_mono()) e_st.epos
  1021. let to_typed_ast ctx dt p =
  1022. let first = dt.dt_dt_lookup.(dt.dt_first) in
  1023. let cctx = {
  1024. ctx = ctx;
  1025. dt_lookup = dt.dt_dt_lookup;
  1026. eval_stack = [];
  1027. } in
  1028. let e = convert_dt cctx first in
  1029. let e = { e with epos = p; etype = dt.dt_type} in
  1030. if dt.dt_var_init = [] then
  1031. e
  1032. else begin
  1033. let el_v = List.map (fun (v,eo) -> mk (TVar (v,eo)) cctx.ctx.t.tvoid p) dt.dt_var_init in
  1034. mk (TBlock (el_v @ [e])) dt.dt_type e.epos
  1035. end
  1036. end
  1037. (* -------------------------------------------------------------------------- *)
  1038. (* USAGE *)
  1039. let detect_usage com =
  1040. let usage = ref [] in
  1041. List.iter (fun t -> match t with
  1042. | TClassDecl c ->
  1043. let check_constructor c p =
  1044. try
  1045. let _,cf = get_constructor (fun cf -> cf.cf_type) c in
  1046. if Meta.has Meta.Usage cf.cf_meta then
  1047. usage := p :: !usage;
  1048. with Not_found ->
  1049. ()
  1050. in
  1051. let rec expr e = match e.eexpr with
  1052. | TField(_,FEnum(_,ef)) when Meta.has Meta.Usage ef.ef_meta ->
  1053. let p = {e.epos with pmin = e.epos.pmax - (String.length ef.ef_name)} in
  1054. usage := p :: !usage;
  1055. Type.iter expr e
  1056. | TField(_,(FAnon cf | FInstance (_,_,cf) | FStatic (_,cf) | FClosure (_,cf))) when Meta.has Meta.Usage cf.cf_meta ->
  1057. let p = {e.epos with pmin = e.epos.pmax - (String.length cf.cf_name)} in
  1058. usage := p :: !usage;
  1059. Type.iter expr e
  1060. | TLocal v when Meta.has Meta.Usage v.v_meta ->
  1061. usage := e.epos :: !usage
  1062. | TVar (v,_) when com.display = DMPosition && Meta.has Meta.Usage v.v_meta ->
  1063. raise (Typecore.DisplayPosition [e.epos])
  1064. | TFunction tf when com.display = DMPosition && List.exists (fun (v,_) -> Meta.has Meta.Usage v.v_meta) tf.tf_args ->
  1065. raise (Typecore.DisplayPosition [e.epos])
  1066. | TTypeExpr mt when (Meta.has Meta.Usage (t_infos mt).mt_meta) ->
  1067. usage := e.epos :: !usage
  1068. | TNew (c,_,_) ->
  1069. check_constructor c e.epos;
  1070. Type.iter expr e;
  1071. | TCall({eexpr = TConst TSuper},_) ->
  1072. begin match c.cl_super with
  1073. | Some (c,_) ->
  1074. check_constructor c e.epos
  1075. | _ ->
  1076. ()
  1077. end
  1078. | _ -> Type.iter expr e
  1079. in
  1080. let field cf = ignore(follow cf.cf_type); match cf.cf_expr with None -> () | Some e -> expr e in
  1081. (match c.cl_constructor with None -> () | Some cf -> field cf);
  1082. (match c.cl_init with None -> () | Some e -> expr e);
  1083. List.iter field c.cl_ordered_statics;
  1084. List.iter field c.cl_ordered_fields;
  1085. | _ -> ()
  1086. ) com.types;
  1087. let usage = List.sort (fun p1 p2 ->
  1088. let c = compare p1.pfile p2.pfile in
  1089. if c <> 0 then c else compare p1.pmin p2.pmin
  1090. ) !usage in
  1091. raise (Typecore.DisplayPosition usage)
  1092. let update_cache_dependencies com =
  1093. let rec check_t m t = match t with
  1094. | TInst(c,tl) ->
  1095. add_dependency m c.cl_module;
  1096. List.iter (check_t m) tl;
  1097. | TEnum(en,tl) ->
  1098. add_dependency m en.e_module;
  1099. List.iter (check_t m) tl;
  1100. | TType(t,tl) ->
  1101. add_dependency m t.t_module;
  1102. List.iter (check_t m) tl;
  1103. | TAbstract(a,tl) ->
  1104. add_dependency m a.a_module;
  1105. List.iter (check_t m) tl;
  1106. | TFun(targs,tret) ->
  1107. List.iter (fun (_,_,t) -> check_t m t) targs;
  1108. check_t m tret;
  1109. | TAnon an ->
  1110. PMap.iter (fun _ cf -> check_field m cf) an.a_fields
  1111. | TMono r ->
  1112. (match !r with
  1113. | Some t -> check_t m t
  1114. | _ -> ())
  1115. | TLazy f ->
  1116. check_t m (!f())
  1117. | TDynamic t ->
  1118. if t == t_dynamic then
  1119. ()
  1120. else
  1121. check_t m t
  1122. and check_field m cf =
  1123. check_t m cf.cf_type
  1124. in
  1125. List.iter (fun t -> match t with
  1126. | TClassDecl c ->
  1127. List.iter (check_field c.cl_module) c.cl_ordered_statics;
  1128. List.iter (check_field c.cl_module) c.cl_ordered_fields;
  1129. (match c.cl_constructor with None -> () | Some cf -> check_field c.cl_module cf);
  1130. | _ ->
  1131. ()
  1132. ) com.types
  1133. (* -------------------------------------------------------------------------- *)
  1134. (* STACK MANAGEMENT EMULATION *)
  1135. type stack_context = {
  1136. stack_var : string;
  1137. stack_exc_var : string;
  1138. stack_pos_var : string;
  1139. stack_pos : pos;
  1140. stack_expr : texpr;
  1141. stack_pop : texpr;
  1142. stack_save_pos : texpr;
  1143. stack_restore : texpr list;
  1144. stack_push : tclass -> string -> texpr;
  1145. stack_return : texpr -> texpr;
  1146. }
  1147. let stack_context_init com stack_var exc_var pos_var tmp_var use_add p =
  1148. let t = com.basic in
  1149. let st = t.tarray t.tstring in
  1150. let stack_var = alloc_var stack_var st in
  1151. let exc_var = alloc_var exc_var st in
  1152. let pos_var = alloc_var pos_var t.tint in
  1153. let stack_e = mk (TLocal stack_var) st p in
  1154. let exc_e = mk (TLocal exc_var) st p in
  1155. let stack_pop = fcall stack_e "pop" [] t.tstring p in
  1156. let stack_push c m =
  1157. fcall stack_e "push" [
  1158. if use_add then
  1159. binop OpAdd (string com (s_type_path c.cl_path ^ "::") p) (string com m p) t.tstring p
  1160. else
  1161. string com (s_type_path c.cl_path ^ "::" ^ m) p
  1162. ] t.tvoid p
  1163. in
  1164. let stack_return e =
  1165. let tmp = alloc_var tmp_var e.etype in
  1166. mk (TBlock [
  1167. mk (TVar (tmp, Some e)) t.tvoid e.epos;
  1168. stack_pop;
  1169. mk (TReturn (Some (mk (TLocal tmp) e.etype e.epos))) e.etype e.epos
  1170. ]) e.etype e.epos
  1171. in
  1172. {
  1173. stack_var = stack_var.v_name;
  1174. stack_exc_var = exc_var.v_name;
  1175. stack_pos_var = pos_var.v_name;
  1176. stack_pos = p;
  1177. stack_expr = stack_e;
  1178. stack_pop = stack_pop;
  1179. stack_save_pos = mk (TVar (pos_var, Some (field stack_e "length" t.tint p))) t.tvoid p;
  1180. stack_push = stack_push;
  1181. stack_return = stack_return;
  1182. stack_restore = [
  1183. binop OpAssign exc_e (mk (TArrayDecl []) st p) st p;
  1184. mk (TWhile (
  1185. mk_parent (binop OpGte (field stack_e "length" t.tint p) (mk (TLocal pos_var) t.tint p) t.tbool p),
  1186. fcall exc_e "unshift" [fcall stack_e "pop" [] t.tstring p] t.tvoid p,
  1187. NormalWhile
  1188. )) t.tvoid p;
  1189. fcall stack_e "push" [index com exc_e 0 t.tstring p] t.tvoid p
  1190. ];
  1191. }
  1192. let stack_init com use_add =
  1193. stack_context_init com "$s" "$e" "$spos" "$tmp" use_add null_pos
  1194. let rec stack_block_loop ctx e =
  1195. match e.eexpr with
  1196. | TFunction _ ->
  1197. e
  1198. | TReturn None | TReturn (Some { eexpr = TConst _ }) | TReturn (Some { eexpr = TLocal _ }) ->
  1199. mk (TBlock [
  1200. ctx.stack_pop;
  1201. e;
  1202. ]) e.etype e.epos
  1203. | TReturn (Some e) ->
  1204. ctx.stack_return (stack_block_loop ctx e)
  1205. | TTry (v,cases) ->
  1206. let v = stack_block_loop ctx v in
  1207. let cases = List.map (fun (v,e) ->
  1208. let e = stack_block_loop ctx e in
  1209. let e = (match (mk_block e).eexpr with
  1210. | TBlock l -> mk (TBlock (ctx.stack_restore @ l)) e.etype e.epos
  1211. | _ -> assert false
  1212. ) in
  1213. v , e
  1214. ) cases in
  1215. mk (TTry (v,cases)) e.etype e.epos
  1216. | _ ->
  1217. map_expr (stack_block_loop ctx) e
  1218. let stack_block ctx c m e =
  1219. match (mk_block e).eexpr with
  1220. | TBlock l ->
  1221. mk (TBlock (
  1222. ctx.stack_push c m ::
  1223. ctx.stack_save_pos ::
  1224. List.map (stack_block_loop ctx) l
  1225. @ [ctx.stack_pop]
  1226. )) e.etype e.epos
  1227. | _ ->
  1228. assert false
  1229. (* -------------------------------------------------------------------------- *)
  1230. (* FIX OVERRIDES *)
  1231. (*
  1232. on some platforms which doesn't support type parameters, we must have the
  1233. exact same type for overriden/implemented function as the original one
  1234. *)
  1235. let rec find_field com c f =
  1236. try
  1237. (match c.cl_super with
  1238. | None ->
  1239. raise Not_found
  1240. | Some ( {cl_path = (["cpp"],"FastIterator")}, _ ) ->
  1241. raise Not_found (* This is a strongly typed 'extern' and the usual rules don't apply *)
  1242. | Some (c,_) ->
  1243. find_field com c f)
  1244. with Not_found -> try
  1245. if com.platform = Cpp then (* Cpp uses delegation for interfaces *)
  1246. raise Not_found;
  1247. let rec loop = function
  1248. | [] ->
  1249. raise Not_found
  1250. | (c,_) :: l ->
  1251. try
  1252. find_field com c f
  1253. with
  1254. Not_found -> loop l
  1255. in
  1256. loop c.cl_implements
  1257. with Not_found ->
  1258. let f = PMap.find f.cf_name c.cl_fields in
  1259. (match f.cf_kind with Var { v_read = AccRequire _ } -> raise Not_found | _ -> ());
  1260. f
  1261. let fix_override com c f fd =
  1262. let f2 = (try Some (find_field com c f) with Not_found -> None) in
  1263. match f2,fd with
  1264. | Some (f2), Some(fd) ->
  1265. let targs, tret = (match follow f2.cf_type with TFun (args,ret) -> args, ret | _ -> assert false) in
  1266. let changed_args = ref [] in
  1267. let prefix = "_tmp_" in
  1268. let nargs = List.map2 (fun ((v,ct) as cur) (_,_,t2) ->
  1269. try
  1270. type_eq EqStrict (monomorphs c.cl_params (monomorphs f.cf_params v.v_type)) t2;
  1271. (* Flash generates type parameters with a single constraint as that constraint type, so we
  1272. have to detect this case and change the variable (issue #2712). *)
  1273. begin match follow v.v_type with
  1274. | TInst({cl_kind = KTypeParameter [tc]} as cp,_) when com.platform = Flash ->
  1275. if List.mem_assoc (snd cp.cl_path) c.cl_params then raise (Unify_error [])
  1276. | _ ->
  1277. ()
  1278. end;
  1279. cur
  1280. with Unify_error _ ->
  1281. let v2 = alloc_var (prefix ^ v.v_name) t2 in
  1282. changed_args := (v,v2) :: !changed_args;
  1283. v2,ct
  1284. ) fd.tf_args targs in
  1285. let fd2 = {
  1286. tf_args = nargs;
  1287. tf_type = tret;
  1288. tf_expr = (match List.rev !changed_args with
  1289. | [] -> fd.tf_expr
  1290. | args ->
  1291. let e = fd.tf_expr in
  1292. let el = (match e.eexpr with TBlock el -> el | _ -> [e]) in
  1293. let p = (match el with [] -> e.epos | e :: _ -> e.epos) in
  1294. let el_v = List.map (fun (v,v2) ->
  1295. mk (TVar (v,Some (mk (TCast (mk (TLocal v2) v2.v_type p,None)) v.v_type p))) com.basic.tvoid p
  1296. ) args in
  1297. { e with eexpr = TBlock (el_v @ el) }
  1298. );
  1299. } in
  1300. (* as3 does not allow wider visibility, so the base method has to be made public *)
  1301. if Common.defined com Define.As3 && f.cf_public then f2.cf_public <- true;
  1302. let targs = List.map (fun(v,c) -> (v.v_name, Option.is_some c, v.v_type)) nargs in
  1303. let fde = (match f.cf_expr with None -> assert false | Some e -> e) in
  1304. f.cf_expr <- Some { fde with eexpr = TFunction fd2 };
  1305. f.cf_type <- TFun(targs,tret);
  1306. | Some(f2), None when c.cl_interface ->
  1307. let targs, tret = (match follow f2.cf_type with TFun (args,ret) -> args, ret | _ -> assert false) in
  1308. f.cf_type <- TFun(targs,tret)
  1309. | _ ->
  1310. ()
  1311. let fix_overrides com t =
  1312. match t with
  1313. | TClassDecl c ->
  1314. (* overrides can be removed from interfaces *)
  1315. if c.cl_interface then
  1316. c.cl_ordered_fields <- List.filter (fun f ->
  1317. try
  1318. if find_field com c f == f then raise Not_found;
  1319. c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  1320. false;
  1321. with Not_found ->
  1322. true
  1323. ) c.cl_ordered_fields;
  1324. List.iter (fun f ->
  1325. match f.cf_expr, f.cf_kind with
  1326. | Some { eexpr = TFunction fd }, Method (MethNormal | MethInline) ->
  1327. fix_override com c f (Some fd)
  1328. | None, Method (MethNormal | MethInline) when c.cl_interface ->
  1329. fix_override com c f None
  1330. | _ ->
  1331. ()
  1332. ) c.cl_ordered_fields
  1333. | _ ->
  1334. ()
  1335. (*
  1336. PHP does not allow abstract classes extending other abstract classes to override any fields, so these duplicates
  1337. must be removed from the child interface
  1338. *)
  1339. let fix_abstract_inheritance com t =
  1340. match t with
  1341. | TClassDecl c when c.cl_interface ->
  1342. c.cl_ordered_fields <- List.filter (fun f ->
  1343. let b = try (find_field com c f) == f
  1344. with Not_found -> false in
  1345. if not b then c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  1346. b;
  1347. ) c.cl_ordered_fields
  1348. | _ -> ()
  1349. (* -------------------------------------------------------------------------- *)
  1350. (* MISC FEATURES *)
  1351. let rec is_volatile t =
  1352. match t with
  1353. | TMono r ->
  1354. (match !r with
  1355. | Some t -> is_volatile t
  1356. | _ -> false)
  1357. | TLazy f ->
  1358. is_volatile (!f())
  1359. | TType (t,tl) ->
  1360. (match t.t_path with
  1361. | _ -> is_volatile (apply_params t.t_params tl t.t_type))
  1362. | _ ->
  1363. false
  1364. let set_default ctx a c p =
  1365. let t = a.v_type in
  1366. let ve = mk (TLocal a) t p in
  1367. let cond = TBinop (OpEq,ve,mk (TConst TNull) t p) in
  1368. mk (TIf (mk_parent (mk cond ctx.basic.tbool p), mk (TBinop (OpAssign,ve,mk (TConst c) t p)) t p,None)) ctx.basic.tvoid p
  1369. let bytes_serialize data =
  1370. let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" in
  1371. let tbl = Array.init (String.length b64) (fun i -> String.get b64 i) in
  1372. Base64.str_encode ~tbl data
  1373. (*
  1374. Tells if the constructor might be called without any issue whatever its parameters
  1375. *)
  1376. let rec constructor_side_effects e =
  1377. match e.eexpr with
  1378. | TBinop (op,_,_) when op <> OpAssign ->
  1379. true
  1380. | TField (_,FEnum _) ->
  1381. false
  1382. | TUnop _ | TArray _ | TField _ | TEnumParameter _ | TCall _ | TNew _ | TFor _ | TWhile _ | TSwitch _ | TReturn _ | TThrow _ ->
  1383. true
  1384. | TBinop _ | TTry _ | TIf _ | TBlock _ | TVar _
  1385. | TFunction _ | TArrayDecl _ | TObjectDecl _
  1386. | TParenthesis _ | TTypeExpr _ | TLocal _ | TMeta _
  1387. | TConst _ | TContinue | TBreak | TCast _ ->
  1388. try
  1389. Type.iter (fun e -> if constructor_side_effects e then raise Exit) e;
  1390. false;
  1391. with Exit ->
  1392. true
  1393. let make_valid_filename s =
  1394. let r = Str.regexp "[^A-Za-z0-9_\\-\\.,]" in
  1395. Str.global_substitute r (fun s -> "_") s
  1396. (*
  1397. Make a dump of the full typed AST of all types
  1398. *)
  1399. let rec create_dumpfile acc = function
  1400. | [] -> assert false
  1401. | d :: [] ->
  1402. let d = make_valid_filename d in
  1403. let ch = open_out (String.concat "/" (List.rev (d :: acc)) ^ ".dump") in
  1404. let buf = Buffer.create 0 in
  1405. buf, (fun () ->
  1406. output_string ch (Buffer.contents buf);
  1407. close_out ch)
  1408. | d :: l ->
  1409. let dir = String.concat "/" (List.rev (d :: acc)) in
  1410. if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
  1411. create_dumpfile (d :: acc) l
  1412. let dump_types com =
  1413. let s_type = s_type (Type.print_context()) in
  1414. let params = function [] -> "" | l -> Printf.sprintf "<%s>" (String.concat "," (List.map (fun (n,t) -> n ^ " : " ^ s_type t) l)) in
  1415. let s_expr = match Common.defined_value_safe com Define.Dump with
  1416. | "pretty" -> Type.s_expr_pretty "\t"
  1417. | "legacy" -> Type.s_expr
  1418. | _ -> Type.s_expr_ast (not (Common.defined com Define.DumpIgnoreVarIds)) "\t"
  1419. in
  1420. List.iter (fun mt ->
  1421. let path = Type.t_path mt in
  1422. let buf,close = create_dumpfile [] ("dump" :: (Common.platform_name com.platform) :: fst path @ [snd path]) in
  1423. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1424. (match mt with
  1425. | Type.TClassDecl c ->
  1426. let rec print_field stat f =
  1427. print "\t%s%s%s%s" (if stat then "static " else "") (if f.cf_public then "public " else "") f.cf_name (params f.cf_params);
  1428. print "(%s) : %s" (s_kind f.cf_kind) (s_type f.cf_type);
  1429. (match f.cf_expr with
  1430. | None -> ()
  1431. | Some e -> print "\n\n\t = %s" (s_expr s_type e));
  1432. print "\n\n";
  1433. List.iter (fun f -> print_field stat f) f.cf_overloads
  1434. in
  1435. print "%s%s%s %s%s" (if c.cl_private then "private " else "") (if c.cl_extern then "extern " else "") (if c.cl_interface then "interface" else "class") (s_type_path path) (params c.cl_params);
  1436. (match c.cl_super with None -> () | Some (c,pl) -> print " extends %s" (s_type (TInst (c,pl))));
  1437. List.iter (fun (c,pl) -> print " implements %s" (s_type (TInst (c,pl)))) c.cl_implements;
  1438. (match c.cl_dynamic with None -> () | Some t -> print " implements Dynamic<%s>" (s_type t));
  1439. (match c.cl_array_access with None -> () | Some t -> print " implements ArrayAccess<%s>" (s_type t));
  1440. print "{\n";
  1441. (match c.cl_constructor with
  1442. | None -> ()
  1443. | Some f -> print_field false f);
  1444. List.iter (print_field false) c.cl_ordered_fields;
  1445. List.iter (print_field true) c.cl_ordered_statics;
  1446. (match c.cl_init with
  1447. | None -> ()
  1448. | Some e ->
  1449. print "\n\n\t__init__ = ";
  1450. print "%s" (s_expr s_type e);
  1451. print "}\n");
  1452. print "}";
  1453. | Type.TEnumDecl e ->
  1454. print "%s%senum %s%s {\n" (if e.e_private then "private " else "") (if e.e_extern then "extern " else "") (s_type_path path) (params e.e_params);
  1455. List.iter (fun n ->
  1456. let f = PMap.find n e.e_constrs in
  1457. print "\t%s : %s;\n" f.ef_name (s_type f.ef_type);
  1458. ) e.e_names;
  1459. print "}"
  1460. | Type.TTypeDecl t ->
  1461. print "%stype %s%s = %s" (if t.t_private then "private " else "") (s_type_path path) (params t.t_params) (s_type t.t_type);
  1462. | Type.TAbstractDecl a ->
  1463. print "%sabstract %s%s {}" (if a.a_private then "private " else "") (s_type_path path) (params a.a_params);
  1464. );
  1465. close();
  1466. ) com.types
  1467. let dump_dependencies com =
  1468. let buf,close = create_dumpfile [] ["dump";Common.platform_name com.platform;".dependencies"] in
  1469. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1470. let dep = Hashtbl.create 0 in
  1471. List.iter (fun m ->
  1472. print "%s:\n" m.m_extra.m_file;
  1473. PMap.iter (fun _ m2 ->
  1474. print "\t%s\n" (m2.m_extra.m_file);
  1475. let l = try Hashtbl.find dep m2.m_extra.m_file with Not_found -> [] in
  1476. Hashtbl.replace dep m2.m_extra.m_file (m :: l)
  1477. ) m.m_extra.m_deps;
  1478. ) com.Common.modules;
  1479. close();
  1480. let buf,close = create_dumpfile [] ["dump";Common.platform_name com.platform;".dependants"] in
  1481. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1482. Hashtbl.iter (fun n ml ->
  1483. print "%s:\n" n;
  1484. List.iter (fun m ->
  1485. print "\t%s\n" (m.m_extra.m_file);
  1486. ) ml;
  1487. ) dep;
  1488. close()
  1489. (*
  1490. Build a default safe-cast expression :
  1491. { var $t = <e>; if( Std.is($t,<t>) ) $t else throw "Class cast error"; }
  1492. *)
  1493. let default_cast ?(vtmp="$t") com e texpr t p =
  1494. let api = com.basic in
  1495. let mk_texpr = function
  1496. | TClassDecl c -> TAnon { a_fields = PMap.empty; a_status = ref (Statics c) }
  1497. | TEnumDecl e -> TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics e) }
  1498. | TAbstractDecl a -> TAnon { a_fields = PMap.empty; a_status = ref (AbstractStatics a) }
  1499. | TTypeDecl _ -> assert false
  1500. in
  1501. let vtmp = alloc_var vtmp e.etype in
  1502. let var = mk (TVar (vtmp,Some e)) api.tvoid p in
  1503. let vexpr = mk (TLocal vtmp) e.etype p in
  1504. let texpr = mk (TTypeExpr texpr) (mk_texpr texpr) p in
  1505. let std = (try List.find (fun t -> t_path t = ([],"Std")) com.types with Not_found -> assert false) in
  1506. let fis = (try
  1507. let c = (match std with TClassDecl c -> c | _ -> assert false) in
  1508. FStatic (c, PMap.find "is" c.cl_statics)
  1509. with Not_found ->
  1510. assert false
  1511. ) in
  1512. let std = mk (TTypeExpr std) (mk_texpr std) p in
  1513. let is = mk (TField (std,fis)) (tfun [t_dynamic;t_dynamic] api.tbool) p in
  1514. let is = mk (TCall (is,[vexpr;texpr])) api.tbool p in
  1515. let exc = mk (TThrow (mk (TConst (TString "Class cast error")) api.tstring p)) t p in
  1516. let check = mk (TIf (mk_parent is,mk (TCast (vexpr,None)) t p,Some exc)) t p in
  1517. mk (TBlock [var;check;vexpr]) t p
  1518. (** Overload resolution **)
  1519. module Overloads =
  1520. struct
  1521. let rec simplify_t t = match t with
  1522. | TAbstract(a,_) when Meta.has Meta.CoreType a.a_meta ->
  1523. t
  1524. | TInst _ | TEnum _ ->
  1525. t
  1526. | TAbstract(a,tl) -> simplify_t (Abstract.get_underlying_type a tl)
  1527. | TType(({ t_path = [],"Null" } as t), [t2]) -> (match simplify_t t2 with
  1528. | (TAbstract(a,_) as t2) when Meta.has Meta.CoreType a.a_meta ->
  1529. TType(t, [simplify_t t2])
  1530. | (TEnum _ as t2) ->
  1531. TType(t, [simplify_t t2])
  1532. | t2 -> t2)
  1533. | TType(t, tl) ->
  1534. simplify_t (apply_params t.t_params tl t.t_type)
  1535. | TMono r -> (match !r with
  1536. | Some t -> simplify_t t
  1537. | None -> t_dynamic)
  1538. | TAnon _ -> t_dynamic
  1539. | TDynamic _ -> t
  1540. | TLazy f -> simplify_t (!f())
  1541. | TFun _ -> t
  1542. (* rate type parameters *)
  1543. let rate_tp tlfun tlarg =
  1544. let acc = ref 0 in
  1545. List.iter2 (fun f a -> if not (type_iseq f a) then incr acc) tlfun tlarg;
  1546. !acc
  1547. (**
  1548. The rate function returns an ( int * int ) type.
  1549. The smaller the int, the best rated the caller argument is in comparison with the callee.
  1550. The first int refers to how many "conversions" would be necessary to convert from the callee to the caller type, and
  1551. the second refers to the type parameters.
  1552. **)
  1553. let rec rate_conv cacc tfun targ =
  1554. match simplify_t tfun, simplify_t targ with
  1555. | TInst({ cl_interface = true } as cf, tlf), TInst(ca, tla) ->
  1556. (* breadth-first *)
  1557. let stack = ref [0,ca,tla] in
  1558. let cur = ref (0, ca,tla) in
  1559. let rec loop () =
  1560. match !stack with
  1561. | [] -> (let acc, ca, tla = !cur in match ca.cl_super with
  1562. | None -> raise Not_found
  1563. | Some (sup,tls) ->
  1564. cur := (acc+1,sup,List.map (apply_params ca.cl_params tla) tls);
  1565. stack := [!cur];
  1566. loop())
  1567. | (acc,ca,tla) :: _ when ca == cf ->
  1568. acc,tla
  1569. | (acc,ca,tla) :: s ->
  1570. stack := s @ List.map (fun (c,tl) -> (acc+1,c,List.map (apply_params ca.cl_params tla) tl)) ca.cl_implements;
  1571. loop()
  1572. in
  1573. let acc, tla = loop() in
  1574. (cacc + acc, rate_tp tlf tla)
  1575. | TInst(cf,tlf), TInst(ca,tla) ->
  1576. let rec loop acc ca tla =
  1577. if cf == ca then
  1578. acc, tla
  1579. else match ca.cl_super with
  1580. | None -> raise Not_found
  1581. | Some(sup,stl) ->
  1582. loop (acc+1) sup (List.map (apply_params ca.cl_params tla) stl)
  1583. in
  1584. let acc, tla = loop 0 ca tla in
  1585. (cacc + acc, rate_tp tlf tla)
  1586. | TEnum(ef,tlf), TEnum(ea, tla) ->
  1587. if ef != ea then raise Not_found;
  1588. (cacc, rate_tp tlf tla)
  1589. | TDynamic _, TDynamic _ ->
  1590. (cacc, 0)
  1591. | TDynamic _, _ ->
  1592. (max_int, 0) (* a function with dynamic will always be worst of all *)
  1593. | TAbstract(a, _), TDynamic _ when Meta.has Meta.CoreType a.a_meta ->
  1594. (cacc + 2, 0) (* a dynamic to a basic type will have an "unboxing" penalty *)
  1595. | _, TDynamic _ ->
  1596. (cacc + 1, 0)
  1597. | TAbstract(af,tlf), TAbstract(aa,tla) ->
  1598. (if af == aa then
  1599. (cacc, rate_tp tlf tla)
  1600. else
  1601. let ret = ref None in
  1602. if List.exists (fun t -> try
  1603. ret := Some (rate_conv (cacc+1) (apply_params af.a_params tlf t) targ);
  1604. true
  1605. with | Not_found ->
  1606. false
  1607. ) af.a_from then
  1608. Option.get !ret
  1609. else
  1610. if List.exists (fun t -> try
  1611. ret := Some (rate_conv (cacc+1) tfun (apply_params aa.a_params tla t));
  1612. true
  1613. with | Not_found ->
  1614. false
  1615. ) aa.a_to then
  1616. Option.get !ret
  1617. else
  1618. raise Not_found)
  1619. | TType({ t_path = [], "Null" }, [tf]), TType({ t_path = [], "Null" }, [ta]) ->
  1620. rate_conv (cacc+0) tf ta
  1621. | TType({ t_path = [], "Null" }, [tf]), ta ->
  1622. rate_conv (cacc+1) tf ta
  1623. | tf, TType({ t_path = [], "Null" }, [ta]) ->
  1624. rate_conv (cacc+1) tf ta
  1625. | TFun _, TFun _ -> (* unify will make sure they are compatible *)
  1626. cacc,0
  1627. | tfun,targ ->
  1628. raise Not_found
  1629. let is_best arg1 arg2 =
  1630. (List.for_all2 (fun v1 v2 ->
  1631. v1 <= v2)
  1632. arg1 arg2) && (List.exists2 (fun v1 v2 ->
  1633. v1 < v2)
  1634. arg1 arg2)
  1635. let rec rm_duplicates acc ret = match ret with
  1636. | [] -> acc
  1637. | ( el, t, _ ) :: ret when List.exists (fun (_,t2,_) -> type_iseq t t2) acc ->
  1638. rm_duplicates acc ret
  1639. | r :: ret ->
  1640. rm_duplicates (r :: acc) ret
  1641. let s_options rated =
  1642. String.concat ",\n" (List.map (fun ((elist,t,_),rate) ->
  1643. "( " ^ (String.concat "," (List.map (fun(e,_) -> s_expr (s_type (print_context())) e) elist)) ^ " ) => " ^
  1644. "( " ^ (String.concat "," (List.map (fun (i,i2) -> string_of_int i ^ ":" ^ string_of_int i2) rate)) ^ " ) => " ^ (s_type (print_context()) t)
  1645. ) rated)
  1646. let count_optionals elist =
  1647. List.fold_left (fun acc (_,is_optional) -> if is_optional then acc + 1 else acc) 0 elist
  1648. let rec fewer_optionals acc compatible = match acc, compatible with
  1649. | _, [] -> acc
  1650. | [], c :: comp -> fewer_optionals [c] comp
  1651. | (elist_acc, _, _) :: _, ((elist, _, _) as cur) :: comp ->
  1652. let acc_opt = count_optionals elist_acc in
  1653. let comp_opt = count_optionals elist in
  1654. if acc_opt = comp_opt then
  1655. fewer_optionals (cur :: acc) comp
  1656. else if acc_opt < comp_opt then
  1657. fewer_optionals acc comp
  1658. else
  1659. fewer_optionals [cur] comp
  1660. let reduce_compatible compatible = match fewer_optionals [] (rm_duplicates [] compatible) with
  1661. | [] -> []
  1662. | [v] -> [v]
  1663. | compatible ->
  1664. (* convert compatible into ( rate * compatible_type ) list *)
  1665. let rec mk_rate acc elist args = match elist, args with
  1666. | [], [] -> acc
  1667. | (_,true) :: elist, _ :: args -> mk_rate acc elist args
  1668. | (e,false) :: elist, (n,o,t) :: args ->
  1669. (* if the argument is an implicit cast, we need to start with a penalty *)
  1670. (* The penalty should be higher than any other implicit cast - other than Dynamic *)
  1671. (* since Dynamic has a penalty of max_int, we'll impose max_int - 1 to it *)
  1672. (match e.eexpr with
  1673. | TMeta( (Meta.ImplicitCast,_,_), _) ->
  1674. mk_rate ((max_int - 1, 0) :: acc) elist args
  1675. | _ ->
  1676. mk_rate (rate_conv 0 t e.etype :: acc) elist args)
  1677. | _ -> assert false
  1678. in
  1679. let rated = ref [] in
  1680. List.iter (function
  1681. | (elist,TFun(args,ret),d) -> (try
  1682. rated := ( (elist,TFun(args,ret),d), mk_rate [] elist args ) :: !rated
  1683. with | Not_found -> ())
  1684. | _ -> assert false
  1685. ) compatible;
  1686. let rec loop best rem = match best, rem with
  1687. | _, [] -> best
  1688. | [], r1 :: rem -> loop [r1] rem
  1689. | (bover, bargs) :: b1, (rover, rargs) :: rem ->
  1690. if is_best bargs rargs then
  1691. loop best rem
  1692. else if is_best rargs bargs then
  1693. loop (loop b1 [rover,rargs]) rem
  1694. else (* equally specific *)
  1695. loop ( (rover,rargs) :: best ) rem
  1696. in
  1697. let r = loop [] !rated in
  1698. List.map fst r
  1699. end;;
  1700. module UnificationCallback = struct
  1701. let tf_stack = ref []
  1702. let check_call_params f el tl =
  1703. let rec loop acc el tl = match el,tl with
  1704. | e :: el, (n,_,t) :: tl ->
  1705. loop ((f e t) :: acc) el tl
  1706. | [], [] ->
  1707. acc
  1708. | [],_ ->
  1709. acc
  1710. | e :: el, [] ->
  1711. loop (e :: acc) el []
  1712. in
  1713. List.rev (loop [] el tl)
  1714. let check_call f el t = match follow t with
  1715. | TFun(args,_) ->
  1716. check_call_params f el args
  1717. | _ ->
  1718. List.map (fun e -> f e t_dynamic) el
  1719. let rec run ff e =
  1720. let f e t =
  1721. if not (type_iseq e.etype t) then
  1722. ff e t
  1723. else
  1724. e
  1725. in
  1726. let check e = match e.eexpr with
  1727. | TBinop((OpAssign | OpAssignOp _),e1,e2) ->
  1728. assert false; (* this trigger #4347, to be fixed before enabling
  1729. let e2 = f e2 e1.etype in
  1730. {e with eexpr = TBinop(op,e1,e2)} *)
  1731. | TVar(v,Some ev) ->
  1732. let eo = Some (f ev v.v_type) in
  1733. { e with eexpr = TVar(v,eo) }
  1734. | TCall(e1,el) ->
  1735. let el = check_call f el e1.etype in
  1736. {e with eexpr = TCall(e1,el)}
  1737. | TNew(c,tl,el) ->
  1738. begin try
  1739. let tcf,_ = get_constructor (fun cf -> apply_params c.cl_params tl cf.cf_type) c in
  1740. let el = check_call f el tcf in
  1741. {e with eexpr = TNew(c,tl,el)}
  1742. with Not_found ->
  1743. e
  1744. end
  1745. | TArrayDecl el ->
  1746. begin match follow e.etype with
  1747. | TInst({cl_path=[],"Array"},[t]) -> {e with eexpr = TArrayDecl(List.map (fun e -> f e t) el)}
  1748. | _ -> e
  1749. end
  1750. | TObjectDecl fl ->
  1751. begin match follow e.etype with
  1752. | TAnon an ->
  1753. let fl = List.map (fun (n,e) ->
  1754. let e = try
  1755. let t = (PMap.find n an.a_fields).cf_type in
  1756. f e t
  1757. with Not_found ->
  1758. e
  1759. in
  1760. n,e
  1761. ) fl in
  1762. { e with eexpr = TObjectDecl fl }
  1763. | _ -> e
  1764. end
  1765. | TReturn (Some e1) ->
  1766. begin match !tf_stack with
  1767. | tf :: _ -> { e with eexpr = TReturn (Some (f e1 tf.tf_type))}
  1768. | _ -> e
  1769. end
  1770. | _ ->
  1771. e
  1772. in
  1773. match e.eexpr with
  1774. | TFunction tf ->
  1775. tf_stack := tf :: !tf_stack;
  1776. let etf = {e with eexpr = TFunction({tf with tf_expr = run f tf.tf_expr})} in
  1777. tf_stack := List.tl !tf_stack;
  1778. etf
  1779. | _ ->
  1780. check (Type.map_expr (run ff) e)
  1781. end;;
  1782. module DeprecationCheck = struct
  1783. let curclass = ref null_class
  1784. let warned_positions = Hashtbl.create 0
  1785. let print_deprecation_message com meta s p_usage =
  1786. let s = match meta with
  1787. | _,[EConst(String s),_],_ -> s
  1788. | _ -> Printf.sprintf "Usage of this %s is deprecated" s
  1789. in
  1790. if not (Hashtbl.mem warned_positions p_usage) then begin
  1791. Hashtbl.replace warned_positions p_usage true;
  1792. com.warning s p_usage;
  1793. end
  1794. let check_meta com meta s p_usage =
  1795. try
  1796. print_deprecation_message com (Meta.get Meta.Deprecated meta) s p_usage;
  1797. with Not_found ->
  1798. ()
  1799. let check_cf com cf p = check_meta com cf.cf_meta "field" p
  1800. let check_class com c p = if c != !curclass then check_meta com c.cl_meta "class" p
  1801. let check_enum com en p = check_meta com en.e_meta "enum" p
  1802. let check_ef com ef p = check_meta com ef.ef_meta "enum field" p
  1803. let check_typedef com t p = check_meta com t.t_meta "typedef" p
  1804. let check_module_type com mt p = match mt with
  1805. | TClassDecl c -> check_class com c p
  1806. | TEnumDecl en -> check_enum com en p
  1807. | _ -> ()
  1808. let run com =
  1809. let rec expr e = match e.eexpr with
  1810. | TField(e1,fa) ->
  1811. expr e1;
  1812. begin match fa with
  1813. | FStatic(c,cf) | FInstance(c,_,cf) ->
  1814. check_class com c e.epos;
  1815. check_cf com cf e.epos
  1816. | FAnon cf ->
  1817. check_cf com cf e.epos
  1818. | FClosure(co,cf) ->
  1819. (match co with None -> () | Some (c,_) -> check_class com c e.epos);
  1820. check_cf com cf e.epos
  1821. | FEnum(en,ef) ->
  1822. check_enum com en e.epos;
  1823. check_ef com ef e.epos;
  1824. | _ ->
  1825. ()
  1826. end
  1827. | TNew(c,_,el) ->
  1828. List.iter expr el;
  1829. check_class com c e.epos;
  1830. (match c.cl_constructor with None -> () | Some cf -> check_cf com cf e.epos)
  1831. | TTypeExpr(mt) | TCast(_,Some mt) ->
  1832. check_module_type com mt e.epos
  1833. | TMeta((Meta.Deprecated,_,_) as meta,e1) ->
  1834. print_deprecation_message com meta "field" e1.epos;
  1835. expr e1;
  1836. | _ ->
  1837. Type.iter expr e
  1838. in
  1839. List.iter (fun t -> match t with
  1840. | TClassDecl c ->
  1841. curclass := c;
  1842. let field cf = match cf.cf_expr with None -> () | Some e -> expr e in
  1843. (match c.cl_constructor with None -> () | Some cf -> field cf);
  1844. (match c.cl_init with None -> () | Some e -> expr e);
  1845. List.iter field c.cl_ordered_statics;
  1846. List.iter field c.cl_ordered_fields;
  1847. | _ ->
  1848. ()
  1849. ) com.types
  1850. end
  1851. let interpolate_code com code tl f_string f_expr p =
  1852. let exprs = Array.of_list tl in
  1853. let i = ref 0 in
  1854. let err msg =
  1855. let pos = { p with pmin = p.pmin + !i } in
  1856. com.error msg pos
  1857. in
  1858. let regex = Str.regexp "[{}]" in
  1859. let rec loop m = match m with
  1860. | [] ->
  1861. ()
  1862. | Str.Text txt :: tl ->
  1863. i := !i + String.length txt;
  1864. f_string txt;
  1865. loop tl
  1866. | Str.Delim a :: Str.Delim b :: tl when a = b ->
  1867. i := !i + 2;
  1868. f_string a;
  1869. loop tl
  1870. | Str.Delim "{" :: Str.Text n :: Str.Delim "}" :: tl ->
  1871. (try
  1872. let expr = Array.get exprs (int_of_string n) in
  1873. f_expr expr;
  1874. i := !i + 2 + String.length n;
  1875. loop tl
  1876. with
  1877. | Failure "int_of_string" ->
  1878. err ("Index expected. Got " ^ n)
  1879. | Invalid_argument _ ->
  1880. err ("Out-of-bounds special parameter: " ^ n))
  1881. | Str.Delim x :: _ ->
  1882. err ("Unexpected " ^ x)
  1883. in
  1884. loop (Str.full_split regex code)
  1885. let map_source_header com f =
  1886. match Common.defined_value_safe com Define.SourceHeader with
  1887. | "" -> ()
  1888. | s -> f s
  1889. (* Collection of functions that return expressions *)
  1890. module ExprBuilder = struct
  1891. let make_static_this c p =
  1892. let ta = TAnon { a_fields = c.cl_statics; a_status = ref (Statics c) } in
  1893. mk (TTypeExpr (TClassDecl c)) ta p
  1894. let make_int com i p =
  1895. mk (TConst (TInt (Int32.of_int i))) com.basic.tint p
  1896. let make_float com f p =
  1897. mk (TConst (TFloat f)) com.basic.tfloat p
  1898. let make_null t p =
  1899. mk (TConst TNull) t p
  1900. let make_local v p =
  1901. mk (TLocal v) v.v_type p
  1902. let make_const_texpr com ct p = match ct with
  1903. | TString s -> mk (TConst (TString s)) com.basic.tstring p
  1904. | TInt i -> mk (TConst (TInt i)) com.basic.tint p
  1905. | TFloat f -> mk (TConst (TFloat f)) com.basic.tfloat p
  1906. | TBool b -> mk (TConst (TBool b)) com.basic.tbool p
  1907. | TNull -> mk (TConst TNull) (com.basic.tnull (mk_mono())) p
  1908. | _ -> error "Unsupported constant" p
  1909. end
  1910. (* Static extensions for classes *)
  1911. module ExtClass = struct
  1912. let add_cl_init c e = match c.cl_init with
  1913. | None -> c.cl_init <- Some e
  1914. | Some e' -> c.cl_init <- Some (concat e' e)
  1915. let add_static_init c cf e p =
  1916. let ethis = ExprBuilder.make_static_this c p in
  1917. let ef1 = mk (TField(ethis,FStatic(c,cf))) cf.cf_type p in
  1918. let e_assign = mk (TBinop(OpAssign,ef1,e)) e.etype p in
  1919. add_cl_init c e_assign
  1920. end