codegen.ml 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. unify_raise ctx (f()) t p;
  624. t
  625. ) s in
  626. delay ctx PForce (fun() -> ignore ((!r)()));
  627. TLazy r
  628. in
  629. let ft = (fun pl ->
  630. match c.cl_kind with
  631. | KGeneric ->
  632. build (fun () -> build_generic ctx c p pl) "build_generic"
  633. | KMacroType ->
  634. build (fun () -> build_macro_type ctx pl p) "macro_type"
  635. | KGenericBuild cfl ->
  636. build (fun () -> build_macro_build ctx c pl cfl p) "generic_build"
  637. | _ ->
  638. TInst (c,pl)
  639. ) in
  640. c.cl_params , c.cl_path , ft
  641. | TEnumDecl e ->
  642. e.e_params , e.e_path , (fun t -> TEnum (e,t))
  643. | TTypeDecl t ->
  644. t.t_params , t.t_path , (fun tl -> TType(t,tl))
  645. | TAbstractDecl a ->
  646. a.a_params, a.a_path, (fun tl -> TAbstract(a,tl))
  647. let on_inherit ctx c p h =
  648. match h with
  649. | HExtends { tpackage = ["haxe";"remoting"]; tname = "Proxy"; tparams = [TPType(CTPath t)] } ->
  650. extend_remoting ctx c t p false true;
  651. false
  652. | HExtends { tpackage = ["haxe";"remoting"]; tname = "AsyncProxy"; tparams = [TPType(CTPath t)] } ->
  653. extend_remoting ctx c t p true true;
  654. false
  655. | HExtends { tpackage = ["haxe";"xml"]; tname = "Proxy"; tparams = [TPExpr(EConst (String file),p);TPType t] } ->
  656. extend_xml_proxy ctx c t file p;
  657. true
  658. | _ ->
  659. true
  660. let push_this ctx e = match e.eexpr with
  661. | TConst ((TInt _ | TFloat _ | TString _ | TBool _) as ct) ->
  662. (EConst (tconst_to_const ct),e.epos),fun () -> ()
  663. | _ ->
  664. ctx.this_stack <- e :: ctx.this_stack;
  665. let er = EMeta((Meta.This,[],e.epos), (EConst(Ident "this"),e.epos)),e.epos in
  666. er,fun () -> ctx.this_stack <- List.tl ctx.this_stack
  667. (* -------------------------------------------------------------------------- *)
  668. (* ABSTRACT CASTS *)
  669. module AbstractCast = struct
  670. let cast_stack = ref []
  671. let make_static_call ctx c cf a pl args t p =
  672. if cf.cf_kind = Method MethMacro then begin
  673. match args with
  674. | [e] ->
  675. let e,f = push_this ctx e in
  676. ctx.with_type_stack <- (WithType t) :: ctx.with_type_stack;
  677. let e = match ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [e] p with
  678. | Some e -> type_expr ctx e Value
  679. | None -> type_expr ctx (EConst (Ident "null"),p) Value
  680. in
  681. ctx.with_type_stack <- List.tl ctx.with_type_stack;
  682. f();
  683. e
  684. | _ -> assert false
  685. end else
  686. make_static_call ctx c cf (apply_params a.a_params pl) args t p
  687. let do_check_cast ctx tleft eright p =
  688. let recurse cf f =
  689. if cf == ctx.curfield || List.mem cf !cast_stack then error "Recursive implicit cast" p;
  690. cast_stack := cf :: !cast_stack;
  691. let r = f() in
  692. cast_stack := List.tl !cast_stack;
  693. r
  694. in
  695. let find a tl f =
  696. let tcf,cf = f() in
  697. if (Meta.has Meta.MultiType a.a_meta) then
  698. mk_cast eright tleft p
  699. else match a.a_impl with
  700. | Some c -> recurse cf (fun () ->
  701. let ret = make_static_call ctx c cf a tl [eright] tleft p in
  702. { ret with eexpr = TMeta( (Meta.ImplicitCast,[],ret.epos), ret) }
  703. )
  704. | None -> assert false
  705. in
  706. if type_iseq tleft eright.etype then
  707. eright
  708. else begin
  709. let rec loop tleft tright = match follow tleft,follow tright with
  710. | TAbstract(a1,tl1),TAbstract(a2,tl2) ->
  711. begin try find a2 tl2 (fun () -> Abstract.find_to a2 tl2 tleft)
  712. with Not_found -> try find a1 tl1 (fun () -> Abstract.find_from a1 tl1 eright.etype tleft)
  713. with Not_found -> raise Not_found
  714. end
  715. | TAbstract(a,tl),_ ->
  716. begin try find a tl (fun () -> Abstract.find_from a tl eright.etype tleft)
  717. with Not_found ->
  718. let rec loop2 tcl = match tcl with
  719. | tc :: tcl ->
  720. if not (type_iseq tc tleft) then loop (apply_params a.a_params tl tc) tright
  721. else loop2 tcl
  722. | [] -> raise Not_found
  723. in
  724. loop2 a.a_from
  725. end
  726. | _,TAbstract(a,tl) ->
  727. begin try find a tl (fun () -> Abstract.find_to a tl tleft)
  728. with Not_found ->
  729. let rec loop2 tcl = match tcl with
  730. | tc :: tcl ->
  731. if not (type_iseq tc tright) then loop tleft (apply_params a.a_params tl tc)
  732. else loop2 tcl
  733. | [] -> raise Not_found
  734. in
  735. loop2 a.a_to
  736. end
  737. | _ ->
  738. raise Not_found
  739. in
  740. loop tleft eright.etype
  741. end
  742. let cast_or_unify_raise ctx tleft eright p =
  743. try
  744. (* can't do that anymore because this might miss macro calls (#4315) *)
  745. (* if ctx.com.display <> DMNone then raise Not_found; *)
  746. do_check_cast ctx tleft eright p
  747. with Not_found ->
  748. unify_raise ctx eright.etype tleft p;
  749. eright
  750. let cast_or_unify ctx tleft eright p =
  751. try
  752. cast_or_unify_raise ctx tleft eright p
  753. with Error (Unify _ as err,_) ->
  754. if not ctx.untyped then display_error ctx (error_msg err) p;
  755. eright
  756. let find_array_access_raise ctx a pl e1 e2o p =
  757. let is_set = e2o <> None in
  758. let ta = apply_params a.a_params pl a.a_this in
  759. let rec loop cfl = match cfl with
  760. | [] -> raise Not_found
  761. | cf :: cfl ->
  762. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  763. let map t = apply_params a.a_params pl (apply_params cf.cf_params monos t) in
  764. let check_constraints () =
  765. List.iter2 (fun m (name,t) -> match follow t with
  766. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  767. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> Type.unify m (map tc) ) constr
  768. | _ -> ()
  769. ) monos cf.cf_params;
  770. in
  771. match follow (map cf.cf_type) with
  772. | TFun([(_,_,tab);(_,_,ta1);(_,_,ta2)],r) as tf when is_set ->
  773. begin try
  774. Type.unify tab ta;
  775. let e1 = cast_or_unify_raise ctx ta1 e1 p in
  776. let e2o = match e2o with None -> None | Some e2 -> Some (cast_or_unify_raise ctx ta2 e2 p) in
  777. check_constraints();
  778. cf,tf,r,e1,e2o
  779. with Unify_error _ | Error (Unify _,_) ->
  780. loop cfl
  781. end
  782. | TFun([(_,_,tab);(_,_,ta1)],r) as tf when not is_set ->
  783. begin try
  784. Type.unify tab ta;
  785. let e1 = cast_or_unify_raise ctx ta1 e1 p in
  786. check_constraints();
  787. cf,tf,r,e1,None
  788. with Unify_error _ | Error (Unify _,_) ->
  789. loop cfl
  790. end
  791. | _ -> loop cfl
  792. in
  793. loop a.a_array
  794. let find_array_access ctx a tl e1 e2o p =
  795. try find_array_access_raise ctx a tl e1 e2o p
  796. with Not_found -> match e2o with
  797. | None ->
  798. error (Printf.sprintf "No @:arrayAccess function accepts argument of %s" (s_type (print_context()) e1.etype)) p
  799. | Some e2 ->
  800. 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
  801. let find_multitype_specialization com a pl p =
  802. let m = mk_mono() in
  803. let tl = match Meta.get Meta.MultiType a.a_meta with
  804. | _,[],_ -> pl
  805. | _,el,_ ->
  806. let relevant = Hashtbl.create 0 in
  807. List.iter (fun e -> match fst e with
  808. | EConst(Ident s) -> Hashtbl.replace relevant s true
  809. | _ -> error "Type parameter expected" (pos e)
  810. ) el;
  811. 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
  812. if com.platform = Js && a.a_path = ([],"Map") then begin match tl with
  813. | t1 :: _ ->
  814. let rec loop stack t =
  815. if List.exists (fun t2 -> fast_eq t t2) stack then
  816. t
  817. else begin
  818. let stack = t :: stack in
  819. match follow t with
  820. | TAbstract ({ a_path = [],"Class" },_) ->
  821. error (Printf.sprintf "Cannot use %s as key type to Map because Class<T> is not comparable" (s_type (print_context()) t1)) p;
  822. | TEnum(en,tl) ->
  823. PMap.iter (fun _ ef -> ignore(loop stack ef.ef_type)) en.e_constrs;
  824. Type.map (loop stack) t
  825. | t ->
  826. Type.map (loop stack) t
  827. end
  828. in
  829. ignore(loop [] t1)
  830. | _ -> assert false
  831. end;
  832. tl
  833. in
  834. let _,cf =
  835. try
  836. Abstract.find_to a tl m
  837. with Not_found ->
  838. let at = apply_params a.a_params pl a.a_this in
  839. let st = s_type (print_context()) at in
  840. if has_mono at then
  841. error ("Type parameters of multi type abstracts must be known (for " ^ st ^ ")") p
  842. else
  843. error ("Abstract " ^ (s_type_path a.a_path) ^ " has no @:to function that accepts " ^ st) p;
  844. in
  845. cf, follow m
  846. let handle_abstract_casts ctx e =
  847. let rec loop ctx e = match e.eexpr with
  848. | TNew({cl_kind = KAbstractImpl a} as c,pl,el) ->
  849. (* a TNew of an abstract implementation is only generated if it is a multi type abstract *)
  850. let cf,m = find_multitype_specialization ctx.com a pl e.epos in
  851. let e = make_static_call ctx c cf a pl ((mk (TConst TNull) (TAbstract(a,pl)) e.epos) :: el) m e.epos in
  852. {e with etype = m}
  853. | TCall({eexpr = TField(_,FStatic({cl_path=[],"Std"},{cf_name = "string"}))},[e1]) when (match follow e1.etype with TAbstract({a_impl = Some _},_) -> true | _ -> false) ->
  854. begin match follow e1.etype with
  855. | TAbstract({a_impl = Some c} as a,tl) ->
  856. begin try
  857. let cf = PMap.find "toString" c.cl_statics in
  858. make_static_call ctx c cf a tl [e1] ctx.t.tstring e.epos
  859. with Not_found ->
  860. e
  861. end
  862. | _ ->
  863. assert false
  864. end
  865. | TCall(e1, el) ->
  866. begin try
  867. let rec find_abstract e = match follow e.etype,e.eexpr with
  868. | TAbstract(a,pl),_ when Meta.has Meta.MultiType a.a_meta -> a,pl,e
  869. | _,TCast(e1,None) -> find_abstract e1
  870. | _ -> raise Not_found
  871. in
  872. let rec find_field e1 =
  873. match e1.eexpr with
  874. | TCast(e2,None) ->
  875. {e1 with eexpr = TCast(find_field e2,None)}
  876. | TField(e2,fa) ->
  877. let a,pl,e2 = find_abstract e2 in
  878. let m = Abstract.get_underlying_type a pl in
  879. let fname = field_name fa in
  880. let el = List.map (loop ctx) el in
  881. begin try
  882. let fa = quick_field m fname in
  883. let get_fun_type t = match follow t with
  884. | TFun(_,tr) as tf -> tf,tr
  885. | _ -> raise Not_found
  886. in
  887. let tf,tr = match fa with
  888. | FStatic(_,cf) -> get_fun_type cf.cf_type
  889. | FInstance(c,tl,cf) -> get_fun_type (apply_params c.cl_params tl cf.cf_type)
  890. | FAnon cf -> get_fun_type cf.cf_type
  891. | _ -> raise Not_found
  892. in
  893. let ef = mk (TField({e2 with etype = m},fa)) tf e2.epos in
  894. let ecall = make_call ctx ef el tr e.epos in
  895. if not (type_iseq ecall.etype e.etype) then
  896. mk (TCast(ecall,None)) e.etype e.epos
  897. else
  898. ecall
  899. with Not_found ->
  900. (* quick_field raises Not_found if m is an abstract, we have to replicate the 'using' call here *)
  901. match follow m with
  902. | TAbstract({a_impl = Some c} as a,pl) ->
  903. let cf = PMap.find fname c.cl_statics in
  904. make_static_call ctx c cf a pl (e2 :: el) e.etype e.epos
  905. | _ -> raise Not_found
  906. end
  907. | _ ->
  908. raise Not_found
  909. in
  910. find_field e1
  911. with Not_found ->
  912. Type.map_expr (loop ctx) e
  913. end
  914. | _ ->
  915. Type.map_expr (loop ctx) e
  916. in
  917. loop ctx e
  918. end
  919. module PatternMatchConversion = struct
  920. type cctx = {
  921. ctx : typer;
  922. mutable eval_stack : ((tvar * pos) * texpr) list list;
  923. dt_lookup : dt array;
  924. }
  925. let is_declared cctx v =
  926. let rec loop sl = match sl with
  927. | stack :: sl ->
  928. List.exists (fun ((v2,_),_) -> v == v2) stack || loop sl
  929. | [] ->
  930. false
  931. in
  932. loop cctx.eval_stack
  933. let group_cases cases =
  934. let dt_eq dt1 dt2 = match dt1,dt2 with
  935. | DTGoto i1, DTGoto i2 when i1 = i2 -> true
  936. (* TODO equal bindings *)
  937. | _ -> false
  938. in
  939. match List.rev cases with
  940. | [] -> []
  941. | [con,dt] -> [[con],dt]
  942. | (con,dt) :: cases ->
  943. let tmp,ldt,cases = List.fold_left (fun (tmp,ldt,acc) (con,dt) ->
  944. if dt_eq dt ldt then
  945. (con :: tmp,dt,acc)
  946. else
  947. ([con],dt,(tmp,ldt) :: acc)
  948. ) ([con],dt,[]) cases in
  949. match tmp with
  950. | [] -> cases
  951. | tmp -> ((tmp,ldt) :: cases)
  952. let replace_locals e =
  953. let v_known = ref IntMap.empty in
  954. let copy v =
  955. let v' = alloc_var v.v_name v.v_type in
  956. v_known := IntMap.add v.v_id v' !v_known;
  957. v'
  958. in
  959. let rec loop e = match e.eexpr with
  960. | TVar(v,e1) ->
  961. let v' = copy v in
  962. let e1 = match e1 with None -> None | Some e -> Some (loop e) in
  963. {e with eexpr = TVar(v',e1)}
  964. | TFor(v,e1,e2) ->
  965. let v' = copy v in
  966. let e1 = loop e1 in
  967. let e2 = loop e2 in
  968. {e with eexpr = TFor(v',e1,e2)}
  969. | TTry(e1,catches) ->
  970. let e1 = loop e1 in
  971. let catches = List.map (fun (v,e) ->
  972. let v' = copy v in
  973. let e = loop e in
  974. v',e
  975. ) catches in
  976. {e with eexpr = TTry(e1,catches)}
  977. | TLocal v ->
  978. let v' = try IntMap.find v.v_id !v_known with Not_found -> v in
  979. {e with eexpr = TLocal v'}
  980. | _ ->
  981. Type.map_expr loop e
  982. in
  983. loop e
  984. let rec convert_dt cctx dt =
  985. match dt with
  986. | DTBind (bl,dt) ->
  987. cctx.eval_stack <- bl :: cctx.eval_stack;
  988. let e = convert_dt cctx dt in
  989. cctx.eval_stack <- List.tl cctx.eval_stack;
  990. let vl,el = List.fold_left (fun (vl,el) ((v,p),e) ->
  991. if is_declared cctx v then
  992. vl, (mk (TBinop(OpAssign,mk (TLocal v) v.v_type p,e)) e.etype e.epos) :: el
  993. else
  994. ((v,p,Some e) :: vl), el
  995. ) ([],[e]) bl in
  996. let el_v = List.map (fun (v,p,eo) -> mk (TVar (v,eo)) cctx.ctx.t.tvoid p) vl in
  997. mk (TBlock (el_v @ el)) e.etype e.epos
  998. | DTGoto i ->
  999. convert_dt cctx (cctx.dt_lookup.(i))
  1000. | DTExpr e ->
  1001. e
  1002. | DTGuard(e,dt1,dt2) ->
  1003. let ethen = convert_dt cctx dt1 in
  1004. mk (TIf(e,ethen,match dt2 with None -> None | Some dt -> Some (convert_dt cctx dt))) ethen.etype (punion e.epos ethen.epos)
  1005. | DTSwitch({eexpr = TMeta((Meta.Exhaustive,_,_),_)},[_,dt],None) ->
  1006. convert_dt cctx dt
  1007. | DTSwitch(e_st,cl,dto) ->
  1008. let def = match dto with None -> None | Some dt -> Some (convert_dt cctx dt) in
  1009. let cases = group_cases cl in
  1010. let cases = List.map (fun (cl,dt) ->
  1011. let e = convert_dt cctx dt in
  1012. (* The macro interpreter does not care about unique locals and
  1013. we don't run the analyzer on the output, so let's save some
  1014. time here (issue #3937) *)
  1015. let e = if cctx.ctx.in_macro then e else replace_locals e in
  1016. cl,e
  1017. ) cases in
  1018. mk (TSwitch(e_st,cases,def)) (mk_mono()) e_st.epos
  1019. let to_typed_ast ctx dt p =
  1020. let first = dt.dt_dt_lookup.(dt.dt_first) in
  1021. let cctx = {
  1022. ctx = ctx;
  1023. dt_lookup = dt.dt_dt_lookup;
  1024. eval_stack = [];
  1025. } in
  1026. let e = convert_dt cctx first in
  1027. let e = { e with epos = p; etype = dt.dt_type} in
  1028. if dt.dt_var_init = [] then
  1029. e
  1030. else begin
  1031. let el_v = List.map (fun (v,eo) -> mk (TVar (v,eo)) cctx.ctx.t.tvoid p) dt.dt_var_init in
  1032. mk (TBlock (el_v @ [e])) dt.dt_type e.epos
  1033. end
  1034. end
  1035. (* -------------------------------------------------------------------------- *)
  1036. (* USAGE *)
  1037. let detect_usage com =
  1038. let usage = ref [] in
  1039. List.iter (fun t -> match t with
  1040. | TClassDecl c ->
  1041. let check_constructor c p =
  1042. try
  1043. let _,cf = get_constructor (fun cf -> cf.cf_type) c in
  1044. if Meta.has Meta.Usage cf.cf_meta then
  1045. usage := p :: !usage;
  1046. with Not_found ->
  1047. ()
  1048. in
  1049. let rec expr e = match e.eexpr with
  1050. | TField(_,FEnum(_,ef)) when Meta.has Meta.Usage ef.ef_meta ->
  1051. let p = {e.epos with pmin = e.epos.pmax - (String.length ef.ef_name)} in
  1052. usage := p :: !usage;
  1053. Type.iter expr e
  1054. | TField(_,(FAnon cf | FInstance (_,_,cf) | FStatic (_,cf) | FClosure (_,cf))) when Meta.has Meta.Usage cf.cf_meta ->
  1055. let p = {e.epos with pmin = e.epos.pmax - (String.length cf.cf_name)} in
  1056. usage := p :: !usage;
  1057. Type.iter expr e
  1058. | TLocal v when Meta.has Meta.Usage v.v_meta ->
  1059. usage := e.epos :: !usage
  1060. | TVar (v,_) when com.display = DMPosition && Meta.has Meta.Usage v.v_meta ->
  1061. raise (Typecore.DisplayPosition [e.epos])
  1062. | TFunction tf when com.display = DMPosition && List.exists (fun (v,_) -> Meta.has Meta.Usage v.v_meta) tf.tf_args ->
  1063. raise (Typecore.DisplayPosition [e.epos])
  1064. | TTypeExpr mt when (Meta.has Meta.Usage (t_infos mt).mt_meta) ->
  1065. usage := e.epos :: !usage
  1066. | TNew (c,_,_) ->
  1067. check_constructor c e.epos;
  1068. Type.iter expr e;
  1069. | TCall({eexpr = TConst TSuper},_) ->
  1070. begin match c.cl_super with
  1071. | Some (c,_) ->
  1072. check_constructor c e.epos
  1073. | _ ->
  1074. ()
  1075. end
  1076. | _ -> Type.iter expr e
  1077. in
  1078. let field cf = ignore(follow cf.cf_type); match cf.cf_expr with None -> () | Some e -> expr e in
  1079. (match c.cl_constructor with None -> () | Some cf -> field cf);
  1080. (match c.cl_init with None -> () | Some e -> expr e);
  1081. List.iter field c.cl_ordered_statics;
  1082. List.iter field c.cl_ordered_fields;
  1083. | _ -> ()
  1084. ) com.types;
  1085. let usage = List.sort (fun p1 p2 ->
  1086. let c = compare p1.pfile p2.pfile in
  1087. if c <> 0 then c else compare p1.pmin p2.pmin
  1088. ) !usage in
  1089. raise (Typecore.DisplayPosition usage)
  1090. let update_cache_dependencies com =
  1091. let rec check_t m t = match t with
  1092. | TInst(c,tl) ->
  1093. add_dependency m c.cl_module;
  1094. List.iter (check_t m) tl;
  1095. | TEnum(en,tl) ->
  1096. add_dependency m en.e_module;
  1097. List.iter (check_t m) tl;
  1098. | TType(t,tl) ->
  1099. add_dependency m t.t_module;
  1100. List.iter (check_t m) tl;
  1101. | TAbstract(a,tl) ->
  1102. add_dependency m a.a_module;
  1103. List.iter (check_t m) tl;
  1104. | TFun(targs,tret) ->
  1105. List.iter (fun (_,_,t) -> check_t m t) targs;
  1106. check_t m tret;
  1107. | TAnon an ->
  1108. PMap.iter (fun _ cf -> check_field m cf) an.a_fields
  1109. | TMono r ->
  1110. (match !r with
  1111. | Some t -> check_t m t
  1112. | _ -> ())
  1113. | TLazy f ->
  1114. check_t m (!f())
  1115. | TDynamic t ->
  1116. if t == t_dynamic then
  1117. ()
  1118. else
  1119. check_t m t
  1120. and check_field m cf =
  1121. check_t m cf.cf_type
  1122. in
  1123. List.iter (fun t -> match t with
  1124. | TClassDecl c ->
  1125. List.iter (check_field c.cl_module) c.cl_ordered_statics;
  1126. List.iter (check_field c.cl_module) c.cl_ordered_fields;
  1127. (match c.cl_constructor with None -> () | Some cf -> check_field c.cl_module cf);
  1128. | _ ->
  1129. ()
  1130. ) com.types
  1131. (* -------------------------------------------------------------------------- *)
  1132. (* STACK MANAGEMENT EMULATION *)
  1133. type stack_context = {
  1134. stack_var : string;
  1135. stack_exc_var : string;
  1136. stack_pos_var : string;
  1137. stack_pos : pos;
  1138. stack_expr : texpr;
  1139. stack_pop : texpr;
  1140. stack_save_pos : texpr;
  1141. stack_restore : texpr list;
  1142. stack_push : tclass -> string -> texpr;
  1143. stack_return : texpr -> texpr;
  1144. }
  1145. let stack_context_init com stack_var exc_var pos_var tmp_var use_add p =
  1146. let t = com.basic in
  1147. let st = t.tarray t.tstring in
  1148. let stack_var = alloc_var stack_var st in
  1149. let exc_var = alloc_var exc_var st in
  1150. let pos_var = alloc_var pos_var t.tint in
  1151. let stack_e = mk (TLocal stack_var) st p in
  1152. let exc_e = mk (TLocal exc_var) st p in
  1153. let stack_pop = fcall stack_e "pop" [] t.tstring p in
  1154. let stack_push c m =
  1155. fcall stack_e "push" [
  1156. if use_add then
  1157. binop OpAdd (string com (s_type_path c.cl_path ^ "::") p) (string com m p) t.tstring p
  1158. else
  1159. string com (s_type_path c.cl_path ^ "::" ^ m) p
  1160. ] t.tvoid p
  1161. in
  1162. let stack_return e =
  1163. let tmp = alloc_var tmp_var e.etype in
  1164. mk (TBlock [
  1165. mk (TVar (tmp, Some e)) t.tvoid e.epos;
  1166. stack_pop;
  1167. mk (TReturn (Some (mk (TLocal tmp) e.etype e.epos))) e.etype e.epos
  1168. ]) e.etype e.epos
  1169. in
  1170. {
  1171. stack_var = stack_var.v_name;
  1172. stack_exc_var = exc_var.v_name;
  1173. stack_pos_var = pos_var.v_name;
  1174. stack_pos = p;
  1175. stack_expr = stack_e;
  1176. stack_pop = stack_pop;
  1177. stack_save_pos = mk (TVar (pos_var, Some (field stack_e "length" t.tint p))) t.tvoid p;
  1178. stack_push = stack_push;
  1179. stack_return = stack_return;
  1180. stack_restore = [
  1181. binop OpAssign exc_e (mk (TArrayDecl []) st p) st p;
  1182. mk (TWhile (
  1183. mk_parent (binop OpGte (field stack_e "length" t.tint p) (mk (TLocal pos_var) t.tint p) t.tbool p),
  1184. fcall exc_e "unshift" [fcall stack_e "pop" [] t.tstring p] t.tvoid p,
  1185. NormalWhile
  1186. )) t.tvoid p;
  1187. fcall stack_e "push" [index com exc_e 0 t.tstring p] t.tvoid p
  1188. ];
  1189. }
  1190. let stack_init com use_add =
  1191. stack_context_init com "$s" "$e" "$spos" "$tmp" use_add null_pos
  1192. let rec stack_block_loop ctx e =
  1193. match e.eexpr with
  1194. | TFunction _ ->
  1195. e
  1196. | TReturn None | TReturn (Some { eexpr = TConst _ }) | TReturn (Some { eexpr = TLocal _ }) ->
  1197. mk (TBlock [
  1198. ctx.stack_pop;
  1199. e;
  1200. ]) e.etype e.epos
  1201. | TReturn (Some e) ->
  1202. ctx.stack_return (stack_block_loop ctx e)
  1203. | TTry (v,cases) ->
  1204. let v = stack_block_loop ctx v in
  1205. let cases = List.map (fun (v,e) ->
  1206. let e = stack_block_loop ctx e in
  1207. let e = (match (mk_block e).eexpr with
  1208. | TBlock l -> mk (TBlock (ctx.stack_restore @ l)) e.etype e.epos
  1209. | _ -> assert false
  1210. ) in
  1211. v , e
  1212. ) cases in
  1213. mk (TTry (v,cases)) e.etype e.epos
  1214. | _ ->
  1215. map_expr (stack_block_loop ctx) e
  1216. let stack_block ctx c m e =
  1217. match (mk_block e).eexpr with
  1218. | TBlock l ->
  1219. mk (TBlock (
  1220. ctx.stack_push c m ::
  1221. ctx.stack_save_pos ::
  1222. List.map (stack_block_loop ctx) l
  1223. @ [ctx.stack_pop]
  1224. )) e.etype e.epos
  1225. | _ ->
  1226. assert false
  1227. (* -------------------------------------------------------------------------- *)
  1228. (* FIX OVERRIDES *)
  1229. (*
  1230. on some platforms which doesn't support type parameters, we must have the
  1231. exact same type for overriden/implemented function as the original one
  1232. *)
  1233. let rec find_field com c f =
  1234. try
  1235. (match c.cl_super with
  1236. | None ->
  1237. raise Not_found
  1238. | Some ( {cl_path = (["cpp"],"FastIterator")}, _ ) ->
  1239. raise Not_found (* This is a strongly typed 'extern' and the usual rules don't apply *)
  1240. | Some (c,_) ->
  1241. find_field com c f)
  1242. with Not_found -> try
  1243. if com.platform = Cpp then (* Cpp uses delegation for interfaces *)
  1244. raise Not_found;
  1245. let rec loop = function
  1246. | [] ->
  1247. raise Not_found
  1248. | (c,_) :: l ->
  1249. try
  1250. find_field com c f
  1251. with
  1252. Not_found -> loop l
  1253. in
  1254. loop c.cl_implements
  1255. with Not_found ->
  1256. let f = PMap.find f.cf_name c.cl_fields in
  1257. (match f.cf_kind with Var { v_read = AccRequire _ } -> raise Not_found | _ -> ());
  1258. f
  1259. let fix_override com c f fd =
  1260. let f2 = (try Some (find_field com c f) with Not_found -> None) in
  1261. match f2,fd with
  1262. | Some (f2), Some(fd) ->
  1263. let targs, tret = (match follow f2.cf_type with TFun (args,ret) -> args, ret | _ -> assert false) in
  1264. let changed_args = ref [] in
  1265. let prefix = "_tmp_" in
  1266. let nargs = List.map2 (fun ((v,ct) as cur) (_,_,t2) ->
  1267. try
  1268. type_eq EqStrict (monomorphs c.cl_params (monomorphs f.cf_params v.v_type)) t2;
  1269. (* Flash generates type parameters with a single constraint as that constraint type, so we
  1270. have to detect this case and change the variable (issue #2712). *)
  1271. begin match follow v.v_type with
  1272. | TInst({cl_kind = KTypeParameter [tc]} as cp,_) when com.platform = Flash ->
  1273. if List.mem_assoc (snd cp.cl_path) c.cl_params then raise (Unify_error [])
  1274. | _ ->
  1275. ()
  1276. end;
  1277. cur
  1278. with Unify_error _ ->
  1279. let v2 = alloc_var (prefix ^ v.v_name) t2 in
  1280. changed_args := (v,v2) :: !changed_args;
  1281. v2,ct
  1282. ) fd.tf_args targs in
  1283. let fd2 = {
  1284. tf_args = nargs;
  1285. tf_type = tret;
  1286. tf_expr = (match List.rev !changed_args with
  1287. | [] -> fd.tf_expr
  1288. | args ->
  1289. let e = fd.tf_expr in
  1290. let el = (match e.eexpr with TBlock el -> el | _ -> [e]) in
  1291. let p = (match el with [] -> e.epos | e :: _ -> e.epos) in
  1292. let el_v = List.map (fun (v,v2) ->
  1293. mk (TVar (v,Some (mk (TCast (mk (TLocal v2) v2.v_type p,None)) v.v_type p))) com.basic.tvoid p
  1294. ) args in
  1295. { e with eexpr = TBlock (el_v @ el) }
  1296. );
  1297. } in
  1298. (* as3 does not allow wider visibility, so the base method has to be made public *)
  1299. if Common.defined com Define.As3 && f.cf_public then f2.cf_public <- true;
  1300. let targs = List.map (fun(v,c) -> (v.v_name, Option.is_some c, v.v_type)) nargs in
  1301. let fde = (match f.cf_expr with None -> assert false | Some e -> e) in
  1302. f.cf_expr <- Some { fde with eexpr = TFunction fd2 };
  1303. f.cf_type <- TFun(targs,tret);
  1304. | Some(f2), None when c.cl_interface ->
  1305. let targs, tret = (match follow f2.cf_type with TFun (args,ret) -> args, ret | _ -> assert false) in
  1306. f.cf_type <- TFun(targs,tret)
  1307. | _ ->
  1308. ()
  1309. let fix_overrides com t =
  1310. match t with
  1311. | TClassDecl c ->
  1312. (* overrides can be removed from interfaces *)
  1313. if c.cl_interface then
  1314. c.cl_ordered_fields <- List.filter (fun f ->
  1315. try
  1316. if find_field com c f == f then raise Not_found;
  1317. c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  1318. false;
  1319. with Not_found ->
  1320. true
  1321. ) c.cl_ordered_fields;
  1322. List.iter (fun f ->
  1323. match f.cf_expr, f.cf_kind with
  1324. | Some { eexpr = TFunction fd }, Method (MethNormal | MethInline) ->
  1325. fix_override com c f (Some fd)
  1326. | None, Method (MethNormal | MethInline) when c.cl_interface ->
  1327. fix_override com c f None
  1328. | _ ->
  1329. ()
  1330. ) c.cl_ordered_fields
  1331. | _ ->
  1332. ()
  1333. (*
  1334. PHP does not allow abstract classes extending other abstract classes to override any fields, so these duplicates
  1335. must be removed from the child interface
  1336. *)
  1337. let fix_abstract_inheritance com t =
  1338. match t with
  1339. | TClassDecl c when c.cl_interface ->
  1340. c.cl_ordered_fields <- List.filter (fun f ->
  1341. let b = try (find_field com c f) == f
  1342. with Not_found -> false in
  1343. if not b then c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  1344. b;
  1345. ) c.cl_ordered_fields
  1346. | _ -> ()
  1347. (* -------------------------------------------------------------------------- *)
  1348. (* MISC FEATURES *)
  1349. let rec is_volatile t =
  1350. match t with
  1351. | TMono r ->
  1352. (match !r with
  1353. | Some t -> is_volatile t
  1354. | _ -> false)
  1355. | TLazy f ->
  1356. is_volatile (!f())
  1357. | TType (t,tl) ->
  1358. (match t.t_path with
  1359. | _ -> is_volatile (apply_params t.t_params tl t.t_type))
  1360. | _ ->
  1361. false
  1362. let set_default ctx a c p =
  1363. let t = a.v_type in
  1364. let ve = mk (TLocal a) t p in
  1365. let cond = TBinop (OpEq,ve,mk (TConst TNull) t p) in
  1366. 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
  1367. let bytes_serialize data =
  1368. let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" in
  1369. let tbl = Array.init (String.length b64) (fun i -> String.get b64 i) in
  1370. Base64.str_encode ~tbl data
  1371. (*
  1372. Tells if the constructor might be called without any issue whatever its parameters
  1373. *)
  1374. let rec constructor_side_effects e =
  1375. match e.eexpr with
  1376. | TBinop (op,_,_) when op <> OpAssign ->
  1377. true
  1378. | TField (_,FEnum _) ->
  1379. false
  1380. | TUnop _ | TArray _ | TField _ | TEnumParameter _ | TCall _ | TNew _ | TFor _ | TWhile _ | TSwitch _ | TReturn _ | TThrow _ ->
  1381. true
  1382. | TBinop _ | TTry _ | TIf _ | TBlock _ | TVar _
  1383. | TFunction _ | TArrayDecl _ | TObjectDecl _
  1384. | TParenthesis _ | TTypeExpr _ | TLocal _ | TMeta _
  1385. | TConst _ | TContinue | TBreak | TCast _ ->
  1386. try
  1387. Type.iter (fun e -> if constructor_side_effects e then raise Exit) e;
  1388. false;
  1389. with Exit ->
  1390. true
  1391. let make_valid_filename s =
  1392. let r = Str.regexp "[^A-Za-z0-9_\\-\\.,]" in
  1393. Str.global_substitute r (fun s -> "_") s
  1394. (*
  1395. Make a dump of the full typed AST of all types
  1396. *)
  1397. let rec create_dumpfile acc = function
  1398. | [] -> assert false
  1399. | d :: [] ->
  1400. let d = make_valid_filename d in
  1401. let ch = open_out (String.concat "/" (List.rev (d :: acc)) ^ ".dump") in
  1402. let buf = Buffer.create 0 in
  1403. buf, (fun () ->
  1404. output_string ch (Buffer.contents buf);
  1405. close_out ch)
  1406. | d :: l ->
  1407. let dir = String.concat "/" (List.rev (d :: acc)) in
  1408. if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
  1409. create_dumpfile (d :: acc) l
  1410. let dump_types com =
  1411. let s_type = s_type (Type.print_context()) in
  1412. let params = function [] -> "" | l -> Printf.sprintf "<%s>" (String.concat "," (List.map (fun (n,t) -> n ^ " : " ^ s_type t) l)) in
  1413. let s_expr = match Common.defined_value_safe com Define.Dump with
  1414. | "pretty" -> Type.s_expr_pretty "\t"
  1415. | "legacy" -> Type.s_expr
  1416. | _ -> Type.s_expr_ast (not (Common.defined com Define.DumpIgnoreVarIds)) "\t"
  1417. in
  1418. List.iter (fun mt ->
  1419. let path = Type.t_path mt in
  1420. let buf,close = create_dumpfile [] ("dump" :: (Common.platform_name com.platform) :: fst path @ [snd path]) in
  1421. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1422. (match mt with
  1423. | Type.TClassDecl c ->
  1424. let rec print_field stat f =
  1425. 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);
  1426. print "(%s) : %s" (s_kind f.cf_kind) (s_type f.cf_type);
  1427. (match f.cf_expr with
  1428. | None -> ()
  1429. | Some e -> print "\n\n\t = %s" (s_expr s_type e));
  1430. print "\n\n";
  1431. List.iter (fun f -> print_field stat f) f.cf_overloads
  1432. in
  1433. 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);
  1434. (match c.cl_super with None -> () | Some (c,pl) -> print " extends %s" (s_type (TInst (c,pl))));
  1435. List.iter (fun (c,pl) -> print " implements %s" (s_type (TInst (c,pl)))) c.cl_implements;
  1436. (match c.cl_dynamic with None -> () | Some t -> print " implements Dynamic<%s>" (s_type t));
  1437. (match c.cl_array_access with None -> () | Some t -> print " implements ArrayAccess<%s>" (s_type t));
  1438. print "{\n";
  1439. (match c.cl_constructor with
  1440. | None -> ()
  1441. | Some f -> print_field false f);
  1442. List.iter (print_field false) c.cl_ordered_fields;
  1443. List.iter (print_field true) c.cl_ordered_statics;
  1444. (match c.cl_init with
  1445. | None -> ()
  1446. | Some e ->
  1447. print "\n\n\t__init__ = ";
  1448. print "%s" (s_expr s_type e);
  1449. print "}\n");
  1450. print "}";
  1451. | Type.TEnumDecl e ->
  1452. 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);
  1453. List.iter (fun n ->
  1454. let f = PMap.find n e.e_constrs in
  1455. print "\t%s : %s;\n" f.ef_name (s_type f.ef_type);
  1456. ) e.e_names;
  1457. print "}"
  1458. | Type.TTypeDecl t ->
  1459. 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);
  1460. | Type.TAbstractDecl a ->
  1461. print "%sabstract %s%s {}" (if a.a_private then "private " else "") (s_type_path path) (params a.a_params);
  1462. );
  1463. close();
  1464. ) com.types
  1465. let dump_dependencies com =
  1466. let buf,close = create_dumpfile [] ["dump";Common.platform_name com.platform;".dependencies"] in
  1467. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1468. let dep = Hashtbl.create 0 in
  1469. List.iter (fun m ->
  1470. print "%s:\n" m.m_extra.m_file;
  1471. PMap.iter (fun _ m2 ->
  1472. print "\t%s\n" (m2.m_extra.m_file);
  1473. let l = try Hashtbl.find dep m2.m_extra.m_file with Not_found -> [] in
  1474. Hashtbl.replace dep m2.m_extra.m_file (m :: l)
  1475. ) m.m_extra.m_deps;
  1476. ) com.Common.modules;
  1477. close();
  1478. let buf,close = create_dumpfile [] ["dump";Common.platform_name com.platform;".dependants"] in
  1479. let print fmt = Printf.kprintf (fun s -> Buffer.add_string buf s) fmt in
  1480. Hashtbl.iter (fun n ml ->
  1481. print "%s:\n" n;
  1482. List.iter (fun m ->
  1483. print "\t%s\n" (m.m_extra.m_file);
  1484. ) ml;
  1485. ) dep;
  1486. close()
  1487. (*
  1488. Build a default safe-cast expression :
  1489. { var $t = <e>; if( Std.is($t,<t>) ) $t else throw "Class cast error"; }
  1490. *)
  1491. let default_cast ?(vtmp="$t") com e texpr t p =
  1492. let api = com.basic in
  1493. let mk_texpr = function
  1494. | TClassDecl c -> TAnon { a_fields = PMap.empty; a_status = ref (Statics c) }
  1495. | TEnumDecl e -> TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics e) }
  1496. | TAbstractDecl a -> TAnon { a_fields = PMap.empty; a_status = ref (AbstractStatics a) }
  1497. | TTypeDecl _ -> assert false
  1498. in
  1499. let vtmp = alloc_var vtmp e.etype in
  1500. let var = mk (TVar (vtmp,Some e)) api.tvoid p in
  1501. let vexpr = mk (TLocal vtmp) e.etype p in
  1502. let texpr = mk (TTypeExpr texpr) (mk_texpr texpr) p in
  1503. let std = (try List.find (fun t -> t_path t = ([],"Std")) com.types with Not_found -> assert false) in
  1504. let fis = (try
  1505. let c = (match std with TClassDecl c -> c | _ -> assert false) in
  1506. FStatic (c, PMap.find "is" c.cl_statics)
  1507. with Not_found ->
  1508. assert false
  1509. ) in
  1510. let std = mk (TTypeExpr std) (mk_texpr std) p in
  1511. let is = mk (TField (std,fis)) (tfun [t_dynamic;t_dynamic] api.tbool) p in
  1512. let is = mk (TCall (is,[vexpr;texpr])) api.tbool p in
  1513. let exc = mk (TThrow (mk (TConst (TString "Class cast error")) api.tstring p)) t p in
  1514. let check = mk (TIf (mk_parent is,mk (TCast (vexpr,None)) t p,Some exc)) t p in
  1515. mk (TBlock [var;check;vexpr]) t p
  1516. (** Overload resolution **)
  1517. module Overloads =
  1518. struct
  1519. let rec simplify_t t = match t with
  1520. | TAbstract(a,_) when Meta.has Meta.CoreType a.a_meta ->
  1521. t
  1522. | TInst _ | TEnum _ ->
  1523. t
  1524. | TAbstract(a,tl) -> simplify_t (Abstract.get_underlying_type a tl)
  1525. | TType(({ t_path = [],"Null" } as t), [t2]) -> (match simplify_t t2 with
  1526. | (TAbstract(a,_) as t2) when Meta.has Meta.CoreType a.a_meta ->
  1527. TType(t, [simplify_t t2])
  1528. | (TEnum _ as t2) ->
  1529. TType(t, [simplify_t t2])
  1530. | t2 -> t2)
  1531. | TType(t, tl) ->
  1532. simplify_t (apply_params t.t_params tl t.t_type)
  1533. | TMono r -> (match !r with
  1534. | Some t -> simplify_t t
  1535. | None -> t_dynamic)
  1536. | TAnon _ -> t_dynamic
  1537. | TDynamic _ -> t
  1538. | TLazy f -> simplify_t (!f())
  1539. | TFun _ -> t
  1540. (* rate type parameters *)
  1541. let rate_tp tlfun tlarg =
  1542. let acc = ref 0 in
  1543. List.iter2 (fun f a -> if not (type_iseq f a) then incr acc) tlfun tlarg;
  1544. !acc
  1545. (**
  1546. The rate function returns an ( int * int ) type.
  1547. The smaller the int, the best rated the caller argument is in comparison with the callee.
  1548. The first int refers to how many "conversions" would be necessary to convert from the callee to the caller type, and
  1549. the second refers to the type parameters.
  1550. **)
  1551. let rec rate_conv cacc tfun targ =
  1552. match simplify_t tfun, simplify_t targ with
  1553. | TInst({ cl_interface = true } as cf, tlf), TInst(ca, tla) ->
  1554. (* breadth-first *)
  1555. let stack = ref [0,ca,tla] in
  1556. let cur = ref (0, ca,tla) in
  1557. let rec loop () =
  1558. match !stack with
  1559. | [] -> (let acc, ca, tla = !cur in match ca.cl_super with
  1560. | None -> raise Not_found
  1561. | Some (sup,tls) ->
  1562. cur := (acc+1,sup,List.map (apply_params ca.cl_params tla) tls);
  1563. stack := [!cur];
  1564. loop())
  1565. | (acc,ca,tla) :: _ when ca == cf ->
  1566. acc,tla
  1567. | (acc,ca,tla) :: s ->
  1568. stack := s @ List.map (fun (c,tl) -> (acc+1,c,List.map (apply_params ca.cl_params tla) tl)) ca.cl_implements;
  1569. loop()
  1570. in
  1571. let acc, tla = loop() in
  1572. (cacc + acc, rate_tp tlf tla)
  1573. | TInst(cf,tlf), TInst(ca,tla) ->
  1574. let rec loop acc ca tla =
  1575. if cf == ca then
  1576. acc, tla
  1577. else match ca.cl_super with
  1578. | None -> raise Not_found
  1579. | Some(sup,stl) ->
  1580. loop (acc+1) sup (List.map (apply_params ca.cl_params tla) stl)
  1581. in
  1582. let acc, tla = loop 0 ca tla in
  1583. (cacc + acc, rate_tp tlf tla)
  1584. | TEnum(ef,tlf), TEnum(ea, tla) ->
  1585. if ef != ea then raise Not_found;
  1586. (cacc, rate_tp tlf tla)
  1587. | TDynamic _, TDynamic _ ->
  1588. (cacc, 0)
  1589. | TDynamic _, _ ->
  1590. (max_int, 0) (* a function with dynamic will always be worst of all *)
  1591. | TAbstract(a, _), TDynamic _ when Meta.has Meta.CoreType a.a_meta ->
  1592. (cacc + 2, 0) (* a dynamic to a basic type will have an "unboxing" penalty *)
  1593. | _, TDynamic _ ->
  1594. (cacc + 1, 0)
  1595. | TAbstract(af,tlf), TAbstract(aa,tla) ->
  1596. (if af == aa then
  1597. (cacc, rate_tp tlf tla)
  1598. else
  1599. let ret = ref None in
  1600. if List.exists (fun t -> try
  1601. ret := Some (rate_conv (cacc+1) (apply_params af.a_params tlf t) targ);
  1602. true
  1603. with | Not_found ->
  1604. false
  1605. ) af.a_from then
  1606. Option.get !ret
  1607. else
  1608. if List.exists (fun t -> try
  1609. ret := Some (rate_conv (cacc+1) tfun (apply_params aa.a_params tla t));
  1610. true
  1611. with | Not_found ->
  1612. false
  1613. ) aa.a_to then
  1614. Option.get !ret
  1615. else
  1616. raise Not_found)
  1617. | TType({ t_path = [], "Null" }, [tf]), TType({ t_path = [], "Null" }, [ta]) ->
  1618. rate_conv (cacc+0) tf ta
  1619. | TType({ t_path = [], "Null" }, [tf]), ta ->
  1620. rate_conv (cacc+1) tf ta
  1621. | tf, TType({ t_path = [], "Null" }, [ta]) ->
  1622. rate_conv (cacc+1) tf ta
  1623. | TFun _, TFun _ -> (* unify will make sure they are compatible *)
  1624. cacc,0
  1625. | tfun,targ ->
  1626. raise Not_found
  1627. let is_best arg1 arg2 =
  1628. (List.for_all2 (fun v1 v2 ->
  1629. v1 <= v2)
  1630. arg1 arg2) && (List.exists2 (fun v1 v2 ->
  1631. v1 < v2)
  1632. arg1 arg2)
  1633. let rec rm_duplicates acc ret = match ret with
  1634. | [] -> acc
  1635. | ( el, t, _ ) :: ret when List.exists (fun (_,t2,_) -> type_iseq t t2) acc ->
  1636. rm_duplicates acc ret
  1637. | r :: ret ->
  1638. rm_duplicates (r :: acc) ret
  1639. let s_options rated =
  1640. String.concat ",\n" (List.map (fun ((elist,t,_),rate) ->
  1641. "( " ^ (String.concat "," (List.map (fun(e,_) -> s_expr (s_type (print_context())) e) elist)) ^ " ) => " ^
  1642. "( " ^ (String.concat "," (List.map (fun (i,i2) -> string_of_int i ^ ":" ^ string_of_int i2) rate)) ^ " ) => " ^ (s_type (print_context()) t)
  1643. ) rated)
  1644. let count_optionals elist =
  1645. List.fold_left (fun acc (_,is_optional) -> if is_optional then acc + 1 else acc) 0 elist
  1646. let rec fewer_optionals acc compatible = match acc, compatible with
  1647. | _, [] -> acc
  1648. | [], c :: comp -> fewer_optionals [c] comp
  1649. | (elist_acc, _, _) :: _, ((elist, _, _) as cur) :: comp ->
  1650. let acc_opt = count_optionals elist_acc in
  1651. let comp_opt = count_optionals elist in
  1652. if acc_opt = comp_opt then
  1653. fewer_optionals (cur :: acc) comp
  1654. else if acc_opt < comp_opt then
  1655. fewer_optionals acc comp
  1656. else
  1657. fewer_optionals [cur] comp
  1658. let reduce_compatible compatible = match fewer_optionals [] (rm_duplicates [] compatible) with
  1659. | [] -> []
  1660. | [v] -> [v]
  1661. | compatible ->
  1662. (* convert compatible into ( rate * compatible_type ) list *)
  1663. let rec mk_rate acc elist args = match elist, args with
  1664. | [], [] -> acc
  1665. | (_,true) :: elist, _ :: args -> mk_rate acc elist args
  1666. | (e,false) :: elist, (n,o,t) :: args ->
  1667. (* if the argument is an implicit cast, we need to start with a penalty *)
  1668. (* The penalty should be higher than any other implicit cast - other than Dynamic *)
  1669. (* since Dynamic has a penalty of max_int, we'll impose max_int - 1 to it *)
  1670. (match e.eexpr with
  1671. | TMeta( (Meta.ImplicitCast,_,_), _) ->
  1672. mk_rate ((max_int - 1, 0) :: acc) elist args
  1673. | _ ->
  1674. mk_rate (rate_conv 0 t e.etype :: acc) elist args)
  1675. | _ -> assert false
  1676. in
  1677. let rated = ref [] in
  1678. List.iter (function
  1679. | (elist,TFun(args,ret),d) -> (try
  1680. rated := ( (elist,TFun(args,ret),d), mk_rate [] elist args ) :: !rated
  1681. with | Not_found -> ())
  1682. | _ -> assert false
  1683. ) compatible;
  1684. let rec loop best rem = match best, rem with
  1685. | _, [] -> best
  1686. | [], r1 :: rem -> loop [r1] rem
  1687. | (bover, bargs) :: b1, (rover, rargs) :: rem ->
  1688. if is_best bargs rargs then
  1689. loop best rem
  1690. else if is_best rargs bargs then
  1691. loop (loop b1 [rover,rargs]) rem
  1692. else (* equally specific *)
  1693. loop ( (rover,rargs) :: best ) rem
  1694. in
  1695. let r = loop [] !rated in
  1696. List.map fst r
  1697. end;;
  1698. module UnificationCallback = struct
  1699. let tf_stack = ref []
  1700. let check_call_params f el tl =
  1701. let rec loop acc el tl = match el,tl with
  1702. | e :: el, (n,_,t) :: tl ->
  1703. loop ((f e t) :: acc) el tl
  1704. | [], [] ->
  1705. acc
  1706. | [],_ ->
  1707. acc
  1708. | e :: el, [] ->
  1709. loop (e :: acc) el []
  1710. in
  1711. List.rev (loop [] el tl)
  1712. let check_call f el t = match follow t with
  1713. | TFun(args,_) ->
  1714. check_call_params f el args
  1715. | _ ->
  1716. List.map (fun e -> f e t_dynamic) el
  1717. let rec run ff e =
  1718. let f e t =
  1719. if not (type_iseq e.etype t) then
  1720. ff e t
  1721. else
  1722. e
  1723. in
  1724. let check e = match e.eexpr with
  1725. | TBinop((OpAssign | OpAssignOp _),e1,e2) ->
  1726. assert false; (* this trigger #4347, to be fixed before enabling
  1727. let e2 = f e2 e1.etype in
  1728. {e with eexpr = TBinop(op,e1,e2)} *)
  1729. | TVar(v,Some ev) ->
  1730. let eo = Some (f ev v.v_type) in
  1731. { e with eexpr = TVar(v,eo) }
  1732. | TCall(e1,el) ->
  1733. let el = check_call f el e1.etype in
  1734. {e with eexpr = TCall(e1,el)}
  1735. | TNew(c,tl,el) ->
  1736. begin try
  1737. let tcf,_ = get_constructor (fun cf -> apply_params c.cl_params tl cf.cf_type) c in
  1738. let el = check_call f el tcf in
  1739. {e with eexpr = TNew(c,tl,el)}
  1740. with Not_found ->
  1741. e
  1742. end
  1743. | TArrayDecl el ->
  1744. begin match follow e.etype with
  1745. | TInst({cl_path=[],"Array"},[t]) -> {e with eexpr = TArrayDecl(List.map (fun e -> f e t) el)}
  1746. | _ -> e
  1747. end
  1748. | TObjectDecl fl ->
  1749. begin match follow e.etype with
  1750. | TAnon an ->
  1751. let fl = List.map (fun (n,e) ->
  1752. let e = try
  1753. let t = (PMap.find n an.a_fields).cf_type in
  1754. f e t
  1755. with Not_found ->
  1756. e
  1757. in
  1758. n,e
  1759. ) fl in
  1760. { e with eexpr = TObjectDecl fl }
  1761. | _ -> e
  1762. end
  1763. | TReturn (Some e1) ->
  1764. begin match !tf_stack with
  1765. | tf :: _ -> { e with eexpr = TReturn (Some (f e1 tf.tf_type))}
  1766. | _ -> e
  1767. end
  1768. | _ ->
  1769. e
  1770. in
  1771. match e.eexpr with
  1772. | TFunction tf ->
  1773. tf_stack := tf :: !tf_stack;
  1774. let etf = {e with eexpr = TFunction({tf with tf_expr = run f tf.tf_expr})} in
  1775. tf_stack := List.tl !tf_stack;
  1776. etf
  1777. | _ ->
  1778. check (Type.map_expr (run ff) e)
  1779. end;;
  1780. module DeprecationCheck = struct
  1781. let curclass = ref null_class
  1782. let warned_positions = Hashtbl.create 0
  1783. let print_deprecation_message com meta s p_usage =
  1784. let s = match meta with
  1785. | _,[EConst(String s),_],_ -> s
  1786. | _ -> Printf.sprintf "Usage of this %s is deprecated" s
  1787. in
  1788. if not (Hashtbl.mem warned_positions p_usage) then begin
  1789. Hashtbl.replace warned_positions p_usage true;
  1790. com.warning s p_usage;
  1791. end
  1792. let check_meta com meta s p_usage =
  1793. try
  1794. print_deprecation_message com (Meta.get Meta.Deprecated meta) s p_usage;
  1795. with Not_found ->
  1796. ()
  1797. let check_cf com cf p = check_meta com cf.cf_meta "field" p
  1798. let check_class com c p = if c != !curclass then check_meta com c.cl_meta "class" p
  1799. let check_enum com en p = check_meta com en.e_meta "enum" p
  1800. let check_ef com ef p = check_meta com ef.ef_meta "enum field" p
  1801. let check_typedef com t p = check_meta com t.t_meta "typedef" p
  1802. let check_module_type com mt p = match mt with
  1803. | TClassDecl c -> check_class com c p
  1804. | TEnumDecl en -> check_enum com en p
  1805. | _ -> ()
  1806. let run com =
  1807. let rec expr e = match e.eexpr with
  1808. | TField(e1,fa) ->
  1809. expr e1;
  1810. begin match fa with
  1811. | FStatic(c,cf) | FInstance(c,_,cf) ->
  1812. check_class com c e.epos;
  1813. check_cf com cf e.epos
  1814. | FAnon cf ->
  1815. check_cf com cf e.epos
  1816. | FClosure(co,cf) ->
  1817. (match co with None -> () | Some (c,_) -> check_class com c e.epos);
  1818. check_cf com cf e.epos
  1819. | FEnum(en,ef) ->
  1820. check_enum com en e.epos;
  1821. check_ef com ef e.epos;
  1822. | _ ->
  1823. ()
  1824. end
  1825. | TNew(c,_,el) ->
  1826. List.iter expr el;
  1827. check_class com c e.epos;
  1828. (match c.cl_constructor with None -> () | Some cf -> check_cf com cf e.epos)
  1829. | TTypeExpr(mt) | TCast(_,Some mt) ->
  1830. check_module_type com mt e.epos
  1831. | TMeta((Meta.Deprecated,_,_) as meta,e1) ->
  1832. print_deprecation_message com meta "field" e1.epos;
  1833. expr e1;
  1834. | _ ->
  1835. Type.iter expr e
  1836. in
  1837. List.iter (fun t -> match t with
  1838. | TClassDecl c ->
  1839. curclass := c;
  1840. let field cf = match cf.cf_expr with None -> () | Some e -> expr e in
  1841. (match c.cl_constructor with None -> () | Some cf -> field cf);
  1842. (match c.cl_init with None -> () | Some e -> expr e);
  1843. List.iter field c.cl_ordered_statics;
  1844. List.iter field c.cl_ordered_fields;
  1845. | _ ->
  1846. ()
  1847. ) com.types
  1848. end
  1849. let interpolate_code com code tl f_string f_expr p =
  1850. let exprs = Array.of_list tl in
  1851. let i = ref 0 in
  1852. let err msg =
  1853. let pos = { p with pmin = p.pmin + !i } in
  1854. com.error msg pos
  1855. in
  1856. let regex = Str.regexp "[{}]" in
  1857. let rec loop m = match m with
  1858. | [] ->
  1859. ()
  1860. | Str.Text txt :: tl ->
  1861. i := !i + String.length txt;
  1862. f_string txt;
  1863. loop tl
  1864. | Str.Delim a :: Str.Delim b :: tl when a = b ->
  1865. i := !i + 2;
  1866. f_string a;
  1867. loop tl
  1868. | Str.Delim "{" :: Str.Text n :: Str.Delim "}" :: tl ->
  1869. (try
  1870. let expr = Array.get exprs (int_of_string n) in
  1871. f_expr expr;
  1872. i := !i + 2 + String.length n;
  1873. loop tl
  1874. with
  1875. | Failure "int_of_string" ->
  1876. err ("Index expected. Got " ^ n)
  1877. | Invalid_argument _ ->
  1878. err ("Out-of-bounds special parameter: " ^ n))
  1879. | Str.Delim x :: _ ->
  1880. err ("Unexpected " ^ x)
  1881. in
  1882. loop (Str.full_split regex code)
  1883. let map_source_header com f =
  1884. match Common.defined_value_safe com Define.SourceHeader with
  1885. | "" -> ()
  1886. | s -> f s
  1887. (* Collection of functions that return expressions *)
  1888. module ExprBuilder = struct
  1889. let make_static_this c p =
  1890. let ta = TAnon { a_fields = c.cl_statics; a_status = ref (Statics c) } in
  1891. mk (TTypeExpr (TClassDecl c)) ta p
  1892. let make_int com i p =
  1893. mk (TConst (TInt (Int32.of_int i))) com.basic.tint p
  1894. let make_float com f p =
  1895. mk (TConst (TFloat f)) com.basic.tfloat p
  1896. let make_null t p =
  1897. mk (TConst TNull) t p
  1898. let make_local v p =
  1899. mk (TLocal v) v.v_type p
  1900. let make_const_texpr com ct p = match ct with
  1901. | TString s -> mk (TConst (TString s)) com.basic.tstring p
  1902. | TInt i -> mk (TConst (TInt i)) com.basic.tint p
  1903. | TFloat f -> mk (TConst (TFloat f)) com.basic.tfloat p
  1904. | TBool b -> mk (TConst (TBool b)) com.basic.tbool p
  1905. | TNull -> mk (TConst TNull) (com.basic.tnull (mk_mono())) p
  1906. | _ -> error "Unsupported constant" p
  1907. end
  1908. (* Static extensions for classes *)
  1909. module ExtClass = struct
  1910. let add_cl_init c e = match c.cl_init with
  1911. | None -> c.cl_init <- Some e
  1912. | Some e' -> c.cl_init <- Some (concat e' e)
  1913. let add_static_init c cf e p =
  1914. let ethis = ExprBuilder.make_static_this c p in
  1915. let ef1 = mk (TField(ethis,FStatic(c,cf))) cf.cf_type p in
  1916. let e_assign = mk (TBinop(OpAssign,ef1,e)) e.etype p in
  1917. add_cl_init c e_assign
  1918. end