type.ml 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. open Ast
  23. type path = string list * string
  24. type field_kind =
  25. | Var of var_kind
  26. | Method of method_kind
  27. and var_kind = {
  28. v_read : var_access;
  29. v_write : var_access;
  30. }
  31. and var_access =
  32. | AccNormal
  33. | AccNo (* can't be accessed outside of the class itself and its subclasses *)
  34. | AccNever (* can't be accessed, even in subclasses *)
  35. | AccResolve (* call resolve("field") when accessed *)
  36. | AccCall (* perform a method call when accessed *)
  37. | AccInline (* similar to Normal but inline when accessed *)
  38. | AccRequire of string * string option (* set when @:require(cond) fails *)
  39. and method_kind =
  40. | MethNormal
  41. | MethInline
  42. | MethDynamic
  43. | MethMacro
  44. type t =
  45. | TMono of t option ref
  46. | TEnum of tenum * tparams
  47. | TInst of tclass * tparams
  48. | TType of tdef * tparams
  49. | TFun of (string * bool * t) list * t
  50. | TAnon of tanon
  51. | TDynamic of t
  52. | TLazy of (unit -> t) ref
  53. | TAbstract of tabstract * tparams
  54. and tparams = t list
  55. and type_params = (string * t) list
  56. and tconstant =
  57. | TInt of int32
  58. | TFloat of string
  59. | TString of string
  60. | TBool of bool
  61. | TNull
  62. | TThis
  63. | TSuper
  64. and tvar = {
  65. mutable v_id : int;
  66. mutable v_name : string;
  67. mutable v_type : t;
  68. mutable v_capture : bool;
  69. mutable v_extra : (type_params * texpr option) option;
  70. mutable v_meta : metadata;
  71. }
  72. and tfunc = {
  73. tf_args : (tvar * tconstant option) list;
  74. tf_type : t;
  75. tf_expr : texpr;
  76. }
  77. and anon_status =
  78. | Closed
  79. | Opened
  80. | Const
  81. | Statics of tclass
  82. | EnumStatics of tenum
  83. | AbstractStatics of tabstract
  84. and tanon = {
  85. mutable a_fields : (string, tclass_field) PMap.t;
  86. a_status : anon_status ref;
  87. }
  88. and texpr_expr =
  89. | TConst of tconstant
  90. | TLocal of tvar
  91. | TArray of texpr * texpr
  92. | TBinop of Ast.binop * texpr * texpr
  93. | TField of texpr * tfield_access
  94. | TTypeExpr of module_type
  95. | TParenthesis of texpr
  96. | TObjectDecl of (string * texpr) list
  97. | TArrayDecl of texpr list
  98. | TCall of texpr * texpr list
  99. | TNew of tclass * tparams * texpr list
  100. | TUnop of Ast.unop * Ast.unop_flag * texpr
  101. | TFunction of tfunc
  102. | TVar of tvar * texpr option
  103. | TBlock of texpr list
  104. | TFor of tvar * texpr * texpr
  105. | TIf of texpr * texpr * texpr option
  106. | TWhile of texpr * texpr * Ast.while_flag
  107. | TSwitch of texpr * (texpr list * texpr) list * texpr option
  108. | TPatMatch of decision_tree
  109. | TTry of texpr * (tvar * texpr) list
  110. | TReturn of texpr option
  111. | TBreak
  112. | TContinue
  113. | TThrow of texpr
  114. | TCast of texpr * module_type option
  115. | TMeta of metadata_entry * texpr
  116. | TEnumParameter of texpr * tenum_field * int
  117. and tfield_access =
  118. | FInstance of tclass * tclass_field
  119. | FStatic of tclass * tclass_field
  120. | FAnon of tclass_field
  121. | FDynamic of string
  122. | FClosure of tclass option * tclass_field (* None class = TAnon *)
  123. | FEnum of tenum * tenum_field
  124. and texpr = {
  125. eexpr : texpr_expr;
  126. etype : t;
  127. epos : Ast.pos;
  128. }
  129. and tclass_field = {
  130. cf_name : string;
  131. mutable cf_type : t;
  132. mutable cf_public : bool;
  133. cf_pos : pos;
  134. mutable cf_doc : Ast.documentation;
  135. mutable cf_meta : metadata;
  136. mutable cf_kind : field_kind;
  137. mutable cf_params : type_params;
  138. mutable cf_expr : texpr option;
  139. mutable cf_overloads : tclass_field list;
  140. }
  141. and tclass_kind =
  142. | KNormal
  143. | KTypeParameter of t list
  144. | KExtension of tclass * tparams
  145. | KExpr of Ast.expr
  146. | KGeneric
  147. | KGenericInstance of tclass * tparams
  148. | KMacroType
  149. | KGenericBuild of class_field list
  150. | KAbstractImpl of tabstract
  151. and metadata = Ast.metadata
  152. and tinfos = {
  153. mt_path : path;
  154. mt_module : module_def;
  155. mt_pos : Ast.pos;
  156. mt_private : bool;
  157. mt_doc : Ast.documentation;
  158. mutable mt_meta : metadata;
  159. mt_types : type_params;
  160. }
  161. and tclass = {
  162. mutable cl_path : path;
  163. mutable cl_module : module_def;
  164. mutable cl_pos : Ast.pos;
  165. mutable cl_private : bool;
  166. mutable cl_doc : Ast.documentation;
  167. mutable cl_meta : metadata;
  168. mutable cl_types : type_params;
  169. (* do not insert any fields above *)
  170. mutable cl_kind : tclass_kind;
  171. mutable cl_extern : bool;
  172. mutable cl_interface : bool;
  173. mutable cl_super : (tclass * tparams) option;
  174. mutable cl_implements : (tclass * tparams) list;
  175. mutable cl_fields : (string , tclass_field) PMap.t;
  176. mutable cl_statics : (string, tclass_field) PMap.t;
  177. mutable cl_ordered_statics : tclass_field list;
  178. mutable cl_ordered_fields : tclass_field list;
  179. mutable cl_dynamic : t option;
  180. mutable cl_array_access : t option;
  181. mutable cl_constructor : tclass_field option;
  182. mutable cl_init : texpr option;
  183. mutable cl_overrides : tclass_field list;
  184. mutable cl_build : unit -> unit;
  185. mutable cl_restore : unit -> unit;
  186. }
  187. and tenum_field = {
  188. ef_name : string;
  189. ef_type : t;
  190. ef_pos : Ast.pos;
  191. ef_doc : Ast.documentation;
  192. ef_index : int;
  193. ef_params : type_params;
  194. mutable ef_meta : metadata;
  195. }
  196. and tenum = {
  197. mutable e_path : path;
  198. e_module : module_def;
  199. e_pos : Ast.pos;
  200. e_private : bool;
  201. e_doc : Ast.documentation;
  202. mutable e_meta : metadata;
  203. mutable e_types : type_params;
  204. (* do not insert any fields above *)
  205. e_type : tdef;
  206. mutable e_extern : bool;
  207. mutable e_constrs : (string , tenum_field) PMap.t;
  208. mutable e_names : string list;
  209. }
  210. and tdef = {
  211. t_path : path;
  212. t_module : module_def;
  213. t_pos : Ast.pos;
  214. t_private : bool;
  215. t_doc : Ast.documentation;
  216. mutable t_meta : metadata;
  217. mutable t_types : type_params;
  218. (* do not insert any fields above *)
  219. mutable t_type : t;
  220. }
  221. and tabstract = {
  222. mutable a_path : path;
  223. a_module : module_def;
  224. a_pos : Ast.pos;
  225. a_private : bool;
  226. a_doc : Ast.documentation;
  227. mutable a_meta : metadata;
  228. mutable a_types : type_params;
  229. (* do not insert any fields above *)
  230. mutable a_ops : (Ast.binop * tclass_field) list;
  231. mutable a_unops : (Ast.unop * unop_flag * tclass_field) list;
  232. mutable a_impl : tclass option;
  233. mutable a_this : t;
  234. mutable a_from : (t * tclass_field option) list;
  235. mutable a_array : tclass_field list;
  236. mutable a_to : (t * tclass_field option) list;
  237. }
  238. and module_type =
  239. | TClassDecl of tclass
  240. | TEnumDecl of tenum
  241. | TTypeDecl of tdef
  242. | TAbstractDecl of tabstract
  243. and module_def = {
  244. m_id : int;
  245. m_path : path;
  246. mutable m_types : module_type list;
  247. m_extra : module_def_extra;
  248. }
  249. and module_def_extra = {
  250. m_file : string;
  251. m_sign : string;
  252. mutable m_time : float;
  253. mutable m_dirty : bool;
  254. mutable m_added : int;
  255. mutable m_mark : int;
  256. mutable m_deps : (int,module_def) PMap.t;
  257. mutable m_processed : int;
  258. mutable m_kind : module_kind;
  259. mutable m_binded_res : (string, string) PMap.t;
  260. mutable m_macro_calls : string list;
  261. mutable m_features : (string *(tclass * tclass_field * bool)) list;
  262. }
  263. and module_kind =
  264. | MCode
  265. | MMacro
  266. | MFake
  267. | MSub
  268. and dt =
  269. | DTSwitch of texpr * (texpr * dt) list * dt option
  270. | DTBind of ((tvar * pos) * texpr) list * dt
  271. | DTGoto of int
  272. | DTExpr of texpr
  273. | DTGuard of texpr * dt * dt option
  274. and decision_tree = {
  275. dt_dt_lookup : dt array;
  276. dt_first : int;
  277. dt_type : t;
  278. dt_var_init : (tvar * texpr option) list;
  279. dt_is_complex : bool;
  280. }
  281. (* ======= General utility ======= *)
  282. let alloc_var =
  283. let uid = ref 0 in
  284. (fun n t -> incr uid; { v_name = n; v_type = t; v_id = !uid; v_capture = false; v_extra = None; v_meta = [] })
  285. let alloc_mid =
  286. let mid = ref 0 in
  287. (fun() -> incr mid; !mid)
  288. let mk e t p = { eexpr = e; etype = t; epos = p }
  289. let mk_block e =
  290. match e.eexpr with
  291. | TBlock (_ :: _) -> e
  292. | _ -> mk (TBlock [e]) e.etype e.epos
  293. let null t p = mk (TConst TNull) t p
  294. let mk_mono() = TMono (ref None)
  295. let rec t_dynamic = TDynamic t_dynamic
  296. let tfun pl r = TFun (List.map (fun t -> "",false,t) pl,r)
  297. let fun_args l = List.map (fun (a,c,t) -> a, c <> None, t) l
  298. let mk_class m path pos =
  299. {
  300. cl_path = path;
  301. cl_module = m;
  302. cl_pos = pos;
  303. cl_doc = None;
  304. cl_meta = [];
  305. cl_private = false;
  306. cl_kind = KNormal;
  307. cl_extern = false;
  308. cl_interface = false;
  309. cl_types = [];
  310. cl_super = None;
  311. cl_implements = [];
  312. cl_fields = PMap.empty;
  313. cl_ordered_statics = [];
  314. cl_ordered_fields = [];
  315. cl_statics = PMap.empty;
  316. cl_dynamic = None;
  317. cl_array_access = None;
  318. cl_constructor = None;
  319. cl_init = None;
  320. cl_overrides = [];
  321. cl_build = (fun() -> ());
  322. cl_restore = (fun() -> ());
  323. }
  324. let module_extra file sign time kind =
  325. {
  326. m_file = file;
  327. m_sign = sign;
  328. m_dirty = false;
  329. m_added = 0;
  330. m_mark = 0;
  331. m_time = time;
  332. m_processed = 0;
  333. m_deps = PMap.empty;
  334. m_kind = kind;
  335. m_binded_res = PMap.empty;
  336. m_macro_calls = [];
  337. m_features = [];
  338. }
  339. let mk_field name t p = {
  340. cf_name = name;
  341. cf_type = t;
  342. cf_pos = p;
  343. cf_doc = None;
  344. cf_meta = [];
  345. cf_public = true;
  346. cf_kind = Var { v_read = AccNormal; v_write = AccNormal };
  347. cf_expr = None;
  348. cf_params = [];
  349. cf_overloads = [];
  350. }
  351. let null_module = {
  352. m_id = alloc_mid();
  353. m_path = [] , "";
  354. m_types = [];
  355. m_extra = module_extra "" "" 0. MFake;
  356. }
  357. let null_class =
  358. let c = mk_class null_module ([],"") Ast.null_pos in
  359. c.cl_private <- true;
  360. c
  361. let null_field = mk_field "" t_dynamic Ast.null_pos
  362. let add_dependency m mdep =
  363. if m != null_module && m != mdep then m.m_extra.m_deps <- PMap.add mdep.m_id mdep m.m_extra.m_deps
  364. let arg_name (a,_) = a.v_name
  365. let t_infos t : tinfos =
  366. match t with
  367. | TClassDecl c -> Obj.magic c
  368. | TEnumDecl e -> Obj.magic e
  369. | TTypeDecl t -> Obj.magic t
  370. | TAbstractDecl a -> Obj.magic a
  371. let t_path t = (t_infos t).mt_path
  372. let rec is_parent csup c =
  373. if c == csup || List.exists (fun (i,_) -> is_parent csup i) c.cl_implements then
  374. true
  375. else match c.cl_super with
  376. | None -> false
  377. | Some (c,_) -> is_parent csup c
  378. let map loop t =
  379. match t with
  380. | TMono r ->
  381. (match !r with
  382. | None -> t
  383. | Some t -> loop t) (* erase*)
  384. | TEnum (_,[]) | TInst (_,[]) | TType (_,[]) ->
  385. t
  386. | TEnum (e,tl) ->
  387. TEnum (e, List.map loop tl)
  388. | TInst (c,tl) ->
  389. TInst (c, List.map loop tl)
  390. | TType (t2,tl) ->
  391. TType (t2,List.map loop tl)
  392. | TAbstract (a,tl) ->
  393. TAbstract (a,List.map loop tl)
  394. | TFun (tl,r) ->
  395. TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
  396. | TAnon a ->
  397. TAnon {
  398. a_fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields;
  399. a_status = a.a_status;
  400. }
  401. | TLazy f ->
  402. let ft = !f() in
  403. let ft2 = loop ft in
  404. if ft == ft2 then t else ft2
  405. | TDynamic t2 ->
  406. if t == t2 then t else TDynamic (loop t2)
  407. (* substitute parameters with other types *)
  408. let apply_params cparams params t =
  409. match cparams with
  410. | [] -> t
  411. | _ ->
  412. let rec loop l1 l2 =
  413. match l1, l2 with
  414. | [] , [] -> []
  415. | (x,TLazy f) :: l1, _ -> loop ((x,(!f)()) :: l1) l2
  416. | (_,t1) :: l1 , t2 :: l2 -> (t1,t2) :: loop l1 l2
  417. | _ -> assert false
  418. in
  419. let subst = loop cparams params in
  420. let rec loop t =
  421. try
  422. List.assq t subst
  423. with Not_found ->
  424. match t with
  425. | TMono r ->
  426. (match !r with
  427. | None -> t
  428. | Some t -> loop t)
  429. | TEnum (e,tl) ->
  430. (match tl with
  431. | [] -> t
  432. | _ -> TEnum (e,List.map loop tl))
  433. | TType (t2,tl) ->
  434. (match tl with
  435. | [] -> t
  436. | _ -> TType (t2,List.map loop tl))
  437. | TAbstract (a,tl) ->
  438. (match tl with
  439. | [] -> t
  440. | _ -> TAbstract (a,List.map loop tl))
  441. | TInst (c,tl) ->
  442. (match tl with
  443. | [] ->
  444. t
  445. | [TMono r] ->
  446. (match !r with
  447. | Some tt when t == tt ->
  448. (* for dynamic *)
  449. let pt = mk_mono() in
  450. let t = TInst (c,[pt]) in
  451. (match pt with TMono r -> r := Some t | _ -> assert false);
  452. t
  453. | _ -> TInst (c,List.map loop tl))
  454. | _ ->
  455. TInst (c,List.map loop tl))
  456. | TFun (tl,r) ->
  457. TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
  458. | TAnon a ->
  459. TAnon {
  460. a_fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields;
  461. a_status = a.a_status;
  462. }
  463. | TLazy f ->
  464. let ft = !f() in
  465. let ft2 = loop ft in
  466. if ft == ft2 then
  467. t
  468. else
  469. ft2
  470. | TDynamic t2 ->
  471. if t == t2 then
  472. t
  473. else
  474. TDynamic (loop t2)
  475. in
  476. loop t
  477. let monomorphs eparams t =
  478. apply_params eparams (List.map (fun _ -> mk_mono()) eparams) t
  479. let rec follow t =
  480. match t with
  481. | TMono r ->
  482. (match !r with
  483. | Some t -> follow t
  484. | _ -> t)
  485. | TLazy f ->
  486. follow (!f())
  487. | TType (t,tl) ->
  488. follow (apply_params t.t_types tl t.t_type)
  489. | _ -> t
  490. let rec is_nullable ?(no_lazy=false) = function
  491. | TMono r ->
  492. (match !r with None -> false | Some t -> is_nullable t)
  493. | TType ({ t_path = ([],"Null") },[_]) ->
  494. true
  495. | TLazy f ->
  496. if no_lazy then raise Exit else is_nullable (!f())
  497. | TType (t,tl) ->
  498. is_nullable (apply_params t.t_types tl t.t_type)
  499. | TFun _ ->
  500. false
  501. (*
  502. Type parameters will most of the time be nullable objects, so we don't want to make it hard for users
  503. to have to specify Null<T> all over the place, so while they could be a basic type, let's assume they will not.
  504. This will still cause issues with inlining and haxe.rtti.Generic. In that case proper explicit Null<T> is required to
  505. work correctly with basic types. This could still be fixed by redoing a nullability inference on the typed AST.
  506. | TInst ({ cl_kind = KTypeParameter },_) -> false
  507. *)
  508. | TAbstract (a,_) when Meta.has Meta.CoreType a.a_meta ->
  509. not (Meta.has Meta.NotNull a.a_meta)
  510. | TAbstract (a,tl) ->
  511. is_nullable (apply_params a.a_types tl a.a_this)
  512. | _ ->
  513. true
  514. let rec is_null = function
  515. | TMono r ->
  516. (match !r with None -> false | Some t -> is_null t)
  517. | TType ({ t_path = ([],"Null") },[t]) ->
  518. not (is_nullable (follow t))
  519. | TLazy f ->
  520. is_null (!f())
  521. | TType (t,tl) ->
  522. is_null (apply_params t.t_types tl t.t_type)
  523. | _ ->
  524. false
  525. let rec has_mono t = match t with
  526. | TMono r ->
  527. (match !r with None -> true | Some t -> has_mono t)
  528. | TInst(_,pl) | TEnum(_,pl) | TAbstract(_,pl) | TType(_,pl) ->
  529. List.exists has_mono pl
  530. | TDynamic _ ->
  531. false
  532. | TFun(args,r) ->
  533. has_mono r || List.exists (fun (_,_,t) -> has_mono t) args
  534. | TAnon a ->
  535. PMap.fold (fun cf b -> has_mono cf.cf_type || b) a.a_fields false
  536. | TLazy r ->
  537. has_mono (!r())
  538. let concat e1 e2 =
  539. let e = (match e1.eexpr, e2.eexpr with
  540. | TBlock el1, TBlock el2 -> TBlock (el1@el2)
  541. | TBlock el, _ -> TBlock (el @ [e2])
  542. | _, TBlock el -> TBlock (e1 :: el)
  543. | _ , _ -> TBlock [e1;e2]
  544. ) in
  545. mk e e2.etype (punion e1.epos e2.epos)
  546. (* ======= Field utility ======= *)
  547. let field_name f =
  548. match f with
  549. | FAnon f | FInstance (_,f) | FStatic (_,f) | FClosure (_,f) -> f.cf_name
  550. | FEnum (_,f) -> f.ef_name
  551. | FDynamic n -> n
  552. let extract_field = function
  553. | FAnon f | FInstance (_,f) | FStatic (_,f) | FClosure (_,f) -> Some f
  554. | _ -> None
  555. let is_extern_field f =
  556. match f.cf_kind with
  557. | Method _ -> false
  558. | Var { v_read = AccNormal | AccInline | AccNo } | Var { v_write = AccNormal | AccNo } -> false
  559. | _ -> not (Meta.has Meta.IsVar f.cf_meta)
  560. let field_type f =
  561. match f.cf_params with
  562. | [] -> f.cf_type
  563. | l -> monomorphs l f.cf_type
  564. let rec raw_class_field build_type c i =
  565. try
  566. let f = PMap.find i c.cl_fields in
  567. Some c, build_type f , f
  568. with Not_found -> try (match c.cl_constructor with
  569. | Some ctor when i = "new" -> Some c, build_type ctor,ctor
  570. | _ -> raise Not_found)
  571. with Not_found -> try
  572. match c.cl_super with
  573. | None ->
  574. raise Not_found
  575. | Some (c,tl) ->
  576. let c2 , t , f = raw_class_field build_type c i in
  577. c2, apply_params c.cl_types tl t , f
  578. with Not_found ->
  579. match c.cl_kind with
  580. | KTypeParameter tl ->
  581. let rec loop = function
  582. | [] ->
  583. raise Not_found
  584. | t :: ctl ->
  585. match follow t with
  586. | TAnon a ->
  587. (try
  588. let f = PMap.find i a.a_fields in
  589. None, build_type f, f
  590. with
  591. Not_found -> loop ctl)
  592. | TInst (c,pl) ->
  593. (try
  594. let c2, t , f = raw_class_field build_type c i in
  595. c2, apply_params c.cl_types pl t, f
  596. with
  597. Not_found -> loop ctl)
  598. | _ ->
  599. loop ctl
  600. in
  601. loop tl
  602. | _ ->
  603. if not c.cl_interface then raise Not_found;
  604. (*
  605. an interface can implements other interfaces without
  606. having to redeclare its fields
  607. *)
  608. let rec loop = function
  609. | [] ->
  610. raise Not_found
  611. | (c,tl) :: l ->
  612. try
  613. let c2, t , f = raw_class_field build_type c i in
  614. c2, apply_params c.cl_types tl t, f
  615. with
  616. Not_found -> loop l
  617. in
  618. loop c.cl_implements
  619. let class_field = raw_class_field field_type
  620. let quick_field t n =
  621. match follow t with
  622. | TInst (c,_) ->
  623. let c, _, f = raw_class_field (fun f -> f.cf_type) c n in
  624. (match c with None -> FAnon f | Some c -> FInstance (c,f))
  625. | TAnon a ->
  626. (match !(a.a_status) with
  627. | EnumStatics e ->
  628. let ef = PMap.find n e.e_constrs in
  629. FEnum(e,ef)
  630. | Statics c ->
  631. FStatic (c,PMap.find n c.cl_statics)
  632. | AbstractStatics a ->
  633. begin match a.a_impl with
  634. | Some c ->
  635. let cf = PMap.find n c.cl_statics in
  636. FStatic(c,cf) (* is that right? *)
  637. | _ ->
  638. raise Not_found
  639. end
  640. | _ ->
  641. FAnon (PMap.find n a.a_fields))
  642. | TDynamic _ ->
  643. FDynamic n
  644. | TEnum _ | TMono _ | TAbstract _ | TFun _ ->
  645. raise Not_found
  646. | TLazy _ | TType _ ->
  647. assert false
  648. let quick_field_dynamic t s =
  649. try quick_field t s
  650. with Not_found -> FDynamic s
  651. let rec get_constructor build_type c =
  652. match c.cl_constructor, c.cl_super with
  653. | Some c, _ -> build_type c, c
  654. | None, None -> raise Not_found
  655. | None, Some (csup,cparams) ->
  656. let t, c = get_constructor build_type csup in
  657. apply_params csup.cl_types cparams t, c
  658. (* ======= Unification ======= *)
  659. let rec link e a b =
  660. (* tell if setting a == b will create a type-loop *)
  661. let rec loop t =
  662. if t == a then
  663. true
  664. else match t with
  665. | TMono t -> (match !t with None -> false | Some t -> loop t)
  666. | TEnum (_,tl) -> List.exists loop tl
  667. | TInst (_,tl) | TType (_,tl) | TAbstract (_,tl) -> List.exists loop tl
  668. | TFun (tl,t) -> List.exists (fun (_,_,t) -> loop t) tl || loop t
  669. | TDynamic t2 ->
  670. if t == t2 then
  671. false
  672. else
  673. loop t2
  674. | TLazy f ->
  675. loop (!f())
  676. | TAnon a ->
  677. try
  678. PMap.iter (fun _ f -> if loop f.cf_type then raise Exit) a.a_fields;
  679. false
  680. with
  681. Exit -> true
  682. in
  683. (* tell is already a ~= b *)
  684. if loop b then
  685. (follow b) == a
  686. else if b == t_dynamic then
  687. true
  688. else begin
  689. e := Some b;
  690. true
  691. end
  692. let rec fast_eq a b =
  693. if a == b then
  694. true
  695. else match a , b with
  696. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  697. List.for_all2 (fun (_,_,t1) (_,_,t2) -> fast_eq t1 t2) l1 l2 && fast_eq r1 r2
  698. | TType (t1,l1), TType (t2,l2) ->
  699. t1 == t2 && List.for_all2 fast_eq l1 l2
  700. | TEnum (e1,l1), TEnum (e2,l2) ->
  701. e1 == e2 && List.for_all2 fast_eq l1 l2
  702. | TInst (c1,l1), TInst (c2,l2) ->
  703. c1 == c2 && List.for_all2 fast_eq l1 l2
  704. | TAbstract (a1,l1), TAbstract (a2,l2) ->
  705. a1 == a2 && List.for_all2 fast_eq l1 l2
  706. | _ , _ ->
  707. false
  708. (* perform unification with subtyping.
  709. the first type is always the most down in the class hierarchy
  710. it's also the one that is pointed by the position.
  711. It's actually a typecheck of A :> B where some mutations can happen *)
  712. type unify_error =
  713. | Cannot_unify of t * t
  714. | Invalid_field_type of string
  715. | Has_no_field of t * string
  716. | Has_no_runtime_field of t * string
  717. | Has_extra_field of t * string
  718. | Invalid_kind of string * field_kind * field_kind
  719. | Invalid_visibility of string
  720. | Not_matching_optional of string
  721. | Cant_force_optional
  722. | Invariant_parameter of t * t
  723. | Constraint_failure of string
  724. | Missing_overload of tclass_field * t
  725. | Unify_custom of string
  726. exception Unify_error of unify_error list
  727. let cannot_unify a b = Cannot_unify (a,b)
  728. let invalid_field n = Invalid_field_type n
  729. let invalid_kind n a b = Invalid_kind (n,a,b)
  730. let invalid_visibility n = Invalid_visibility n
  731. let has_no_field t n = Has_no_field (t,n)
  732. let has_extra_field t n = Has_extra_field (t,n)
  733. let error l = raise (Unify_error l)
  734. let has_meta m ml = List.exists (fun (m2,_,_) -> m = m2) ml
  735. let get_meta m ml = List.find (fun (m2,_,_) -> m = m2) ml
  736. let no_meta = []
  737. (*
  738. we can restrict access as soon as both are runtime-compatible
  739. *)
  740. let unify_access a1 a2 =
  741. a1 = a2 || match a1, a2 with
  742. | _, AccNo | _, AccNever -> true
  743. | AccInline, AccNormal -> true
  744. | _ -> false
  745. let direct_access = function
  746. | AccNo | AccNever | AccNormal | AccInline | AccRequire _ -> true
  747. | AccResolve | AccCall -> false
  748. let unify_kind k1 k2 =
  749. k1 = k2 || match k1, k2 with
  750. | Var v1, Var v2 -> unify_access v1.v_read v2.v_read && unify_access v1.v_write v2.v_write
  751. | Var v, Method m ->
  752. (match v.v_read, v.v_write, m with
  753. | AccNormal, _, MethNormal -> true
  754. | AccNormal, AccNormal, MethDynamic -> true
  755. | _ -> false)
  756. | Method m, Var v ->
  757. (match m with
  758. | MethDynamic -> direct_access v.v_read && direct_access v.v_write
  759. | MethMacro -> false
  760. | MethNormal | MethInline ->
  761. match v.v_write with
  762. | AccNo | AccNever -> true
  763. | _ -> false)
  764. | Method m1, Method m2 ->
  765. match m1,m2 with
  766. | MethInline, MethNormal
  767. | MethDynamic, MethNormal -> true
  768. | _ -> false
  769. let eq_stack = ref []
  770. type eq_kind =
  771. | EqStrict
  772. | EqCoreType
  773. | EqRightDynamic
  774. | EqBothDynamic
  775. let is_closed a = !(a.a_status) <> Opened
  776. let rec type_eq param a b =
  777. if a == b then
  778. ()
  779. else match a , b with
  780. | TLazy f , _ -> type_eq param (!f()) b
  781. | _ , TLazy f -> type_eq param a (!f())
  782. | TMono t , _ ->
  783. (match !t with
  784. | None -> if param = EqCoreType || not (link t a b) then error [cannot_unify a b]
  785. | Some t -> type_eq param t b)
  786. | _ , TMono t ->
  787. (match !t with
  788. | None -> if param = EqCoreType || not (link t b a) then error [cannot_unify a b]
  789. | Some t -> type_eq param a t)
  790. | TType (t1,tl1), TType (t2,tl2) when (t1 == t2 || (param = EqCoreType && t1.t_path = t2.t_path)) && List.length tl1 = List.length tl2 ->
  791. List.iter2 (type_eq param) tl1 tl2
  792. | TType (t,tl) , _ when param <> EqCoreType ->
  793. type_eq param (apply_params t.t_types tl t.t_type) b
  794. | _ , TType (t,tl) when param <> EqCoreType ->
  795. if List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!eq_stack) then
  796. ()
  797. else begin
  798. eq_stack := (a,b) :: !eq_stack;
  799. try
  800. type_eq param a (apply_params t.t_types tl t.t_type);
  801. eq_stack := List.tl !eq_stack;
  802. with
  803. Unify_error l ->
  804. eq_stack := List.tl !eq_stack;
  805. error (cannot_unify a b :: l)
  806. end
  807. | TEnum (e1,tl1) , TEnum (e2,tl2) ->
  808. if e1 != e2 && not (param = EqCoreType && e1.e_path = e2.e_path) then error [cannot_unify a b];
  809. List.iter2 (type_eq param) tl1 tl2
  810. | TInst (c1,tl1) , TInst (c2,tl2) ->
  811. if c1 != c2 && not (param = EqCoreType && c1.cl_path = c2.cl_path) && (match c1.cl_kind, c2.cl_kind with KExpr _, KExpr _ -> false | _ -> true) then error [cannot_unify a b];
  812. List.iter2 (type_eq param) tl1 tl2
  813. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  814. (try
  815. type_eq param r1 r2;
  816. List.iter2 (fun (n,o1,t1) (_,o2,t2) ->
  817. if o1 <> o2 then error [Not_matching_optional n];
  818. type_eq param t1 t2
  819. ) l1 l2
  820. with
  821. Unify_error l -> error (cannot_unify a b :: l))
  822. | TDynamic a , TDynamic b ->
  823. type_eq param a b
  824. | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
  825. if a1 != a2 && not (param = EqCoreType && a1.a_path = a2.a_path) then error [cannot_unify a b];
  826. List.iter2 (type_eq param) tl1 tl2
  827. | TAnon a1, TAnon a2 ->
  828. (try
  829. PMap.iter (fun n f1 ->
  830. try
  831. let f2 = PMap.find n a2.a_fields in
  832. if f1.cf_kind <> f2.cf_kind && (param = EqStrict || param = EqCoreType || not (unify_kind f1.cf_kind f2.cf_kind)) then error [invalid_kind n f1.cf_kind f2.cf_kind];
  833. try
  834. type_eq param f1.cf_type f2.cf_type
  835. with
  836. Unify_error l -> error (invalid_field n :: l)
  837. with
  838. Not_found ->
  839. if is_closed a2 then error [has_no_field b n];
  840. if not (link (ref None) b f1.cf_type) then error [cannot_unify a b];
  841. a2.a_fields <- PMap.add n f1 a2.a_fields
  842. ) a1.a_fields;
  843. PMap.iter (fun n f2 ->
  844. if not (PMap.mem n a1.a_fields) then begin
  845. if is_closed a1 then error [has_no_field a n];
  846. if not (link (ref None) a f2.cf_type) then error [cannot_unify a b];
  847. a1.a_fields <- PMap.add n f2 a1.a_fields
  848. end;
  849. ) a2.a_fields;
  850. with
  851. Unify_error l -> error (cannot_unify a b :: l))
  852. | _ , _ ->
  853. if b == t_dynamic && (param = EqRightDynamic || param = EqBothDynamic) then
  854. ()
  855. else if a == t_dynamic && param = EqBothDynamic then
  856. ()
  857. else
  858. error [cannot_unify a b]
  859. let type_iseq a b =
  860. try
  861. type_eq EqStrict a b;
  862. true
  863. with
  864. Unify_error _ -> false
  865. let unify_stack = ref []
  866. let abstract_cast_stack = ref []
  867. let rec unify a b =
  868. if a == b then
  869. ()
  870. else match a, b with
  871. | TLazy f , _ -> unify (!f()) b
  872. | _ , TLazy f -> unify a (!f())
  873. | TMono t , _ ->
  874. (match !t with
  875. | None -> if not (link t a b) then error [cannot_unify a b]
  876. | Some t -> unify t b)
  877. | _ , TMono t ->
  878. (match !t with
  879. | None -> if not (link t b a) then error [cannot_unify a b]
  880. | Some t -> unify a t)
  881. | TType (t,tl) , _ ->
  882. if not (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!unify_stack)) then begin
  883. try
  884. unify_stack := (a,b) :: !unify_stack;
  885. unify (apply_params t.t_types tl t.t_type) b;
  886. unify_stack := List.tl !unify_stack;
  887. with
  888. Unify_error l ->
  889. unify_stack := List.tl !unify_stack;
  890. error (cannot_unify a b :: l)
  891. end
  892. | _ , TType (t,tl) ->
  893. if not (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!unify_stack)) then begin
  894. try
  895. unify_stack := (a,b) :: !unify_stack;
  896. unify a (apply_params t.t_types tl t.t_type);
  897. unify_stack := List.tl !unify_stack;
  898. with
  899. Unify_error l ->
  900. unify_stack := List.tl !unify_stack;
  901. error (cannot_unify a b :: l)
  902. end
  903. | TEnum (ea,tl1) , TEnum (eb,tl2) ->
  904. if ea != eb then error [cannot_unify a b];
  905. unify_types a b tl1 tl2
  906. | TAbstract (a1,tl1) , TAbstract (a2,tl2) when a1 == a2 ->
  907. unify_types a b tl1 tl2
  908. | TAbstract ({a_path=[],"Void"},_) , _
  909. | _ , TAbstract ({a_path=[],"Void"},_) ->
  910. error [cannot_unify a b]
  911. | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
  912. let f1 = unify_to_field a1 tl1 b in
  913. let f2 = unify_from_field a2 tl2 a b in
  914. if not (List.exists (f1 ~allow_transitive_cast:false) a1.a_to) && not (List.exists (f2 ~allow_transitive_cast:false) a2.a_from)
  915. && not (List.exists f1 a1.a_to) && not (List.exists f2 a2.a_from) then error [cannot_unify a b]
  916. | TInst (c1,tl1) , TInst (c2,tl2) ->
  917. let rec loop c tl =
  918. if c == c2 then begin
  919. unify_types a b tl tl2;
  920. true
  921. end else (match c.cl_super with
  922. | None -> false
  923. | Some (cs,tls) ->
  924. loop cs (List.map (apply_params c.cl_types tl) tls)
  925. ) || List.exists (fun (cs,tls) ->
  926. loop cs (List.map (apply_params c.cl_types tl) tls)
  927. ) c.cl_implements
  928. || (match c.cl_kind with
  929. | KTypeParameter pl -> List.exists (fun t -> match follow t with TInst (cs,tls) -> loop cs (List.map (apply_params c.cl_types tl) tls) | _ -> false) pl
  930. | _ -> false)
  931. in
  932. if not (loop c1 tl1) then error [cannot_unify a b]
  933. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  934. let i = ref 0 in
  935. (try
  936. (match r2 with
  937. | TAbstract ({a_path=[],"Void"},_) -> incr i
  938. | _ -> unify r1 r2; incr i);
  939. List.iter2 (fun (_,o1,t1) (_,o2,t2) ->
  940. if o1 && not o2 then error [Cant_force_optional];
  941. unify t1 t2;
  942. incr i
  943. ) l2 l1 (* contravariance *)
  944. with
  945. Unify_error l ->
  946. let msg = if !i = 0 then "Cannot unify return types" else "Cannot unify argument " ^ (string_of_int !i) in
  947. error (cannot_unify a b :: Unify_custom msg :: l))
  948. | TInst (c,tl) , TAnon an ->
  949. if PMap.is_empty an.a_fields then (match c.cl_kind with
  950. | KTypeParameter pl ->
  951. (* one of the constraints must unify with { } *)
  952. if not (List.exists (fun t -> match t with TInst _ | TAnon _ -> true | _ -> false) pl) then error [cannot_unify a b]
  953. | _ -> ());
  954. (try
  955. PMap.iter (fun n f2 ->
  956. let _, ft, f1 = (try class_field c n with Not_found -> error [has_no_field a n]) in
  957. if not (unify_kind f1.cf_kind f2.cf_kind) then error [invalid_kind n f1.cf_kind f2.cf_kind];
  958. if f2.cf_public && not f1.cf_public then error [invalid_visibility n];
  959. (try
  960. unify_with_access (apply_params c.cl_types tl ft) f2
  961. with
  962. Unify_error l -> error (invalid_field n :: l));
  963. List.iter (fun f2o ->
  964. if not (List.exists (fun f1o -> type_iseq f1o.cf_type f2o.cf_type) (f1 :: f1.cf_overloads))
  965. then error [Missing_overload (f1, f2o.cf_type)]
  966. ) f2.cf_overloads;
  967. (* we mark the field as :?used because it might be used through the structure *)
  968. if not (Meta.has Meta.MaybeUsed f1.cf_meta) then f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta;
  969. (match f1.cf_kind with
  970. | Method MethInline ->
  971. if (c.cl_extern || Meta.has Meta.Extern f1.cf_meta) && not (Meta.has Meta.Runtime f1.cf_meta) then error [Has_no_runtime_field (a,n)];
  972. | _ -> ());
  973. ) an.a_fields;
  974. (match !(an.a_status) with
  975. | Opened -> an.a_status := Closed;
  976. | Statics _ | EnumStatics _ | AbstractStatics _ -> error []
  977. | Closed | Const -> ())
  978. with
  979. Unify_error l -> error (cannot_unify a b :: l))
  980. | TAnon a1, TAnon a2 ->
  981. (try
  982. PMap.iter (fun n f2 ->
  983. try
  984. let f1 = PMap.find n a1.a_fields in
  985. if not (unify_kind f1.cf_kind f2.cf_kind) then
  986. (match !(a1.a_status), f1.cf_kind, f2.cf_kind with
  987. | Opened, Var { v_read = AccNormal; v_write = AccNo }, Var { v_read = AccNormal; v_write = AccNormal } ->
  988. f1.cf_kind <- f2.cf_kind;
  989. | _ -> error [invalid_kind n f1.cf_kind f2.cf_kind]);
  990. if f2.cf_public && not f1.cf_public then error [invalid_visibility n];
  991. try
  992. unify_with_access f1.cf_type f2;
  993. (match !(a1.a_status) with
  994. | Statics c when not (Meta.has Meta.MaybeUsed f1.cf_meta) -> f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta
  995. | _ -> ());
  996. with
  997. Unify_error l -> error (invalid_field n :: l)
  998. with
  999. Not_found ->
  1000. match !(a1.a_status) with
  1001. | Opened ->
  1002. if not (link (ref None) a f2.cf_type) then error [];
  1003. a1.a_fields <- PMap.add n f2 a1.a_fields
  1004. | Const when Meta.has Meta.Optional f2.cf_meta ->
  1005. ()
  1006. | _ ->
  1007. error [has_no_field a n];
  1008. ) a2.a_fields;
  1009. (match !(a1.a_status) with
  1010. | Const when not (PMap.is_empty a2.a_fields) ->
  1011. PMap.iter (fun n _ -> if not (PMap.mem n a2.a_fields) then error [has_extra_field a n]) a1.a_fields;
  1012. | Opened ->
  1013. a1.a_status := Closed
  1014. | _ -> ());
  1015. (match !(a2.a_status) with
  1016. | Statics c -> (match !(a1.a_status) with Statics c2 when c == c2 -> () | _ -> error [])
  1017. | EnumStatics e -> (match !(a1.a_status) with EnumStatics e2 when e == e2 -> () | _ -> error [])
  1018. | AbstractStatics a -> (match !(a1.a_status) with AbstractStatics a2 when a == a2 -> () | _ -> error [])
  1019. | Opened -> a2.a_status := Closed
  1020. | Const | Closed -> ())
  1021. with
  1022. Unify_error l -> error (cannot_unify a b :: l))
  1023. | TAnon an, TAbstract ({ a_path = [],"Class" },[pt]) ->
  1024. (match !(an.a_status) with
  1025. | Statics cl -> unify (TInst (cl,List.map (fun _ -> mk_mono()) cl.cl_types)) pt
  1026. | _ -> error [cannot_unify a b])
  1027. | TAnon an, TAbstract ({ a_path = [],"Enum" },[pt]) ->
  1028. (match !(an.a_status) with
  1029. | EnumStatics e -> unify (TEnum (e,List.map (fun _ -> mk_mono()) e.e_types)) pt
  1030. | _ -> error [cannot_unify a b])
  1031. | TEnum _, TAbstract ({ a_path = [],"EnumValue" },[]) ->
  1032. ()
  1033. | TEnum(en,_), TAbstract ({ a_path = ["haxe"],"FlatEnum" },[]) when Meta.has Meta.FlatEnum en.e_meta ->
  1034. ()
  1035. | TFun _, TAbstract ({ a_path = ["haxe"],"Function" },[]) ->
  1036. ()
  1037. | TDynamic t , _ ->
  1038. if t == a then
  1039. ()
  1040. else (match b with
  1041. | TDynamic t2 ->
  1042. if t2 != b then
  1043. (try
  1044. type_eq EqRightDynamic t t2
  1045. with
  1046. Unify_error l -> error (cannot_unify a b :: l));
  1047. | _ ->
  1048. error [cannot_unify a b])
  1049. | _ , TDynamic t ->
  1050. if t == b then
  1051. ()
  1052. else (match a with
  1053. | TDynamic t2 ->
  1054. if t2 != a then
  1055. (try
  1056. type_eq EqRightDynamic t t2
  1057. with
  1058. Unify_error l -> error (cannot_unify a b :: l));
  1059. | TAnon an ->
  1060. (try
  1061. (match !(an.a_status) with
  1062. | Statics _ | EnumStatics _ -> error []
  1063. | Opened -> an.a_status := Closed
  1064. | _ -> ());
  1065. PMap.iter (fun _ f ->
  1066. try
  1067. type_eq EqStrict (field_type f) t
  1068. with Unify_error l ->
  1069. error (invalid_field f.cf_name :: l)
  1070. ) an.a_fields
  1071. with Unify_error l ->
  1072. error (cannot_unify a b :: l))
  1073. | _ ->
  1074. error [cannot_unify a b])
  1075. | TAbstract (aa,tl), _ ->
  1076. if not (List.exists (unify_to_field aa tl b) aa.a_to) then error [cannot_unify a b];
  1077. | TInst ({ cl_kind = KTypeParameter ctl } as c,pl), TAbstract (bb,tl) ->
  1078. (* one of the constraints must satisfy the abstract *)
  1079. if not (List.exists (fun t ->
  1080. let t = apply_params c.cl_types pl t in
  1081. try unify t b; true with Unify_error _ -> false
  1082. ) ctl) && not (List.exists (unify_from_field bb tl a b) bb.a_from) then error [cannot_unify a b];
  1083. | _, TAbstract (bb,tl) ->
  1084. if not (List.exists (unify_from_field bb tl a b) bb.a_from) then error [cannot_unify a b]
  1085. | _ , _ ->
  1086. error [cannot_unify a b]
  1087. and unify_from_field ab tl a b ?(allow_transitive_cast=true) (t,cfo) =
  1088. if (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!abstract_cast_stack)) then false else begin
  1089. abstract_cast_stack := (a,b) :: !abstract_cast_stack;
  1090. let unify_func = match follow a with TAbstract({a_impl = Some _},_) when ab.a_impl <> None || not allow_transitive_cast -> type_eq EqStrict | _ -> unify in
  1091. let b = try begin match cfo with
  1092. | Some cf -> (match follow cf.cf_type with
  1093. | TFun(_,r) ->
  1094. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  1095. let map t = apply_params ab.a_types tl (apply_params cf.cf_params monos t) in
  1096. unify_func a (map t);
  1097. List.iter2 (fun m (name,t) -> match follow t with
  1098. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  1099. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> unify m (map tc) ) constr
  1100. | _ -> ()
  1101. ) monos cf.cf_params;
  1102. unify (map r) b;
  1103. | _ -> assert false)
  1104. | _ ->
  1105. unify_func a (apply_params ab.a_types tl t)
  1106. end;
  1107. true
  1108. with Unify_error _ -> false
  1109. in
  1110. abstract_cast_stack := List.tl !abstract_cast_stack;
  1111. b
  1112. end
  1113. and unify_to_field ab tl b ?(allow_transitive_cast=true) (t,cfo) =
  1114. let a = TAbstract(ab,tl) in
  1115. if (List.exists (fun (b2,a2) -> fast_eq a a2 && fast_eq b b2) (!abstract_cast_stack)) then false else begin
  1116. abstract_cast_stack := (b,a) :: !abstract_cast_stack;
  1117. let unify_func = match follow b with
  1118. | TAbstract(ab2,_) when not (Meta.has Meta.CoreType ab.a_meta) || not (Meta.has Meta.CoreType ab2.a_meta) || not allow_transitive_cast ->
  1119. type_eq EqStrict
  1120. | _ ->
  1121. unify
  1122. in
  1123. let r = try begin match cfo with
  1124. | Some cf -> (match follow cf.cf_type with
  1125. | TFun((_,_,ta) :: _,_) ->
  1126. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  1127. let map t = apply_params ab.a_types tl (apply_params cf.cf_params monos t) in
  1128. let athis = map ab.a_this in
  1129. (* we cannot allow implicit casts when the this type is not completely known yet *)
  1130. if has_mono athis then raise (Unify_error []);
  1131. with_variance (type_eq EqStrict) athis (map ta);
  1132. (* immediate constraints checking is ok here because we know there are no monomorphs *)
  1133. List.iter2 (fun m (name,t) -> match follow t with
  1134. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  1135. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> unify m (map tc) ) constr
  1136. | _ -> ()
  1137. ) monos cf.cf_params;
  1138. unify_func (map t) b;
  1139. | _ -> assert false)
  1140. | _ ->
  1141. unify_func (apply_params ab.a_types tl t) b;
  1142. end;
  1143. true
  1144. with Unify_error _ -> false
  1145. in
  1146. abstract_cast_stack := List.tl !abstract_cast_stack;
  1147. r
  1148. end
  1149. and unify_with_variance t1 t2 =
  1150. let allows_variance_to t (tf,cfo) = match cfo with
  1151. | None -> type_iseq tf t
  1152. | Some _ -> false
  1153. in
  1154. match follow t1,follow t2 with
  1155. | TInst(c1,tl1),TInst(c2,tl2) when c1 == c2 ->
  1156. List.iter2 unify_with_variance tl1 tl2
  1157. | TEnum(en1,tl1),TEnum(en2,tl2) when en1 == en2 ->
  1158. List.iter2 unify_with_variance tl1 tl2
  1159. | TAbstract(a1,pl1),TAbstract(a2,pl2) ->
  1160. let ta1 = apply_params a1.a_types pl1 a1.a_this in
  1161. let ta2 = apply_params a2.a_types pl2 a2.a_this in
  1162. if (Meta.has Meta.CoreType a1.a_meta) && (Meta.has Meta.CoreType a2.a_meta) then
  1163. type_eq EqStrict ta1 ta2;
  1164. if not (List.exists (allows_variance_to ta2) a1.a_to) && not (List.exists (allows_variance_to ta1) a2.a_from) then
  1165. error [cannot_unify t1 t2]
  1166. | TAbstract(a,pl),t ->
  1167. type_eq EqStrict (apply_params a.a_types pl a.a_this) t;
  1168. if not (List.exists (allows_variance_to t) a.a_to) then error [cannot_unify t1 t2]
  1169. | t,TAbstract(a,pl) ->
  1170. type_eq EqStrict t (apply_params a.a_types pl a.a_this);
  1171. if not (List.exists (allows_variance_to t) a.a_from) then error [cannot_unify t1 t2]
  1172. | _ ->
  1173. error [cannot_unify t1 t2]
  1174. and unify_types a b tl1 tl2 =
  1175. List.iter2 (fun t1 t2 ->
  1176. try
  1177. with_variance (type_eq EqRightDynamic) t1 t2
  1178. with Unify_error l ->
  1179. let err = cannot_unify a b in
  1180. error (err :: (Invariant_parameter (t1,t2)) :: l)
  1181. ) tl1 tl2
  1182. and with_variance f t1 t2 =
  1183. try
  1184. f t1 t2
  1185. with Unify_error l -> try
  1186. unify_with_variance t1 t2
  1187. with Unify_error _ ->
  1188. raise (Unify_error l)
  1189. and unify_with_access t1 f2 =
  1190. match f2.cf_kind with
  1191. (* write only *)
  1192. | Var { v_read = AccNo } | Var { v_read = AccNever } -> unify f2.cf_type t1
  1193. (* read only *)
  1194. | Method MethNormal | Method MethInline | Var { v_write = AccNo } | Var { v_write = AccNever } -> unify t1 f2.cf_type
  1195. (* read/write *)
  1196. | _ -> type_eq EqBothDynamic t1 f2.cf_type
  1197. (* ======= Mapping and iterating ======= *)
  1198. let iter_dt f dt = match dt with
  1199. | DTBind(_,dt) -> f dt
  1200. | DTSwitch(_,cl,dto) ->
  1201. List.iter (fun (_,dt) -> f dt) cl;
  1202. (match dto with None -> () | Some dt -> f dt)
  1203. | DTGuard(_,dt1,dt2) ->
  1204. f dt1;
  1205. (match dt2 with None -> () | Some dt -> f dt)
  1206. | DTGoto _ | DTExpr _ -> ()
  1207. let iter f e =
  1208. match e.eexpr with
  1209. | TConst _
  1210. | TLocal _
  1211. | TBreak
  1212. | TContinue
  1213. | TTypeExpr _ ->
  1214. ()
  1215. | TArray (e1,e2)
  1216. | TBinop (_,e1,e2)
  1217. | TFor (_,e1,e2)
  1218. | TWhile (e1,e2,_) ->
  1219. f e1;
  1220. f e2;
  1221. | TThrow e
  1222. | TField (e,_)
  1223. | TEnumParameter (e,_,_)
  1224. | TParenthesis e
  1225. | TCast (e,_)
  1226. | TUnop (_,_,e)
  1227. | TMeta(_,e) ->
  1228. f e
  1229. | TArrayDecl el
  1230. | TNew (_,_,el)
  1231. | TBlock el ->
  1232. List.iter f el
  1233. | TObjectDecl fl ->
  1234. List.iter (fun (_,e) -> f e) fl
  1235. | TCall (e,el) ->
  1236. f e;
  1237. List.iter f el
  1238. | TVar (v,eo) ->
  1239. (match eo with None -> () | Some e -> f e)
  1240. | TFunction fu ->
  1241. f fu.tf_expr
  1242. | TIf (e,e1,e2) ->
  1243. f e;
  1244. f e1;
  1245. (match e2 with None -> () | Some e -> f e)
  1246. | TSwitch (e,cases,def) ->
  1247. f e;
  1248. List.iter (fun (el,e2) -> List.iter f el; f e2) cases;
  1249. (match def with None -> () | Some e -> f e)
  1250. | TPatMatch dt ->
  1251. let rec loop dt = match dt with
  1252. | DTBind(_,dt) -> loop dt
  1253. | DTGoto _ -> ()
  1254. | DTSwitch(e,cl,dto) ->
  1255. f e;
  1256. List.iter (fun (e,dt) ->
  1257. f e;
  1258. loop dt
  1259. ) cl;
  1260. (match dto with None -> () | Some dt -> loop dt)
  1261. | DTExpr e -> f e
  1262. | DTGuard(eg,dt1,dt2) ->
  1263. f eg;
  1264. loop dt1;
  1265. (match dt2 with None -> () | Some dt -> loop dt)
  1266. in
  1267. List.iter (fun (_,eo) -> match eo with None -> () | Some e -> f e) dt.dt_var_init;
  1268. Array.iter loop dt.dt_dt_lookup
  1269. | TTry (e,catches) ->
  1270. f e;
  1271. List.iter (fun (_,e) -> f e) catches
  1272. | TReturn eo ->
  1273. (match eo with None -> () | Some e -> f e)
  1274. let map_expr f e =
  1275. match e.eexpr with
  1276. | TConst _
  1277. | TLocal _
  1278. | TBreak
  1279. | TContinue
  1280. | TTypeExpr _ ->
  1281. e
  1282. | TArray (e1,e2) ->
  1283. let e1 = f e1 in
  1284. { e with eexpr = TArray (e1,f e2) }
  1285. | TBinop (op,e1,e2) ->
  1286. let e1 = f e1 in
  1287. { e with eexpr = TBinop (op,e1,f e2) }
  1288. | TFor (v,e1,e2) ->
  1289. let e1 = f e1 in
  1290. { e with eexpr = TFor (v,e1,f e2) }
  1291. | TWhile (e1,e2,flag) ->
  1292. let e1 = f e1 in
  1293. { e with eexpr = TWhile (e1,f e2,flag) }
  1294. | TThrow e1 ->
  1295. { e with eexpr = TThrow (f e1) }
  1296. | TEnumParameter (e1,ef,i) ->
  1297. { e with eexpr = TEnumParameter(f e1,ef,i) }
  1298. | TField (e1,v) ->
  1299. { e with eexpr = TField (f e1,v) }
  1300. | TParenthesis e1 ->
  1301. { e with eexpr = TParenthesis (f e1) }
  1302. | TUnop (op,pre,e1) ->
  1303. { e with eexpr = TUnop (op,pre,f e1) }
  1304. | TArrayDecl el ->
  1305. { e with eexpr = TArrayDecl (List.map f el) }
  1306. | TNew (t,pl,el) ->
  1307. { e with eexpr = TNew (t,pl,List.map f el) }
  1308. | TBlock el ->
  1309. { e with eexpr = TBlock (List.map f el) }
  1310. | TObjectDecl el ->
  1311. { e with eexpr = TObjectDecl (List.map (fun (v,e) -> v, f e) el) }
  1312. | TCall (e1,el) ->
  1313. { e with eexpr = TCall (f e1, List.map f el) }
  1314. | TVar (v,eo) ->
  1315. { e with eexpr = TVar (v, match eo with None -> None | Some e -> Some (f e)) }
  1316. | TFunction fu ->
  1317. { e with eexpr = TFunction { fu with tf_expr = f fu.tf_expr } }
  1318. | TIf (ec,e1,e2) ->
  1319. let ec = f ec in
  1320. let e1 = f e1 in
  1321. { e with eexpr = TIf (ec,e1,match e2 with None -> None | Some e -> Some (f e)) }
  1322. | TSwitch (e1,cases,def) ->
  1323. let e1 = f e1 in
  1324. let cases = List.map (fun (el,e2) -> List.map f el, f e2) cases in
  1325. { e with eexpr = TSwitch (e1, cases, match def with None -> None | Some e -> Some (f e)) }
  1326. | TPatMatch dt ->
  1327. let rec loop dt = match dt with
  1328. | DTBind(vl,dt) -> DTBind(vl, loop dt)
  1329. | DTGoto _ -> dt
  1330. | DTSwitch(e,cl,dto) -> DTSwitch(f e, List.map (fun (e,dt) -> f e,loop dt) cl,match dto with None -> None | Some dt -> Some (loop dt))
  1331. | DTExpr e -> DTExpr(f e)
  1332. | DTGuard(e,dt1,dt2) -> DTGuard(f e,loop dt1,match dt2 with None -> None | Some dt -> Some (loop dt))
  1333. in
  1334. let vi = List.map (fun (v,eo) -> v, match eo with None -> None | Some e -> Some(f e)) dt.dt_var_init in
  1335. { e with eexpr = TPatMatch({dt with dt_dt_lookup = Array.map loop dt.dt_dt_lookup; dt_var_init = vi})}
  1336. | TTry (e1,catches) ->
  1337. let e1 = f e1 in
  1338. { e with eexpr = TTry (e1, List.map (fun (v,e) -> v, f e) catches) }
  1339. | TReturn eo ->
  1340. { e with eexpr = TReturn (match eo with None -> None | Some e -> Some (f e)) }
  1341. | TCast (e1,t) ->
  1342. { e with eexpr = TCast (f e1,t) }
  1343. | TMeta (m,e1) ->
  1344. {e with eexpr = TMeta(m,f e1)}
  1345. let map_expr_type f ft fv e =
  1346. match e.eexpr with
  1347. | TConst _
  1348. | TBreak
  1349. | TContinue
  1350. | TTypeExpr _ ->
  1351. { e with etype = ft e.etype }
  1352. | TLocal v ->
  1353. { e with eexpr = TLocal (fv v); etype = ft e.etype }
  1354. | TArray (e1,e2) ->
  1355. let e1 = f e1 in
  1356. { e with eexpr = TArray (e1,f e2); etype = ft e.etype }
  1357. | TBinop (op,e1,e2) ->
  1358. let e1 = f e1 in
  1359. { e with eexpr = TBinop (op,e1,f e2); etype = ft e.etype }
  1360. | TFor (v,e1,e2) ->
  1361. let v = fv v in
  1362. let e1 = f e1 in
  1363. { e with eexpr = TFor (v,e1,f e2); etype = ft e.etype }
  1364. | TWhile (e1,e2,flag) ->
  1365. let e1 = f e1 in
  1366. { e with eexpr = TWhile (e1,f e2,flag); etype = ft e.etype }
  1367. | TThrow e1 ->
  1368. { e with eexpr = TThrow (f e1); etype = ft e.etype }
  1369. | TEnumParameter (e1,ef,i) ->
  1370. { e with eexpr = TEnumParameter(f e1,ef,i); etype = ft e.etype }
  1371. | TField (e1,v) ->
  1372. let e1 = f e1 in
  1373. let v = try
  1374. let n = match v with
  1375. | FClosure _ -> raise Not_found
  1376. | FAnon f | FInstance (_,f) | FStatic (_,f) -> f.cf_name
  1377. | FEnum (_,f) -> f.ef_name
  1378. | FDynamic n -> n
  1379. in
  1380. quick_field e1.etype n
  1381. with Not_found ->
  1382. v
  1383. in
  1384. { e with eexpr = TField (e1,v); etype = ft e.etype }
  1385. | TParenthesis e1 ->
  1386. { e with eexpr = TParenthesis (f e1); etype = ft e.etype }
  1387. | TUnop (op,pre,e1) ->
  1388. { e with eexpr = TUnop (op,pre,f e1); etype = ft e.etype }
  1389. | TArrayDecl el ->
  1390. { e with eexpr = TArrayDecl (List.map f el); etype = ft e.etype }
  1391. | TNew (_,_,el) ->
  1392. let et = ft e.etype in
  1393. (* make sure that we use the class corresponding to the replaced type *)
  1394. let c, pl = (match follow et with TInst (c,pl) -> (c,pl) | TAbstract({a_impl = Some c},pl) -> c,pl | t -> error [has_no_field t "new"]) in
  1395. { e with eexpr = TNew (c,pl,List.map f el); etype = et }
  1396. | TBlock el ->
  1397. { e with eexpr = TBlock (List.map f el); etype = ft e.etype }
  1398. | TObjectDecl el ->
  1399. { e with eexpr = TObjectDecl (List.map (fun (v,e) -> v, f e) el); etype = ft e.etype }
  1400. | TCall (e1,el) ->
  1401. let e1 = f e1 in
  1402. { e with eexpr = TCall (e1, List.map f el); etype = ft e.etype }
  1403. | TVar (v,eo) ->
  1404. { e with eexpr = TVar (fv v, match eo with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1405. | TFunction fu ->
  1406. let fu = {
  1407. tf_expr = f fu.tf_expr;
  1408. tf_args = List.map (fun (v,o) -> fv v, o) fu.tf_args;
  1409. tf_type = ft fu.tf_type;
  1410. } in
  1411. { e with eexpr = TFunction fu; etype = ft e.etype }
  1412. | TIf (ec,e1,e2) ->
  1413. let ec = f ec in
  1414. let e1 = f e1 in
  1415. { e with eexpr = TIf (ec,e1,match e2 with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1416. | TSwitch (e1,cases,def) ->
  1417. let e1 = f e1 in
  1418. let cases = List.map (fun (el,e2) -> List.map f el, f e2) cases in
  1419. { e with eexpr = TSwitch (e1, cases, match def with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1420. | TPatMatch dt ->
  1421. let rec loop dt = match dt with
  1422. | DTBind(vl,dt) -> DTBind(vl, loop dt)
  1423. | DTGoto _ -> dt
  1424. | DTSwitch(e,cl,dto) -> DTSwitch(f e, List.map (fun (e,dt) -> f e,loop dt) cl,match dto with None -> None | Some dt -> Some (loop dt))
  1425. | DTExpr e -> DTExpr(f e)
  1426. | DTGuard (e,dt1,dt2) -> DTGuard(f e, loop dt, match dt2 with None -> None | Some dt -> Some (loop dt))
  1427. in
  1428. let vi = List.map (fun (v,eo) -> v, match eo with None -> None | Some e -> Some(f e)) dt.dt_var_init in
  1429. { e with eexpr = TPatMatch({dt with dt_dt_lookup = Array.map loop dt.dt_dt_lookup; dt_var_init = vi}); etype = ft e.etype}
  1430. | TTry (e1,catches) ->
  1431. let e1 = f e1 in
  1432. { e with eexpr = TTry (e1, List.map (fun (v,e) -> fv v, f e) catches); etype = ft e.etype }
  1433. | TReturn eo ->
  1434. { e with eexpr = TReturn (match eo with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1435. | TCast (e1,t) ->
  1436. { e with eexpr = TCast (f e1,t); etype = ft e.etype }
  1437. | TMeta (m,e1) ->
  1438. {e with eexpr = TMeta(m, f e1); etype = ft e.etype }
  1439. (* ======= Miscellaneous ======= *)
  1440. let find_array_access a pl t1 t2 is_set =
  1441. let ta = apply_params a.a_types pl a.a_this in
  1442. let rec loop cfl = match cfl with
  1443. | [] -> raise Not_found
  1444. | cf :: cfl when not (Meta.has Meta.ArrayAccess cf.cf_meta) ->
  1445. loop cfl
  1446. | cf :: cfl ->
  1447. match follow (apply_params a.a_types pl (monomorphs cf.cf_params cf.cf_type)) with
  1448. | TFun([(_,_,tab);(_,_,ta1);(_,_,ta2)],r) as tf when is_set ->
  1449. begin try
  1450. unify tab ta;
  1451. unify t1 ta1;
  1452. unify t2 ta2;
  1453. cf,tf,r
  1454. with Unify_error _ ->
  1455. loop cfl
  1456. end
  1457. | TFun([(_,_,tab);(_,_,ta1)],r) as tf when not is_set ->
  1458. begin try
  1459. unify tab ta;
  1460. unify t1 ta1;
  1461. cf,tf,r
  1462. with Unify_error _ ->
  1463. loop cfl
  1464. end
  1465. | _ -> loop cfl
  1466. in
  1467. loop a.a_array
  1468. (* ======= Printing ======= *)
  1469. let print_context() = ref []
  1470. let rec s_type ctx t =
  1471. match t with
  1472. | TMono r ->
  1473. (match !r with
  1474. | None -> Printf.sprintf "Unknown<%d>" (try List.assq t (!ctx) with Not_found -> let n = List.length !ctx in ctx := (t,n) :: !ctx; n)
  1475. | Some t -> s_type ctx t)
  1476. | TEnum (e,tl) ->
  1477. Ast.s_type_path e.e_path ^ s_type_params ctx tl
  1478. | TInst (c,tl) ->
  1479. Ast.s_type_path c.cl_path ^ s_type_params ctx tl
  1480. | TType (t,tl) ->
  1481. Ast.s_type_path t.t_path ^ s_type_params ctx tl
  1482. | TAbstract (a,tl) ->
  1483. Ast.s_type_path a.a_path ^ s_type_params ctx tl
  1484. | TFun ([],t) ->
  1485. "Void -> " ^ s_fun ctx t false
  1486. | TFun (l,t) ->
  1487. String.concat " -> " (List.map (fun (s,b,t) ->
  1488. (if b then "?" else "") ^ (if s = "" then "" else s ^ " : ") ^ s_fun ctx t true
  1489. ) l) ^ " -> " ^ s_fun ctx t false
  1490. | TAnon a ->
  1491. let fl = PMap.fold (fun f acc -> ((if Meta.has Meta.Optional f.cf_meta then " ?" else " ") ^ f.cf_name ^ " : " ^ s_type ctx f.cf_type) :: acc) a.a_fields [] in
  1492. "{" ^ (if not (is_closed a) then "+" else "") ^ String.concat "," fl ^ " }"
  1493. | TDynamic t2 ->
  1494. "Dynamic" ^ s_type_params ctx (if t == t2 then [] else [t2])
  1495. | TLazy f ->
  1496. s_type ctx (!f())
  1497. and s_fun ctx t void =
  1498. match t with
  1499. | TFun _ ->
  1500. "(" ^ s_type ctx t ^ ")"
  1501. | TAbstract ({ a_path = ([],"Void") },[]) when void ->
  1502. "(" ^ s_type ctx t ^ ")"
  1503. | TMono r ->
  1504. (match !r with
  1505. | None -> s_type ctx t
  1506. | Some t -> s_fun ctx t void)
  1507. | TLazy f ->
  1508. s_fun ctx (!f()) void
  1509. | _ ->
  1510. s_type ctx t
  1511. and s_type_params ctx = function
  1512. | [] -> ""
  1513. | l -> "<" ^ String.concat ", " (List.map (s_type ctx) l) ^ ">"
  1514. let s_access is_read = function
  1515. | AccNormal -> "default"
  1516. | AccNo -> "null"
  1517. | AccNever -> "never"
  1518. | AccResolve -> "resolve"
  1519. | AccCall -> if is_read then "get" else "set"
  1520. | AccInline -> "inline"
  1521. | AccRequire (n,_) -> "require " ^ n
  1522. let s_kind = function
  1523. | Var { v_read = AccNormal; v_write = AccNormal } -> "var"
  1524. | Var v -> "(" ^ s_access true v.v_read ^ "," ^ s_access false v.v_write ^ ")"
  1525. | Method m ->
  1526. match m with
  1527. | MethNormal -> "method"
  1528. | MethDynamic -> "dynamic method"
  1529. | MethInline -> "inline method"
  1530. | MethMacro -> "macro method"
  1531. let s_expr_kind e =
  1532. match e.eexpr with
  1533. | TConst _ -> "Const"
  1534. | TLocal _ -> "Local"
  1535. | TArray (_,_) -> "Array"
  1536. | TBinop (_,_,_) -> "Binop"
  1537. | TEnumParameter (_,_,_) -> "EnumParameter"
  1538. | TField (_,_) -> "Field"
  1539. | TTypeExpr _ -> "TypeExpr"
  1540. | TParenthesis _ -> "Parenthesis"
  1541. | TObjectDecl _ -> "ObjectDecl"
  1542. | TArrayDecl _ -> "ArrayDecl"
  1543. | TCall (_,_) -> "Call"
  1544. | TNew (_,_,_) -> "New"
  1545. | TUnop (_,_,_) -> "Unop"
  1546. | TFunction _ -> "Function"
  1547. | TVar _ -> "Vars"
  1548. | TBlock _ -> "Block"
  1549. | TFor (_,_,_) -> "For"
  1550. | TIf (_,_,_) -> "If"
  1551. | TWhile (_,_,_) -> "While"
  1552. | TSwitch (_,_,_) -> "Switch"
  1553. | TPatMatch _ -> "PatMatch"
  1554. | TTry (_,_) -> "Try"
  1555. | TReturn _ -> "Return"
  1556. | TBreak -> "Break"
  1557. | TContinue -> "Continue"
  1558. | TThrow _ -> "Throw"
  1559. | TCast _ -> "Cast"
  1560. | TMeta _ -> "Meta"
  1561. let s_const = function
  1562. | TInt i -> Int32.to_string i
  1563. | TFloat s -> s ^ "f"
  1564. | TString s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)
  1565. | TBool b -> if b then "true" else "false"
  1566. | TNull -> "null"
  1567. | TThis -> "this"
  1568. | TSuper -> "super"
  1569. let rec s_expr s_type e =
  1570. let sprintf = Printf.sprintf in
  1571. let slist f l = String.concat "," (List.map f l) in
  1572. let loop = s_expr s_type in
  1573. let s_var v = v.v_name ^ ":" ^ string_of_int v.v_id ^ if v.v_capture then "[c]" else "" in
  1574. let str = (match e.eexpr with
  1575. | TConst c ->
  1576. "Const " ^ s_const c
  1577. | TLocal v ->
  1578. "Local " ^ s_var v
  1579. | TArray (e1,e2) ->
  1580. sprintf "%s[%s]" (loop e1) (loop e2)
  1581. | TBinop (op,e1,e2) ->
  1582. sprintf "(%s %s %s)" (loop e1) (s_binop op) (loop e2)
  1583. | TEnumParameter (e1,_,i) ->
  1584. sprintf "%s[%i]" (loop e1) i
  1585. | TField (e,f) ->
  1586. let fstr = (match f with
  1587. | FStatic (c,f) -> "static(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ ")"
  1588. | FInstance (c,f) -> "inst(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ " : " ^ s_type f.cf_type ^ ")"
  1589. | FClosure (c,f) -> "closure(" ^ (match c with None -> f.cf_name | Some c -> s_type_path c.cl_path ^ "." ^ f.cf_name) ^ ")"
  1590. | FAnon f -> "anon(" ^ f.cf_name ^ ")"
  1591. | FEnum (en,f) -> "enum(" ^ s_type_path en.e_path ^ "." ^ f.ef_name ^ ")"
  1592. | FDynamic f -> "dynamic(" ^ f ^ ")"
  1593. ) in
  1594. sprintf "%s.%s" (loop e) fstr
  1595. | TTypeExpr m ->
  1596. sprintf "TypeExpr %s" (s_type_path (t_path m))
  1597. | TParenthesis e ->
  1598. sprintf "Parenthesis %s" (loop e)
  1599. | TObjectDecl fl ->
  1600. sprintf "ObjectDecl {%s)" (slist (fun (f,e) -> sprintf "%s : %s" f (loop e)) fl)
  1601. | TArrayDecl el ->
  1602. sprintf "ArrayDecl [%s]" (slist loop el)
  1603. | TCall (e,el) ->
  1604. sprintf "Call %s(%s)" (loop e) (slist loop el)
  1605. | TNew (c,pl,el) ->
  1606. sprintf "New %s%s(%s)" (s_type_path c.cl_path) (match pl with [] -> "" | l -> sprintf "<%s>" (slist s_type l)) (slist loop el)
  1607. | TUnop (op,f,e) ->
  1608. (match f with
  1609. | Prefix -> sprintf "(%s %s)" (s_unop op) (loop e)
  1610. | Postfix -> sprintf "(%s %s)" (loop e) (s_unop op))
  1611. | TFunction f ->
  1612. let args = slist (fun (v,o) -> sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ s_const c)) f.tf_args in
  1613. sprintf "Function(%s) : %s = %s" args (s_type f.tf_type) (loop f.tf_expr)
  1614. | TVar (v,eo) ->
  1615. sprintf "Vars %s" (sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match eo with None -> "" | Some e -> " = " ^ loop e))
  1616. | TBlock el ->
  1617. sprintf "Block {\n%s}" (String.concat "" (List.map (fun e -> sprintf "%s;\n" (loop e)) el))
  1618. | TFor (v,econd,e) ->
  1619. sprintf "For (%s : %s in %s,%s)" (s_var v) (s_type v.v_type) (loop econd) (loop e)
  1620. | TIf (e,e1,e2) ->
  1621. sprintf "If (%s,%s%s)" (loop e) (loop e1) (match e2 with None -> "" | Some e -> "," ^ loop e)
  1622. | TWhile (econd,e,flag) ->
  1623. (match flag with
  1624. | NormalWhile -> sprintf "While (%s,%s)" (loop econd) (loop e)
  1625. | DoWhile -> sprintf "DoWhile (%s,%s)" (loop e) (loop econd))
  1626. | TSwitch (e,cases,def) ->
  1627. sprintf "Switch (%s,(%s)%s)" (loop e) (slist (fun (cl,e) -> sprintf "case %s: %s" (slist loop cl) (loop e)) cases) (match def with None -> "" | Some e -> "," ^ loop e)
  1628. | TPatMatch dt -> s_dt "" (dt.dt_dt_lookup.(dt.dt_first))
  1629. | TTry (e,cl) ->
  1630. sprintf "Try %s(%s) " (loop e) (slist (fun (v,e) -> sprintf "catch( %s : %s ) %s" (s_var v) (s_type v.v_type) (loop e)) cl)
  1631. | TReturn None ->
  1632. "Return"
  1633. | TReturn (Some e) ->
  1634. sprintf "Return %s" (loop e)
  1635. | TBreak ->
  1636. "Break"
  1637. | TContinue ->
  1638. "Continue"
  1639. | TThrow e ->
  1640. "Throw " ^ (loop e)
  1641. | TCast (e,t) ->
  1642. sprintf "Cast %s%s" (match t with None -> "" | Some t -> s_type_path (t_path t) ^ ": ") (loop e)
  1643. | TMeta ((n,el,_),e) ->
  1644. sprintf "@%s%s %s" (Meta.to_string n) (match el with [] -> "" | _ -> "(" ^ (String.concat ", " (List.map Ast.s_expr el)) ^ ")") (loop e)
  1645. ) in
  1646. sprintf "(%s : %s)" str (s_type e.etype)
  1647. and s_dt tabs tree =
  1648. let s_type = s_type (print_context()) in
  1649. tabs ^ match tree with
  1650. | DTSwitch (st,cl,dto) ->
  1651. "switch(" ^ (s_expr s_type st) ^ ") { \n" ^ tabs
  1652. ^ (String.concat ("\n" ^ tabs) (List.map (fun (c,dt) ->
  1653. "case " ^ (s_expr s_type c) ^ ":\n" ^ (s_dt (tabs ^ "\t") dt)
  1654. ) cl))
  1655. ^ (match dto with None -> "" | Some dt -> tabs ^ "default: " ^ (s_dt (tabs ^ "\t") dt))
  1656. ^ "\n" ^ (if String.length tabs = 0 then "" else (String.sub tabs 0 (String.length tabs - 1))) ^ "}"
  1657. | DTBind (bl, dt) -> "bind " ^ (String.concat "," (List.map (fun ((v,_),st) -> v.v_name ^ "(" ^ (string_of_int v.v_id) ^ ") =" ^ (s_expr s_type st)) bl)) ^ "\n" ^ (s_dt tabs dt)
  1658. | DTGoto i ->
  1659. "goto " ^ (string_of_int i)
  1660. | DTExpr e -> s_expr s_type e
  1661. | DTGuard (e,dt1,dt2) -> "if(" ^ (s_expr s_type e) ^ ") " ^ (s_dt tabs dt1) ^ (match dt2 with None -> "" | Some dt -> " else " ^ (s_dt tabs dt))
  1662. let rec s_expr_pretty tabs s_type e =
  1663. let sprintf = Printf.sprintf in
  1664. let loop = s_expr_pretty tabs s_type in
  1665. let slist f l = String.concat "," (List.map f l) in
  1666. match e.eexpr with
  1667. | TConst c -> s_const c
  1668. | TLocal v -> v.v_name
  1669. | TArray (e1,e2) -> sprintf "%s[%s]" (loop e1) (loop e2)
  1670. | TBinop (op,e1,e2) -> sprintf "%s %s %s" (loop e1) (s_binop op) (loop e2)
  1671. | TEnumParameter (e1,_,i) -> sprintf "%s[%i]" (loop e1) i
  1672. | TField (e1,s) -> sprintf "%s.%s" (loop e1) (field_name s)
  1673. | TTypeExpr mt -> (s_type_path (t_path mt))
  1674. | TParenthesis e1 -> sprintf "(%s)" (loop e1)
  1675. | TObjectDecl fl -> sprintf "{%s}" (slist (fun (f,e) -> sprintf "%s : %s" f (loop e)) fl)
  1676. | TArrayDecl el -> sprintf "[%s]" (slist loop el)
  1677. | TCall (e1,el) -> sprintf "%s(%s)" (loop e1) (slist loop el)
  1678. | TNew (c,pl,el) ->
  1679. sprintf "new %s(%s)" (s_type_path c.cl_path) (slist loop el)
  1680. | TUnop (op,f,e) ->
  1681. (match f with
  1682. | Prefix -> sprintf "%s %s" (s_unop op) (loop e)
  1683. | Postfix -> sprintf "%s %s" (loop e) (s_unop op))
  1684. | TFunction f ->
  1685. let args = slist (fun (v,o) -> sprintf "%s:%s%s" v.v_name (s_type v.v_type) (match o with None -> "" | Some c -> " = " ^ s_const c)) f.tf_args in
  1686. sprintf "function(%s) = %s" args (loop f.tf_expr)
  1687. | TVar (v,eo) ->
  1688. sprintf "var %s" (sprintf "%s%s" v.v_name (match eo with None -> "" | Some e -> " = " ^ loop e))
  1689. | TBlock el ->
  1690. let ntabs = tabs ^ "\t" in
  1691. let s = sprintf "{\n%s" (String.concat "" (List.map (fun e -> sprintf "%s%s;\n" ntabs (s_expr_pretty ntabs s_type e)) el)) in
  1692. s ^ tabs ^ "}"
  1693. | TFor (v,econd,e) ->
  1694. sprintf "for (%s in %s) %s" v.v_name (loop econd) (loop e)
  1695. | TIf (e,e1,e2) ->
  1696. sprintf "if (%s)%s%s)" (loop e) (loop e1) (match e2 with None -> "" | Some e -> " else " ^ loop e)
  1697. | TWhile (econd,e,flag) ->
  1698. (match flag with
  1699. | NormalWhile -> sprintf "while (%s) %s" (loop econd) (loop e)
  1700. | DoWhile -> sprintf "do (%s) while(%s)" (loop e) (loop econd))
  1701. | TSwitch (e,cases,def) ->
  1702. let ntabs = tabs ^ "\t" in
  1703. let s = sprintf "switch (%s) {\n%s%s" (loop e) (slist (fun (cl,e) -> sprintf "%scase %s: %s\n" ntabs (slist loop cl) (s_expr_pretty ntabs s_type e)) cases) (match def with None -> "" | Some e -> ntabs ^ "default: " ^ (s_expr_pretty ntabs s_type e) ^ "\n") in
  1704. s ^ tabs ^ "}"
  1705. | TPatMatch dt -> s_dt tabs (dt.dt_dt_lookup.(dt.dt_first))
  1706. | TTry (e,cl) ->
  1707. sprintf "try %s%s" (loop e) (slist (fun (v,e) -> sprintf "catch( %s : %s ) %s" v.v_name (s_type v.v_type) (loop e)) cl)
  1708. | TReturn None ->
  1709. "return"
  1710. | TReturn (Some e) ->
  1711. sprintf "return %s" (loop e)
  1712. | TBreak ->
  1713. "break"
  1714. | TContinue ->
  1715. "continue"
  1716. | TThrow e ->
  1717. "throw " ^ (loop e)
  1718. | TCast (e,None) ->
  1719. sprintf "cast %s" (loop e)
  1720. | TCast (e,Some mt) ->
  1721. sprintf "cast (%s,%s)" (loop e) (s_type_path (t_path mt))
  1722. | TMeta ((n,el,_),e) ->
  1723. sprintf "@%s%s %s" (Meta.to_string n) (match el with [] -> "" | _ -> "(" ^ (String.concat ", " (List.map Ast.s_expr el)) ^ ")") (loop e)