type.ml 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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. | Extend of t list
  82. | Statics of tclass
  83. | EnumStatics of tenum
  84. | AbstractStatics of tabstract
  85. and tanon = {
  86. mutable a_fields : (string, tclass_field) PMap.t;
  87. a_status : anon_status ref;
  88. }
  89. and texpr_expr =
  90. | TConst of tconstant
  91. | TLocal of tvar
  92. | TArray of texpr * texpr
  93. | TBinop of Ast.binop * texpr * texpr
  94. | TField of texpr * tfield_access
  95. | TTypeExpr of module_type
  96. | TParenthesis of texpr
  97. | TObjectDecl of (string * texpr) list
  98. | TArrayDecl of texpr list
  99. | TCall of texpr * texpr list
  100. | TNew of tclass * tparams * texpr list
  101. | TUnop of Ast.unop * Ast.unop_flag * texpr
  102. | TFunction of tfunc
  103. | TVar of tvar * texpr option
  104. | TBlock of texpr list
  105. | TFor of tvar * texpr * texpr
  106. | TIf of texpr * texpr * texpr option
  107. | TWhile of texpr * texpr * Ast.while_flag
  108. | TSwitch of texpr * (texpr list * texpr) list * texpr option
  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 * tparams * tclass_field
  119. | FStatic of tclass * tclass_field
  120. | FAnon of tclass_field
  121. | FDynamic of string
  122. | FClosure of (tclass * tparams) 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. mutable 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_params : 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_params : 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 -> bool;
  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_params : 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_params : 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_params : 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 list;
  235. mutable a_from_field : (t * tclass_field) list;
  236. mutable a_to : t list;
  237. mutable a_to_field : (t * tclass_field) list;
  238. mutable a_array : tclass_field list;
  239. mutable a_resolve : tclass_field option;
  240. }
  241. and module_type =
  242. | TClassDecl of tclass
  243. | TEnumDecl of tenum
  244. | TTypeDecl of tdef
  245. | TAbstractDecl of tabstract
  246. and module_def = {
  247. m_id : int;
  248. m_path : path;
  249. mutable m_types : module_type list;
  250. m_extra : module_def_extra;
  251. }
  252. and module_def_extra = {
  253. m_file : string;
  254. m_sign : string;
  255. mutable m_time : float;
  256. mutable m_dirty : bool;
  257. mutable m_added : int;
  258. mutable m_mark : int;
  259. mutable m_deps : (int,module_def) PMap.t;
  260. mutable m_processed : int;
  261. mutable m_kind : module_kind;
  262. mutable m_binded_res : (string, string) PMap.t;
  263. mutable m_macro_calls : string list;
  264. mutable m_if_feature : (string *(tclass * tclass_field * bool)) list;
  265. mutable m_features : (string,bool) Hashtbl.t;
  266. }
  267. and module_kind =
  268. | MCode
  269. | MMacro
  270. | MFake
  271. | MSub
  272. | MExtern
  273. and dt =
  274. | DTSwitch of texpr * (texpr * dt) list * dt option
  275. | DTBind of ((tvar * pos) * texpr) list * dt
  276. | DTGoto of int
  277. | DTExpr of texpr
  278. | DTGuard of texpr * dt * dt option
  279. and decision_tree = {
  280. dt_dt_lookup : dt array;
  281. dt_first : int;
  282. dt_type : t;
  283. dt_var_init : (tvar * texpr option) list;
  284. dt_is_complex : bool;
  285. }
  286. (* ======= General utility ======= *)
  287. let alloc_var =
  288. let uid = ref 0 in
  289. (fun n t -> incr uid; { v_name = n; v_type = t; v_id = !uid; v_capture = false; v_extra = None; v_meta = [] })
  290. let alloc_unbound_var n t =
  291. let v = alloc_var n t in
  292. v.v_meta <- [Meta.Unbound,[],null_pos];
  293. v
  294. let alloc_mid =
  295. let mid = ref 0 in
  296. (fun() -> incr mid; !mid)
  297. let mk e t p = { eexpr = e; etype = t; epos = p }
  298. let mk_block e =
  299. match e.eexpr with
  300. | TBlock _ -> e
  301. | _ -> mk (TBlock [e]) e.etype e.epos
  302. let mk_cast e t p = mk (TCast(e,None)) t p
  303. let null t p = mk (TConst TNull) t p
  304. let mk_mono() = TMono (ref None)
  305. let rec t_dynamic = TDynamic t_dynamic
  306. let tfun pl r = TFun (List.map (fun t -> "",false,t) pl,r)
  307. let fun_args l = List.map (fun (a,c,t) -> a, c <> None, t) l
  308. let mk_class m path pos =
  309. {
  310. cl_path = path;
  311. cl_module = m;
  312. cl_pos = pos;
  313. cl_doc = None;
  314. cl_meta = [];
  315. cl_private = false;
  316. cl_kind = KNormal;
  317. cl_extern = false;
  318. cl_interface = false;
  319. cl_params = [];
  320. cl_super = None;
  321. cl_implements = [];
  322. cl_fields = PMap.empty;
  323. cl_ordered_statics = [];
  324. cl_ordered_fields = [];
  325. cl_statics = PMap.empty;
  326. cl_dynamic = None;
  327. cl_array_access = None;
  328. cl_constructor = None;
  329. cl_init = None;
  330. cl_overrides = [];
  331. cl_build = (fun() -> true);
  332. cl_restore = (fun() -> ());
  333. }
  334. let module_extra file sign time kind =
  335. {
  336. m_file = file;
  337. m_sign = sign;
  338. m_dirty = false;
  339. m_added = 0;
  340. m_mark = 0;
  341. m_time = time;
  342. m_processed = 0;
  343. m_deps = PMap.empty;
  344. m_kind = kind;
  345. m_binded_res = PMap.empty;
  346. m_macro_calls = [];
  347. m_if_feature = [];
  348. m_features = Hashtbl.create 0;
  349. }
  350. let mk_field name t p = {
  351. cf_name = name;
  352. cf_type = t;
  353. cf_pos = p;
  354. cf_doc = None;
  355. cf_meta = [];
  356. cf_public = true;
  357. cf_kind = Var { v_read = AccNormal; v_write = AccNormal };
  358. cf_expr = None;
  359. cf_params = [];
  360. cf_overloads = [];
  361. }
  362. let null_module = {
  363. m_id = alloc_mid();
  364. m_path = [] , "";
  365. m_types = [];
  366. m_extra = module_extra "" "" 0. MFake;
  367. }
  368. let null_class =
  369. let c = mk_class null_module ([],"") Ast.null_pos in
  370. c.cl_private <- true;
  371. c
  372. let null_field = mk_field "" t_dynamic Ast.null_pos
  373. let null_abstract = {
  374. a_path = ([],"");
  375. a_module = null_module;
  376. a_pos = null_pos;
  377. a_private = true;
  378. a_doc = None;
  379. a_meta = [];
  380. a_params = [];
  381. a_ops = [];
  382. a_unops = [];
  383. a_impl = None;
  384. a_this = t_dynamic;
  385. a_from = [];
  386. a_from_field = [];
  387. a_to = [];
  388. a_to_field = [];
  389. a_array = [];
  390. a_resolve = None;
  391. }
  392. let add_dependency m mdep =
  393. if m != null_module && m != mdep then m.m_extra.m_deps <- PMap.add mdep.m_id mdep m.m_extra.m_deps
  394. let arg_name (a,_) = a.v_name
  395. let t_infos t : tinfos =
  396. match t with
  397. | TClassDecl c -> Obj.magic c
  398. | TEnumDecl e -> Obj.magic e
  399. | TTypeDecl t -> Obj.magic t
  400. | TAbstractDecl a -> Obj.magic a
  401. let t_path t = (t_infos t).mt_path
  402. let rec is_parent csup c =
  403. if c == csup || List.exists (fun (i,_) -> is_parent csup i) c.cl_implements then
  404. true
  405. else match c.cl_super with
  406. | None -> false
  407. | Some (c,_) -> is_parent csup c
  408. let map loop t =
  409. match t with
  410. | TMono r ->
  411. (match !r with
  412. | None -> t
  413. | Some t -> loop t) (* erase*)
  414. | TEnum (_,[]) | TInst (_,[]) | TType (_,[]) ->
  415. t
  416. | TEnum (e,tl) ->
  417. TEnum (e, List.map loop tl)
  418. | TInst (c,tl) ->
  419. TInst (c, List.map loop tl)
  420. | TType (t2,tl) ->
  421. TType (t2,List.map loop tl)
  422. | TAbstract (a,tl) ->
  423. TAbstract (a,List.map loop tl)
  424. | TFun (tl,r) ->
  425. TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
  426. | TAnon a ->
  427. let fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields in
  428. begin match !(a.a_status) with
  429. | Opened ->
  430. a.a_fields <- fields;
  431. t
  432. | _ ->
  433. TAnon {
  434. a_fields = fields;
  435. a_status = a.a_status;
  436. }
  437. end
  438. | TLazy f ->
  439. let ft = !f() in
  440. let ft2 = loop ft in
  441. if ft == ft2 then t else ft2
  442. | TDynamic t2 ->
  443. if t == t2 then t else TDynamic (loop t2)
  444. (* substitute parameters with other types *)
  445. let apply_params cparams params t =
  446. match cparams with
  447. | [] -> t
  448. | _ ->
  449. let rec loop l1 l2 =
  450. match l1, l2 with
  451. | [] , [] -> []
  452. | (x,TLazy f) :: l1, _ -> loop ((x,(!f)()) :: l1) l2
  453. | (_,t1) :: l1 , t2 :: l2 -> (t1,t2) :: loop l1 l2
  454. | _ -> assert false
  455. in
  456. let subst = loop cparams params in
  457. let rec loop t =
  458. try
  459. List.assq t subst
  460. with Not_found ->
  461. match t with
  462. | TMono r ->
  463. (match !r with
  464. | None -> t
  465. | Some t -> loop t)
  466. | TEnum (e,tl) ->
  467. (match tl with
  468. | [] -> t
  469. | _ -> TEnum (e,List.map loop tl))
  470. | TType (t2,tl) ->
  471. (match tl with
  472. | [] -> t
  473. | _ -> TType (t2,List.map loop tl))
  474. | TAbstract (a,tl) ->
  475. (match tl with
  476. | [] -> t
  477. | _ -> TAbstract (a,List.map loop tl))
  478. | TInst (c,tl) ->
  479. (match tl with
  480. | [] ->
  481. t
  482. | [TMono r] ->
  483. (match !r with
  484. | Some tt when t == tt ->
  485. (* for dynamic *)
  486. let pt = mk_mono() in
  487. let t = TInst (c,[pt]) in
  488. (match pt with TMono r -> r := Some t | _ -> assert false);
  489. t
  490. | _ -> TInst (c,List.map loop tl))
  491. | _ ->
  492. TInst (c,List.map loop tl))
  493. | TFun (tl,r) ->
  494. TFun (List.map (fun (s,o,t) -> s, o, loop t) tl,loop r)
  495. | TAnon a ->
  496. let fields = PMap.map (fun f -> { f with cf_type = loop f.cf_type }) a.a_fields in
  497. begin match !(a.a_status) with
  498. | Opened ->
  499. a.a_fields <- fields;
  500. t
  501. | _ ->
  502. TAnon {
  503. a_fields = fields;
  504. a_status = a.a_status;
  505. }
  506. end
  507. | TLazy f ->
  508. let ft = !f() in
  509. let ft2 = loop ft in
  510. if ft == ft2 then
  511. t
  512. else
  513. ft2
  514. | TDynamic t2 ->
  515. if t == t2 then
  516. t
  517. else
  518. TDynamic (loop t2)
  519. in
  520. loop t
  521. let monomorphs eparams t =
  522. apply_params eparams (List.map (fun _ -> mk_mono()) eparams) t
  523. let rec follow t =
  524. match t with
  525. | TMono r ->
  526. (match !r with
  527. | Some t -> follow t
  528. | _ -> t)
  529. | TLazy f ->
  530. follow (!f())
  531. | TType (t,tl) ->
  532. follow (apply_params t.t_params tl t.t_type)
  533. | _ -> t
  534. let rec is_nullable = function
  535. | TMono r ->
  536. (match !r with None -> false | Some t -> is_nullable t)
  537. | TType ({ t_path = ([],"Null") },[_]) ->
  538. true
  539. | TLazy f ->
  540. is_nullable (!f())
  541. | TType (t,tl) ->
  542. is_nullable (apply_params t.t_params tl t.t_type)
  543. | TFun _ ->
  544. false
  545. (*
  546. Type parameters will most of the time be nullable objects, so we don't want to make it hard for users
  547. to have to specify Null<T> all over the place, so while they could be a basic type, let's assume they will not.
  548. This will still cause issues with inlining and haxe.rtti.Generic. In that case proper explicit Null<T> is required to
  549. work correctly with basic types. This could still be fixed by redoing a nullability inference on the typed AST.
  550. | TInst ({ cl_kind = KTypeParameter },_) -> false
  551. *)
  552. | TAbstract (a,_) when Meta.has Meta.CoreType a.a_meta ->
  553. not (Meta.has Meta.NotNull a.a_meta)
  554. | TAbstract (a,tl) ->
  555. not (Meta.has Meta.NotNull a.a_meta) && is_nullable (apply_params a.a_params tl a.a_this)
  556. | _ ->
  557. true
  558. let rec is_null ?(no_lazy=false) = function
  559. | TMono r ->
  560. (match !r with None -> false | Some t -> is_null t)
  561. | TType ({ t_path = ([],"Null") },[t]) ->
  562. not (is_nullable (follow t))
  563. | TLazy f ->
  564. if no_lazy then raise Exit else is_null (!f())
  565. | TType (t,tl) ->
  566. is_null (apply_params t.t_params tl t.t_type)
  567. | _ ->
  568. false
  569. (* Determines if we have a Null<T>. Unlike is_null, this returns true even if the wrapped type is nullable itself. *)
  570. let rec is_explicit_null = function
  571. | TMono r ->
  572. (match !r with None -> false | Some t -> is_null t)
  573. | TType ({ t_path = ([],"Null") },[t]) ->
  574. true
  575. | TLazy f ->
  576. is_null (!f())
  577. | TType (t,tl) ->
  578. is_null (apply_params t.t_params tl t.t_type)
  579. | _ ->
  580. false
  581. let rec has_mono t = match t with
  582. | TMono r ->
  583. (match !r with None -> true | Some t -> has_mono t)
  584. | TInst(_,pl) | TEnum(_,pl) | TAbstract(_,pl) | TType(_,pl) ->
  585. List.exists has_mono pl
  586. | TDynamic _ ->
  587. false
  588. | TFun(args,r) ->
  589. has_mono r || List.exists (fun (_,_,t) -> has_mono t) args
  590. | TAnon a ->
  591. PMap.fold (fun cf b -> has_mono cf.cf_type || b) a.a_fields false
  592. | TLazy r ->
  593. has_mono (!r())
  594. let concat e1 e2 =
  595. let e = (match e1.eexpr, e2.eexpr with
  596. | TBlock el1, TBlock el2 -> TBlock (el1@el2)
  597. | TBlock el, _ -> TBlock (el @ [e2])
  598. | _, TBlock el -> TBlock (e1 :: el)
  599. | _ , _ -> TBlock [e1;e2]
  600. ) in
  601. mk e e2.etype (punion e1.epos e2.epos)
  602. let is_closed a = !(a.a_status) <> Opened
  603. let type_of_module_type = function
  604. | TClassDecl c -> TInst (c,List.map snd c.cl_params)
  605. | TEnumDecl e -> TEnum (e,List.map snd e.e_params)
  606. | TTypeDecl t -> TType (t,List.map snd t.t_params)
  607. | TAbstractDecl a -> TAbstract (a,List.map snd a.a_params)
  608. let tconst_to_const = function
  609. | TInt i -> Int (Int32.to_string i)
  610. | TFloat s -> Float s
  611. | TString s -> String s
  612. | TBool b -> Ident (if b then "true" else "false")
  613. | TNull -> Ident "null"
  614. | TThis -> Ident "this"
  615. | TSuper -> Ident "super"
  616. (* ======= Field utility ======= *)
  617. let field_name f =
  618. match f with
  619. | FAnon f | FInstance (_,_,f) | FStatic (_,f) | FClosure (_,f) -> f.cf_name
  620. | FEnum (_,f) -> f.ef_name
  621. | FDynamic n -> n
  622. let extract_field = function
  623. | FAnon f | FInstance (_,_,f) | FStatic (_,f) | FClosure (_,f) -> Some f
  624. | _ -> None
  625. let is_extern_field f =
  626. match f.cf_kind with
  627. | Method _ -> false
  628. | Var { v_read = AccNormal | AccInline | AccNo } | Var { v_write = AccNormal | AccNo } -> false
  629. | _ -> not (Meta.has Meta.IsVar f.cf_meta)
  630. let field_type f =
  631. match f.cf_params with
  632. | [] -> f.cf_type
  633. | l -> monomorphs l f.cf_type
  634. let rec raw_class_field build_type c tl i =
  635. let apply = apply_params c.cl_params tl in
  636. try
  637. let f = PMap.find i c.cl_fields in
  638. Some (c,tl), build_type f , f
  639. with Not_found -> try (match c.cl_constructor with
  640. | Some ctor when i = "new" -> Some (c,tl), build_type ctor,ctor
  641. | _ -> raise Not_found)
  642. with Not_found -> try
  643. match c.cl_super with
  644. | None ->
  645. raise Not_found
  646. | Some (c,tl) ->
  647. let c2 , t , f = raw_class_field build_type c (List.map apply tl) i in
  648. c2, apply_params c.cl_params tl t , f
  649. with Not_found ->
  650. match c.cl_kind with
  651. | KTypeParameter tl ->
  652. let rec loop = function
  653. | [] ->
  654. raise Not_found
  655. | t :: ctl ->
  656. match follow t with
  657. | TAnon a ->
  658. (try
  659. let f = PMap.find i a.a_fields in
  660. None, build_type f, f
  661. with
  662. Not_found -> loop ctl)
  663. | TInst (c,tl) ->
  664. (try
  665. let c2, t , f = raw_class_field build_type c (List.map apply tl) i in
  666. c2, apply_params c.cl_params tl t, f
  667. with
  668. Not_found -> loop ctl)
  669. | _ ->
  670. loop ctl
  671. in
  672. loop tl
  673. | _ ->
  674. if not c.cl_interface then raise Not_found;
  675. (*
  676. an interface can implements other interfaces without
  677. having to redeclare its fields
  678. *)
  679. let rec loop = function
  680. | [] ->
  681. raise Not_found
  682. | (c,tl) :: l ->
  683. try
  684. let c2, t , f = raw_class_field build_type c (List.map apply tl) i in
  685. c2, apply_params c.cl_params tl t, f
  686. with
  687. Not_found -> loop l
  688. in
  689. loop c.cl_implements
  690. let class_field = raw_class_field field_type
  691. let quick_field t n =
  692. match follow t with
  693. | TInst (c,tl) ->
  694. let c, _, f = raw_class_field (fun f -> f.cf_type) c tl n in
  695. (match c with None -> FAnon f | Some (c,tl) -> FInstance (c,tl,f))
  696. | TAnon a ->
  697. (match !(a.a_status) with
  698. | EnumStatics e ->
  699. let ef = PMap.find n e.e_constrs in
  700. FEnum(e,ef)
  701. | Statics c ->
  702. FStatic (c,PMap.find n c.cl_statics)
  703. | AbstractStatics a ->
  704. begin match a.a_impl with
  705. | Some c ->
  706. let cf = PMap.find n c.cl_statics in
  707. FStatic(c,cf) (* is that right? *)
  708. | _ ->
  709. raise Not_found
  710. end
  711. | _ ->
  712. FAnon (PMap.find n a.a_fields))
  713. | TDynamic _ ->
  714. FDynamic n
  715. | TEnum _ | TMono _ | TAbstract _ | TFun _ ->
  716. raise Not_found
  717. | TLazy _ | TType _ ->
  718. assert false
  719. let quick_field_dynamic t s =
  720. try quick_field t s
  721. with Not_found -> FDynamic s
  722. let rec get_constructor build_type c =
  723. match c.cl_constructor, c.cl_super with
  724. | Some c, _ -> build_type c, c
  725. | None, None -> raise Not_found
  726. | None, Some (csup,cparams) ->
  727. let t, c = get_constructor build_type csup in
  728. apply_params csup.cl_params cparams t, c
  729. (* ======= Printing ======= *)
  730. let print_context() = ref []
  731. let rec s_type_kind t =
  732. let map tl = String.concat ", " (List.map s_type_kind tl) in
  733. match t with
  734. | TMono r ->
  735. begin match !r with
  736. | None -> "TMono (None)"
  737. | Some t -> "TMono (Some (" ^ (s_type_kind t) ^ "))"
  738. end
  739. | TEnum(en,tl) -> Printf.sprintf "TEnum(%s, [%s])" (s_type_path en.e_path) (map tl)
  740. | TInst(c,tl) -> Printf.sprintf "TInst(%s, [%s])" (s_type_path c.cl_path) (map tl)
  741. | TType(t,tl) -> Printf.sprintf "TType(%s, [%s])" (s_type_path t.t_path) (map tl)
  742. | TAbstract(a,tl) -> Printf.sprintf "TAbstract(%s, [%s])" (s_type_path a.a_path) (map tl)
  743. | TFun(tl,r) -> Printf.sprintf "TFun([%s], %s)" (String.concat ", " (List.map (fun (n,b,t) -> Printf.sprintf "%s%s:%s" (if b then "?" else "") n (s_type_kind t)) tl)) (s_type_kind r)
  744. | TAnon an -> "TAnon"
  745. | TDynamic t2 -> "TDynamic"
  746. | TLazy _ -> "TLazy"
  747. let rec s_type ctx t =
  748. match t with
  749. | TMono r ->
  750. (match !r with
  751. | None -> Printf.sprintf "Unknown<%d>" (try List.assq t (!ctx) with Not_found -> let n = List.length !ctx in ctx := (t,n) :: !ctx; n)
  752. | Some t -> s_type ctx t)
  753. | TEnum (e,tl) ->
  754. Ast.s_type_path e.e_path ^ s_type_params ctx tl
  755. | TInst (c,tl) ->
  756. (match c.cl_kind with
  757. | KExpr e -> Ast.s_expr e
  758. | _ -> Ast.s_type_path c.cl_path ^ s_type_params ctx tl)
  759. | TType (t,tl) ->
  760. Ast.s_type_path t.t_path ^ s_type_params ctx tl
  761. | TAbstract (a,tl) ->
  762. Ast.s_type_path a.a_path ^ s_type_params ctx tl
  763. | TFun ([],t) ->
  764. "Void -> " ^ s_fun ctx t false
  765. | TFun (l,t) ->
  766. String.concat " -> " (List.map (fun (s,b,t) ->
  767. (if b then "?" else "") ^ (if s = "" then "" else s ^ " : ") ^ s_fun ctx t true
  768. ) l) ^ " -> " ^ s_fun ctx t false
  769. | TAnon a ->
  770. 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
  771. "{" ^ (if not (is_closed a) then "+" else "") ^ String.concat "," fl ^ " }"
  772. | TDynamic t2 ->
  773. "Dynamic" ^ s_type_params ctx (if t == t2 then [] else [t2])
  774. | TLazy f ->
  775. s_type ctx (!f())
  776. and s_fun ctx t void =
  777. match t with
  778. | TFun _ ->
  779. "(" ^ s_type ctx t ^ ")"
  780. | TAbstract ({ a_path = ([],"Void") },[]) when void ->
  781. "(" ^ s_type ctx t ^ ")"
  782. | TMono r ->
  783. (match !r with
  784. | None -> s_type ctx t
  785. | Some t -> s_fun ctx t void)
  786. | TLazy f ->
  787. s_fun ctx (!f()) void
  788. | _ ->
  789. s_type ctx t
  790. and s_type_params ctx = function
  791. | [] -> ""
  792. | l -> "<" ^ String.concat ", " (List.map (s_type ctx) l) ^ ">"
  793. let s_access is_read = function
  794. | AccNormal -> "default"
  795. | AccNo -> "null"
  796. | AccNever -> "never"
  797. | AccResolve -> "resolve"
  798. | AccCall -> if is_read then "get" else "set"
  799. | AccInline -> "inline"
  800. | AccRequire (n,_) -> "require " ^ n
  801. let s_kind = function
  802. | Var { v_read = AccNormal; v_write = AccNormal } -> "var"
  803. | Var v -> "(" ^ s_access true v.v_read ^ "," ^ s_access false v.v_write ^ ")"
  804. | Method m ->
  805. match m with
  806. | MethNormal -> "method"
  807. | MethDynamic -> "dynamic method"
  808. | MethInline -> "inline method"
  809. | MethMacro -> "macro method"
  810. let s_expr_kind e =
  811. match e.eexpr with
  812. | TConst _ -> "Const"
  813. | TLocal _ -> "Local"
  814. | TArray (_,_) -> "Array"
  815. | TBinop (_,_,_) -> "Binop"
  816. | TEnumParameter (_,_,_) -> "EnumParameter"
  817. | TField (_,_) -> "Field"
  818. | TTypeExpr _ -> "TypeExpr"
  819. | TParenthesis _ -> "Parenthesis"
  820. | TObjectDecl _ -> "ObjectDecl"
  821. | TArrayDecl _ -> "ArrayDecl"
  822. | TCall (_,_) -> "Call"
  823. | TNew (_,_,_) -> "New"
  824. | TUnop (_,_,_) -> "Unop"
  825. | TFunction _ -> "Function"
  826. | TVar _ -> "Vars"
  827. | TBlock _ -> "Block"
  828. | TFor (_,_,_) -> "For"
  829. | TIf (_,_,_) -> "If"
  830. | TWhile (_,_,_) -> "While"
  831. | TSwitch (_,_,_) -> "Switch"
  832. | TTry (_,_) -> "Try"
  833. | TReturn _ -> "Return"
  834. | TBreak -> "Break"
  835. | TContinue -> "Continue"
  836. | TThrow _ -> "Throw"
  837. | TCast _ -> "Cast"
  838. | TMeta _ -> "Meta"
  839. let s_const = function
  840. | TInt i -> Int32.to_string i
  841. | TFloat s -> s
  842. | TString s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)
  843. | TBool b -> if b then "true" else "false"
  844. | TNull -> "null"
  845. | TThis -> "this"
  846. | TSuper -> "super"
  847. let rec s_expr s_type e =
  848. let sprintf = Printf.sprintf in
  849. let slist f l = String.concat "," (List.map f l) in
  850. let loop = s_expr s_type in
  851. let s_var v = v.v_name ^ ":" ^ string_of_int v.v_id ^ if v.v_capture then "[c]" else "" in
  852. let str = (match e.eexpr with
  853. | TConst c ->
  854. "Const " ^ s_const c
  855. | TLocal v ->
  856. "Local " ^ s_var v
  857. | TArray (e1,e2) ->
  858. sprintf "%s[%s]" (loop e1) (loop e2)
  859. | TBinop (op,e1,e2) ->
  860. sprintf "(%s %s %s)" (loop e1) (s_binop op) (loop e2)
  861. | TEnumParameter (e1,_,i) ->
  862. sprintf "%s[%i]" (loop e1) i
  863. | TField (e,f) ->
  864. let fstr = (match f with
  865. | FStatic (c,f) -> "static(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ ")"
  866. | FInstance (c,_,f) -> "inst(" ^ s_type_path c.cl_path ^ "." ^ f.cf_name ^ " : " ^ s_type f.cf_type ^ ")"
  867. | FClosure (c,f) -> "closure(" ^ (match c with None -> f.cf_name | Some (c,_) -> s_type_path c.cl_path ^ "." ^ f.cf_name) ^ ")"
  868. | FAnon f -> "anon(" ^ f.cf_name ^ ")"
  869. | FEnum (en,f) -> "enum(" ^ s_type_path en.e_path ^ "." ^ f.ef_name ^ ")"
  870. | FDynamic f -> "dynamic(" ^ f ^ ")"
  871. ) in
  872. sprintf "%s.%s" (loop e) fstr
  873. | TTypeExpr m ->
  874. sprintf "TypeExpr %s" (s_type_path (t_path m))
  875. | TParenthesis e ->
  876. sprintf "Parenthesis %s" (loop e)
  877. | TObjectDecl fl ->
  878. sprintf "ObjectDecl {%s}" (slist (fun (f,e) -> sprintf "%s : %s" f (loop e)) fl)
  879. | TArrayDecl el ->
  880. sprintf "ArrayDecl [%s]" (slist loop el)
  881. | TCall (e,el) ->
  882. sprintf "Call %s(%s)" (loop e) (slist loop el)
  883. | TNew (c,pl,el) ->
  884. sprintf "New %s%s(%s)" (s_type_path c.cl_path) (match pl with [] -> "" | l -> sprintf "<%s>" (slist s_type l)) (slist loop el)
  885. | TUnop (op,f,e) ->
  886. (match f with
  887. | Prefix -> sprintf "(%s %s)" (s_unop op) (loop e)
  888. | Postfix -> sprintf "(%s %s)" (loop e) (s_unop op))
  889. | TFunction f ->
  890. 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
  891. sprintf "Function(%s) : %s = %s" args (s_type f.tf_type) (loop f.tf_expr)
  892. | TVar (v,eo) ->
  893. sprintf "Vars %s" (sprintf "%s : %s%s" (s_var v) (s_type v.v_type) (match eo with None -> "" | Some e -> " = " ^ loop e))
  894. | TBlock el ->
  895. sprintf "Block {\n%s}" (String.concat "" (List.map (fun e -> sprintf "%s;\n" (loop e)) el))
  896. | TFor (v,econd,e) ->
  897. sprintf "For (%s : %s in %s,%s)" (s_var v) (s_type v.v_type) (loop econd) (loop e)
  898. | TIf (e,e1,e2) ->
  899. sprintf "If (%s,%s%s)" (loop e) (loop e1) (match e2 with None -> "" | Some e -> "," ^ loop e)
  900. | TWhile (econd,e,flag) ->
  901. (match flag with
  902. | NormalWhile -> sprintf "While (%s,%s)" (loop econd) (loop e)
  903. | DoWhile -> sprintf "DoWhile (%s,%s)" (loop e) (loop econd))
  904. | TSwitch (e,cases,def) ->
  905. 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)
  906. | TTry (e,cl) ->
  907. 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)
  908. | TReturn None ->
  909. "Return"
  910. | TReturn (Some e) ->
  911. sprintf "Return %s" (loop e)
  912. | TBreak ->
  913. "Break"
  914. | TContinue ->
  915. "Continue"
  916. | TThrow e ->
  917. "Throw " ^ (loop e)
  918. | TCast (e,t) ->
  919. sprintf "Cast %s%s" (match t with None -> "" | Some t -> s_type_path (t_path t) ^ ": ") (loop e)
  920. | TMeta ((n,el,_),e) ->
  921. sprintf "@%s%s %s" (Meta.to_string n) (match el with [] -> "" | _ -> "(" ^ (String.concat ", " (List.map Ast.s_expr el)) ^ ")") (loop e)
  922. ) in
  923. sprintf "(%s : %s)" str (s_type e.etype)
  924. and s_dt tabs tree =
  925. let s_type = s_type (print_context()) in
  926. tabs ^ match tree with
  927. | DTSwitch (st,cl,dto) ->
  928. "switch(" ^ (s_expr s_type st) ^ ") { \n" ^ tabs
  929. ^ (String.concat ("\n" ^ tabs) (List.map (fun (c,dt) ->
  930. "case " ^ (s_expr s_type c) ^ ":\n" ^ (s_dt (tabs ^ "\t") dt)
  931. ) cl))
  932. ^ (match dto with None -> "" | Some dt -> tabs ^ "default: " ^ (s_dt (tabs ^ "\t") dt))
  933. ^ "\n" ^ (if String.length tabs = 0 then "" else (String.sub tabs 0 (String.length tabs - 1))) ^ "}"
  934. | 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)
  935. | DTGoto i ->
  936. "goto " ^ (string_of_int i)
  937. | DTExpr e -> s_expr s_type e
  938. | 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))
  939. let rec s_expr_pretty tabs s_type e =
  940. let sprintf = Printf.sprintf in
  941. let loop = s_expr_pretty tabs s_type in
  942. let slist f l = String.concat "," (List.map f l) in
  943. match e.eexpr with
  944. | TConst c -> s_const c
  945. | TLocal v -> v.v_name
  946. | TArray (e1,e2) -> sprintf "%s[%s]" (loop e1) (loop e2)
  947. | TBinop (op,e1,e2) -> sprintf "%s %s %s" (loop e1) (s_binop op) (loop e2)
  948. | TEnumParameter (e1,_,i) -> sprintf "%s[%i]" (loop e1) i
  949. | TField (e1,s) -> sprintf "%s.%s" (loop e1) (field_name s)
  950. | TTypeExpr mt -> (s_type_path (t_path mt))
  951. | TParenthesis e1 -> sprintf "(%s)" (loop e1)
  952. | TObjectDecl fl -> sprintf "{%s}" (slist (fun (f,e) -> sprintf "%s : %s" f (loop e)) fl)
  953. | TArrayDecl el -> sprintf "[%s]" (slist loop el)
  954. | TCall (e1,el) -> sprintf "%s(%s)" (loop e1) (slist loop el)
  955. | TNew (c,pl,el) ->
  956. sprintf "new %s(%s)" (s_type_path c.cl_path) (slist loop el)
  957. | TUnop (op,f,e) ->
  958. (match f with
  959. | Prefix -> sprintf "%s %s" (s_unop op) (loop e)
  960. | Postfix -> sprintf "%s %s" (loop e) (s_unop op))
  961. | TFunction f ->
  962. 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
  963. sprintf "function(%s) = %s" args (loop f.tf_expr)
  964. | TVar (v,eo) ->
  965. sprintf "var %s" (sprintf "%s%s" v.v_name (match eo with None -> "" | Some e -> " = " ^ loop e))
  966. | TBlock el ->
  967. let ntabs = tabs ^ "\t" in
  968. 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
  969. s ^ tabs ^ "}"
  970. | TFor (v,econd,e) ->
  971. sprintf "for (%s in %s) %s" v.v_name (loop econd) (loop e)
  972. | TIf (e,e1,e2) ->
  973. sprintf "if (%s)%s%s" (loop e) (loop e1) (match e2 with None -> "" | Some e -> " else " ^ loop e)
  974. | TWhile (econd,e,flag) ->
  975. (match flag with
  976. | NormalWhile -> sprintf "while (%s) %s" (loop econd) (loop e)
  977. | DoWhile -> sprintf "do (%s) while(%s)" (loop e) (loop econd))
  978. | TSwitch (e,cases,def) ->
  979. let ntabs = tabs ^ "\t" in
  980. 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
  981. s ^ tabs ^ "}"
  982. | TTry (e,cl) ->
  983. 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)
  984. | TReturn None ->
  985. "return"
  986. | TReturn (Some e) ->
  987. sprintf "return %s" (loop e)
  988. | TBreak ->
  989. "break"
  990. | TContinue ->
  991. "continue"
  992. | TThrow e ->
  993. "throw " ^ (loop e)
  994. | TCast (e,None) ->
  995. sprintf "cast %s" (loop e)
  996. | TCast (e,Some mt) ->
  997. sprintf "cast (%s,%s)" (loop e) (s_type_path (t_path mt))
  998. | TMeta ((n,el,_),e) ->
  999. sprintf "@%s%s %s" (Meta.to_string n) (match el with [] -> "" | _ -> "(" ^ (String.concat ", " (List.map Ast.s_expr el)) ^ ")") (loop e)
  1000. let rec s_expr_ast print_var_ids tabs s_type e =
  1001. let sprintf = Printf.sprintf in
  1002. let loop ?(extra_tabs="") = s_expr_ast print_var_ids (tabs ^ "\t" ^ extra_tabs) s_type in
  1003. let tag_args tabs sl = match sl with
  1004. | [] -> ""
  1005. | [s] when not (String.contains s '\n') -> " " ^ s
  1006. | _ ->
  1007. let tabs = "\n" ^ tabs ^ "\t" in
  1008. tabs ^ (String.concat tabs sl)
  1009. in
  1010. let tag s ?(t=None) ?(extra_tabs="") sl =
  1011. let st = match t with
  1012. | None -> s_type e.etype
  1013. | Some t -> s_type t
  1014. in
  1015. sprintf "[%s:%s]%s" s st (tag_args (tabs ^ extra_tabs) sl)
  1016. in
  1017. let var_id v = if print_var_ids then v.v_id else 0 in
  1018. let const c = sprintf "[Const %s:%s]" (s_const c) (s_type e.etype) in
  1019. let local v = sprintf "[Local %s(%i):%s]" v.v_name (var_id v) (s_type v.v_type) in
  1020. let var v sl = sprintf "[Var %s(%i):%s]%s" v.v_name (var_id v) (s_type v.v_type) (tag_args tabs sl) in
  1021. let module_type mt = sprintf "[TypeExpr %s:%s]" (s_type_path (t_path mt)) (s_type e.etype) in
  1022. match e.eexpr with
  1023. | TConst c -> const c
  1024. | TLocal v -> local v
  1025. | TArray (e1,e2) -> tag "Array" [loop e1; loop e2]
  1026. | TBinop (op,e1,e2) -> tag "Binop" [loop e1; s_binop op; loop e2]
  1027. | TUnop (op,flag,e1) -> tag "Unop" [s_unop op; if flag = Postfix then "Postfix" else "Prefix"; loop e1]
  1028. | TEnumParameter (e1,ef,i) -> tag "EnumParameter" [loop e1; ef.ef_name; string_of_int i]
  1029. | TField (e1,fa) ->
  1030. let sfa = match fa with
  1031. | FInstance(c,tl,cf) -> tag "FInstance" ~extra_tabs:"\t" [s_type (TInst(c,tl)); cf.cf_name]
  1032. | FStatic(c,cf) -> tag "FStatic" ~extra_tabs:"\t" [s_type_path c.cl_path; cf.cf_name]
  1033. | FClosure(co,cf) -> tag "FClosure" ~extra_tabs:"\t" [(match co with None -> "None" | Some (c,tl) -> s_type (TInst(c,tl))); cf.cf_name]
  1034. | FAnon cf -> tag "FAnon" ~extra_tabs:"\t" [cf.cf_name]
  1035. | FDynamic s -> tag "FDynamic" ~extra_tabs:"\t" [s]
  1036. | FEnum(en,ef) -> tag "FEnum" ~extra_tabs:"\t" [s_type_path en.e_path; ef.ef_name]
  1037. in
  1038. tag "Field" [loop e1; sfa]
  1039. | TTypeExpr mt -> module_type mt
  1040. | TParenthesis e1 -> tag "Parenthesis" [loop e1]
  1041. | TObjectDecl fl -> tag "ObjectDecl" (List.map (fun (s,e) -> sprintf "%s: %s" s (loop e)) fl)
  1042. | TArrayDecl el -> tag "ArrayDecl" (List.map loop el)
  1043. | TCall (e1,el) -> tag "Call" (loop e1 :: (List.map loop el))
  1044. | TNew (c,tl,el) -> tag "New" ((s_type (TInst(c,tl))) :: (List.map loop el))
  1045. | TFunction f ->
  1046. let arg (v,cto) =
  1047. tag "Arg" ~t:(Some v.v_type) ~extra_tabs:"\t" (match cto with None -> [local v] | Some ct -> [local v;const ct])
  1048. in
  1049. tag "Function" ((List.map arg f.tf_args) @ [loop f.tf_expr])
  1050. | TVar (v,eo) -> var v (match eo with None -> [] | Some e -> [loop e])
  1051. | TBlock el -> tag "Block" (List.map loop el)
  1052. | TIf (e,e1,e2) -> tag "If" (loop e :: (Printf.sprintf "[Then:%s] %s" (s_type e1.etype) (loop e1)) :: (match e2 with None -> [] | Some e -> [Printf.sprintf "[Else:%s] %s" (s_type e.etype) (loop e)]))
  1053. | TCast (e1,None) -> tag "Cast" [loop e1]
  1054. | TCast (e1,Some mt) -> tag "Cast" [loop e1; module_type mt]
  1055. | TThrow e1 -> tag "Throw" [loop e1]
  1056. | TBreak -> tag "Break" []
  1057. | TContinue -> tag "Continue" []
  1058. | TReturn None -> tag "Return" []
  1059. | TReturn (Some e1) -> tag "Return" [loop e1]
  1060. | TWhile (e1,e2,NormalWhile) -> tag "While" [loop e1; loop e2]
  1061. | TWhile (e1,e2,DoWhile) -> tag "Do" [loop e1; loop e2]
  1062. | TFor (v,e1,e2) -> tag "For" [local v; loop e1; loop e2]
  1063. | TTry (e1,catches) ->
  1064. let sl = List.map (fun (v,e) ->
  1065. sprintf "Catch %s%s" (local v) (tag_args (tabs ^ "\t") [loop ~extra_tabs:"\t" e]);
  1066. ) catches in
  1067. tag "Try" ((loop e1) :: sl)
  1068. | TSwitch (e1,cases,eo) ->
  1069. let sl = List.map (fun (el,e) ->
  1070. tag "Case" ~t:(Some e.etype) ~extra_tabs:"\t" ((List.map loop el) @ [loop ~extra_tabs:"\t" e])
  1071. ) cases in
  1072. let sl = match eo with
  1073. | None -> sl
  1074. | Some e -> sl @ [tag "Default" ~t:(Some e.etype) ~extra_tabs:"\t" [loop ~extra_tabs:"\t" e]]
  1075. in
  1076. tag "Switch" ((loop e1) :: sl)
  1077. | TMeta ((m,el,_),e1) ->
  1078. let s = Meta.to_string m in
  1079. let s = match el with
  1080. | [] -> s
  1081. | _ -> sprintf "%s(%s)" s (String.concat ", " (List.map Ast.s_expr el))
  1082. in
  1083. tag "Meta" [s; loop e1]
  1084. let s_types ?(sep = ", ") tl =
  1085. let pctx = print_context() in
  1086. String.concat sep (List.map (s_type pctx) tl)
  1087. let s_class_kind = function
  1088. | KNormal ->
  1089. "KNormal"
  1090. | KTypeParameter tl ->
  1091. Printf.sprintf "KTypeParameter [%s]" (s_types tl)
  1092. | KExtension(c,tl) ->
  1093. Printf.sprintf "KExtension %s<%s>" (s_type_path c.cl_path) (s_types tl)
  1094. | KExpr _ ->
  1095. "KExpr"
  1096. | KGeneric ->
  1097. "KGeneric"
  1098. | KGenericInstance(c,tl) ->
  1099. Printf.sprintf "KGenericInstance %s<%s>" (s_type_path c.cl_path) (s_types tl)
  1100. | KMacroType ->
  1101. "KMacroType"
  1102. | KGenericBuild _ ->
  1103. "KGenericBuild"
  1104. | KAbstractImpl a ->
  1105. Printf.sprintf "KAbstractImpl %s" (s_type_path a.a_path)
  1106. (* ======= Unification ======= *)
  1107. let rec link e a b =
  1108. (* tell if setting a == b will create a type-loop *)
  1109. let rec loop t =
  1110. if t == a then
  1111. true
  1112. else match t with
  1113. | TMono t -> (match !t with None -> false | Some t -> loop t)
  1114. | TEnum (_,tl) -> List.exists loop tl
  1115. | TInst (_,tl) | TType (_,tl) | TAbstract (_,tl) -> List.exists loop tl
  1116. | TFun (tl,t) -> List.exists (fun (_,_,t) -> loop t) tl || loop t
  1117. | TDynamic t2 ->
  1118. if t == t2 then
  1119. false
  1120. else
  1121. loop t2
  1122. | TLazy f ->
  1123. loop (!f())
  1124. | TAnon a ->
  1125. try
  1126. PMap.iter (fun _ f -> if loop f.cf_type then raise Exit) a.a_fields;
  1127. false
  1128. with
  1129. Exit -> true
  1130. in
  1131. (* tell is already a ~= b *)
  1132. if loop b then
  1133. (follow b) == a
  1134. else if b == t_dynamic then
  1135. true
  1136. else begin
  1137. e := Some b;
  1138. true
  1139. end
  1140. let rec fast_eq a b =
  1141. if a == b then
  1142. true
  1143. else match a , b with
  1144. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  1145. List.for_all2 (fun (_,_,t1) (_,_,t2) -> fast_eq t1 t2) l1 l2 && fast_eq r1 r2
  1146. | TType (t1,l1), TType (t2,l2) ->
  1147. t1 == t2 && List.for_all2 fast_eq l1 l2
  1148. | TEnum (e1,l1), TEnum (e2,l2) ->
  1149. e1 == e2 && List.for_all2 fast_eq l1 l2
  1150. | TInst (c1,l1), TInst (c2,l2) ->
  1151. c1 == c2 && List.for_all2 fast_eq l1 l2
  1152. | TAbstract (a1,l1), TAbstract (a2,l2) ->
  1153. a1 == a2 && List.for_all2 fast_eq l1 l2
  1154. | _ , _ ->
  1155. false
  1156. let rec fast_eq_mono ml a b =
  1157. if a == b then
  1158. true
  1159. else match a , b with
  1160. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  1161. List.for_all2 (fun (_,_,t1) (_,_,t2) -> fast_eq_mono ml t1 t2) l1 l2 && fast_eq_mono ml r1 r2
  1162. | TType (t1,l1), TType (t2,l2) ->
  1163. t1 == t2 && List.for_all2 (fast_eq_mono ml) l1 l2
  1164. | TEnum (e1,l1), TEnum (e2,l2) ->
  1165. e1 == e2 && List.for_all2 (fast_eq_mono ml) l1 l2
  1166. | TInst (c1,l1), TInst (c2,l2) ->
  1167. c1 == c2 && List.for_all2 (fast_eq_mono ml) l1 l2
  1168. | TAbstract (a1,l1), TAbstract (a2,l2) ->
  1169. a1 == a2 && List.for_all2 (fast_eq_mono ml) l1 l2
  1170. | TMono _, _ ->
  1171. List.memq a ml
  1172. | _ , _ ->
  1173. false
  1174. (* perform unification with subtyping.
  1175. the first type is always the most down in the class hierarchy
  1176. it's also the one that is pointed by the position.
  1177. It's actually a typecheck of A :> B where some mutations can happen *)
  1178. type unify_error =
  1179. | Cannot_unify of t * t
  1180. | Invalid_field_type of string
  1181. | Has_no_field of t * string
  1182. | Has_no_runtime_field of t * string
  1183. | Has_extra_field of t * string
  1184. | Invalid_kind of string * field_kind * field_kind
  1185. | Invalid_visibility of string
  1186. | Not_matching_optional of string
  1187. | Cant_force_optional
  1188. | Invariant_parameter of t * t
  1189. | Constraint_failure of string
  1190. | Missing_overload of tclass_field * t
  1191. | Unify_custom of string
  1192. exception Unify_error of unify_error list
  1193. let cannot_unify a b = Cannot_unify (a,b)
  1194. let invalid_field n = Invalid_field_type n
  1195. let invalid_kind n a b = Invalid_kind (n,a,b)
  1196. let invalid_visibility n = Invalid_visibility n
  1197. let has_no_field t n = Has_no_field (t,n)
  1198. let has_extra_field t n = Has_extra_field (t,n)
  1199. let error l = raise (Unify_error l)
  1200. let has_meta m ml = List.exists (fun (m2,_,_) -> m = m2) ml
  1201. let get_meta m ml = List.find (fun (m2,_,_) -> m = m2) ml
  1202. let no_meta = []
  1203. (*
  1204. we can restrict access as soon as both are runtime-compatible
  1205. *)
  1206. let unify_access a1 a2 =
  1207. a1 = a2 || match a1, a2 with
  1208. | _, AccNo | _, AccNever -> true
  1209. | AccInline, AccNormal -> true
  1210. | _ -> false
  1211. let direct_access = function
  1212. | AccNo | AccNever | AccNormal | AccInline | AccRequire _ -> true
  1213. | AccResolve | AccCall -> false
  1214. let unify_kind k1 k2 =
  1215. k1 = k2 || match k1, k2 with
  1216. | Var v1, Var v2 -> unify_access v1.v_read v2.v_read && unify_access v1.v_write v2.v_write
  1217. | Var v, Method m ->
  1218. (match v.v_read, v.v_write, m with
  1219. | AccNormal, _, MethNormal -> true
  1220. | AccNormal, AccNormal, MethDynamic -> true
  1221. | _ -> false)
  1222. | Method m, Var v ->
  1223. (match m with
  1224. | MethDynamic -> direct_access v.v_read && direct_access v.v_write
  1225. | MethMacro -> false
  1226. | MethNormal | MethInline ->
  1227. match v.v_read,v.v_write with
  1228. | AccNormal,(AccNo | AccNever) -> true
  1229. | _ -> false)
  1230. | Method m1, Method m2 ->
  1231. match m1,m2 with
  1232. | MethInline, MethNormal
  1233. | MethDynamic, MethNormal -> true
  1234. | _ -> false
  1235. let eq_stack = ref []
  1236. type eq_kind =
  1237. | EqStrict
  1238. | EqCoreType
  1239. | EqRightDynamic
  1240. | EqBothDynamic
  1241. | EqDoNotFollowNull (* like EqStrict, but does not follow Null<T> *)
  1242. let rec type_eq param a b =
  1243. let can_follow t = match param with
  1244. | EqCoreType -> false
  1245. | EqDoNotFollowNull -> not (is_null t)
  1246. | _ -> true
  1247. in
  1248. if a == b then
  1249. ()
  1250. else match a , b with
  1251. | TLazy f , _ -> type_eq param (!f()) b
  1252. | _ , TLazy f -> type_eq param a (!f())
  1253. | TMono t , _ ->
  1254. (match !t with
  1255. | None -> if param = EqCoreType || not (link t a b) then error [cannot_unify a b]
  1256. | Some t -> type_eq param t b)
  1257. | _ , TMono t ->
  1258. (match !t with
  1259. | None -> if param = EqCoreType || not (link t b a) then error [cannot_unify a b]
  1260. | Some t -> type_eq param a t)
  1261. | TType (t1,tl1), TType (t2,tl2) when (t1 == t2 || (param = EqCoreType && t1.t_path = t2.t_path)) && List.length tl1 = List.length tl2 ->
  1262. List.iter2 (type_eq param) tl1 tl2
  1263. | TType (t,tl) , _ when can_follow a ->
  1264. type_eq param (apply_params t.t_params tl t.t_type) b
  1265. | _ , TType (t,tl) when can_follow b ->
  1266. if List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!eq_stack) then
  1267. ()
  1268. else begin
  1269. eq_stack := (a,b) :: !eq_stack;
  1270. try
  1271. type_eq param a (apply_params t.t_params tl t.t_type);
  1272. eq_stack := List.tl !eq_stack;
  1273. with
  1274. Unify_error l ->
  1275. eq_stack := List.tl !eq_stack;
  1276. error (cannot_unify a b :: l)
  1277. end
  1278. | TEnum (e1,tl1) , TEnum (e2,tl2) ->
  1279. if e1 != e2 && not (param = EqCoreType && e1.e_path = e2.e_path) then error [cannot_unify a b];
  1280. List.iter2 (type_eq param) tl1 tl2
  1281. | TInst (c1,tl1) , TInst (c2,tl2) ->
  1282. 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];
  1283. List.iter2 (type_eq param) tl1 tl2
  1284. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  1285. (try
  1286. type_eq param r1 r2;
  1287. List.iter2 (fun (n,o1,t1) (_,o2,t2) ->
  1288. if o1 <> o2 then error [Not_matching_optional n];
  1289. type_eq param t1 t2
  1290. ) l1 l2
  1291. with
  1292. Unify_error l -> error (cannot_unify a b :: l))
  1293. | TDynamic a , TDynamic b ->
  1294. type_eq param a b
  1295. | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
  1296. if a1 != a2 && not (param = EqCoreType && a1.a_path = a2.a_path) then error [cannot_unify a b];
  1297. List.iter2 (type_eq param) tl1 tl2
  1298. | TAnon a1, TAnon a2 ->
  1299. (try
  1300. PMap.iter (fun n f1 ->
  1301. try
  1302. let f2 = PMap.find n a2.a_fields in
  1303. 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];
  1304. try
  1305. type_eq param f1.cf_type f2.cf_type
  1306. with
  1307. Unify_error l -> error (invalid_field n :: l)
  1308. with
  1309. Not_found ->
  1310. if is_closed a2 then error [has_no_field b n];
  1311. if not (link (ref None) b f1.cf_type) then error [cannot_unify a b];
  1312. a2.a_fields <- PMap.add n f1 a2.a_fields
  1313. ) a1.a_fields;
  1314. PMap.iter (fun n f2 ->
  1315. if not (PMap.mem n a1.a_fields) then begin
  1316. if is_closed a1 then error [has_no_field a n];
  1317. if not (link (ref None) a f2.cf_type) then error [cannot_unify a b];
  1318. a1.a_fields <- PMap.add n f2 a1.a_fields
  1319. end;
  1320. ) a2.a_fields;
  1321. with
  1322. Unify_error l -> error (cannot_unify a b :: l))
  1323. | _ , _ ->
  1324. if b == t_dynamic && (param = EqRightDynamic || param = EqBothDynamic) then
  1325. ()
  1326. else if a == t_dynamic && param = EqBothDynamic then
  1327. ()
  1328. else
  1329. error [cannot_unify a b]
  1330. let type_iseq a b =
  1331. try
  1332. type_eq EqStrict a b;
  1333. true
  1334. with
  1335. Unify_error _ -> false
  1336. let type_iseq_strict a b =
  1337. try
  1338. type_eq EqDoNotFollowNull a b;
  1339. true
  1340. with Unify_error _ ->
  1341. false
  1342. let unify_stack = ref []
  1343. let abstract_cast_stack = ref []
  1344. let unify_new_monos = ref []
  1345. let rec unify a b =
  1346. if a == b then
  1347. ()
  1348. else match a, b with
  1349. | TLazy f , _ -> unify (!f()) b
  1350. | _ , TLazy f -> unify a (!f())
  1351. | TMono t , _ ->
  1352. (match !t with
  1353. | None -> if not (link t a b) then error [cannot_unify a b]
  1354. | Some t -> unify t b)
  1355. | _ , TMono t ->
  1356. (match !t with
  1357. | None -> if not (link t b a) then error [cannot_unify a b]
  1358. | Some t -> unify a t)
  1359. | TType (t,tl) , _ ->
  1360. if not (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!unify_stack)) then begin
  1361. try
  1362. unify_stack := (a,b) :: !unify_stack;
  1363. unify (apply_params t.t_params tl t.t_type) b;
  1364. unify_stack := List.tl !unify_stack;
  1365. with
  1366. Unify_error l ->
  1367. unify_stack := List.tl !unify_stack;
  1368. error (cannot_unify a b :: l)
  1369. end
  1370. | _ , TType (t,tl) ->
  1371. if not (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!unify_stack)) then begin
  1372. try
  1373. unify_stack := (a,b) :: !unify_stack;
  1374. unify a (apply_params t.t_params tl t.t_type);
  1375. unify_stack := List.tl !unify_stack;
  1376. with
  1377. Unify_error l ->
  1378. unify_stack := List.tl !unify_stack;
  1379. error (cannot_unify a b :: l)
  1380. end
  1381. | TEnum (ea,tl1) , TEnum (eb,tl2) ->
  1382. if ea != eb then error [cannot_unify a b];
  1383. unify_type_params a b tl1 tl2
  1384. | TAbstract (a1,tl1) , TAbstract (a2,tl2) when a1 == a2 ->
  1385. begin try
  1386. unify_type_params a b tl1 tl2
  1387. with Unify_error _ as err ->
  1388. (* the type could still have a from/to relation to itself (issue #3494) *)
  1389. begin try
  1390. unify_abstracts a b a1 tl1 a2 tl2
  1391. with Unify_error _ ->
  1392. raise err
  1393. end
  1394. end
  1395. | TAbstract ({a_path=[],"Void"},_) , _
  1396. | _ , TAbstract ({a_path=[],"Void"},_) ->
  1397. error [cannot_unify a b]
  1398. | TAbstract (a1,tl1) , TAbstract (a2,tl2) ->
  1399. unify_abstracts a b a1 tl1 a2 tl2
  1400. | TInst (c1,tl1) , TInst (c2,tl2) ->
  1401. let rec loop c tl =
  1402. if c == c2 then begin
  1403. unify_type_params a b tl tl2;
  1404. true
  1405. end else (match c.cl_super with
  1406. | None -> false
  1407. | Some (cs,tls) ->
  1408. loop cs (List.map (apply_params c.cl_params tl) tls)
  1409. ) || List.exists (fun (cs,tls) ->
  1410. loop cs (List.map (apply_params c.cl_params tl) tls)
  1411. ) c.cl_implements
  1412. || (match c.cl_kind with
  1413. | KTypeParameter pl -> List.exists (fun t -> match follow t with TInst (cs,tls) -> loop cs (List.map (apply_params c.cl_params tl) tls) | _ -> false) pl
  1414. | _ -> false)
  1415. in
  1416. if not (loop c1 tl1) then error [cannot_unify a b]
  1417. | TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
  1418. let i = ref 0 in
  1419. (try
  1420. (match r2 with
  1421. | TAbstract ({a_path=[],"Void"},_) -> incr i
  1422. | _ -> unify r1 r2; incr i);
  1423. List.iter2 (fun (_,o1,t1) (_,o2,t2) ->
  1424. if o1 && not o2 then error [Cant_force_optional];
  1425. unify t1 t2;
  1426. incr i
  1427. ) l2 l1 (* contravariance *)
  1428. with
  1429. Unify_error l ->
  1430. let msg = if !i = 0 then "Cannot unify return types" else "Cannot unify argument " ^ (string_of_int !i) in
  1431. error (cannot_unify a b :: Unify_custom msg :: l))
  1432. | TInst (c,tl) , TAnon an ->
  1433. if PMap.is_empty an.a_fields then (match c.cl_kind with
  1434. | KTypeParameter pl ->
  1435. (* one of the constraints must unify with { } *)
  1436. if not (List.exists (fun t -> match follow t with TInst _ | TAnon _ -> true | _ -> false) pl) then error [cannot_unify a b]
  1437. | _ -> ());
  1438. (try
  1439. PMap.iter (fun n f2 ->
  1440. (*
  1441. introducing monomorphs while unifying might create infinite loops - see #2315
  1442. let's store these monomorphs and make sure we reach a fixed point
  1443. *)
  1444. let monos = ref [] in
  1445. let make_type f =
  1446. match f.cf_params with
  1447. | [] -> f.cf_type
  1448. | l ->
  1449. let ml = List.map (fun _ -> mk_mono()) l in
  1450. monos := ml;
  1451. apply_params f.cf_params ml f.cf_type
  1452. in
  1453. let _, ft, f1 = (try raw_class_field make_type c tl n with Not_found -> error [has_no_field a n]) in
  1454. let ft = apply_params c.cl_params tl ft in
  1455. if not (unify_kind f1.cf_kind f2.cf_kind) then error [invalid_kind n f1.cf_kind f2.cf_kind];
  1456. if f2.cf_public && not f1.cf_public then error [invalid_visibility n];
  1457. let old_monos = !unify_new_monos in
  1458. unify_new_monos := !monos @ !unify_new_monos;
  1459. if not (List.exists (fun (a2,b2) -> fast_eq b2 f2.cf_type && fast_eq_mono !unify_new_monos ft a2) (!unify_stack)) then begin
  1460. unify_stack := (ft,f2.cf_type) :: !unify_stack;
  1461. (try
  1462. unify_with_access ft f2
  1463. with
  1464. Unify_error l ->
  1465. unify_new_monos := old_monos;
  1466. unify_stack := List.tl !unify_stack;
  1467. error (invalid_field n :: l));
  1468. unify_stack := List.tl !unify_stack;
  1469. end;
  1470. unify_new_monos := old_monos;
  1471. List.iter (fun f2o ->
  1472. if not (List.exists (fun f1o -> type_iseq f1o.cf_type f2o.cf_type) (f1 :: f1.cf_overloads))
  1473. then error [Missing_overload (f1, f2o.cf_type)]
  1474. ) f2.cf_overloads;
  1475. (* we mark the field as :?used because it might be used through the structure *)
  1476. if not (Meta.has Meta.MaybeUsed f1.cf_meta) then f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta;
  1477. (match f1.cf_kind with
  1478. | Method MethInline ->
  1479. 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)];
  1480. | _ -> ());
  1481. ) an.a_fields;
  1482. (match !(an.a_status) with
  1483. | Opened -> an.a_status := Closed;
  1484. | Statics _ | EnumStatics _ | AbstractStatics _ -> error []
  1485. | Closed | Extend _ | Const -> ())
  1486. with
  1487. Unify_error l -> error (cannot_unify a b :: l))
  1488. | TAnon a1, TAnon a2 ->
  1489. unify_anons a b a1 a2
  1490. | TAnon an, TAbstract ({ a_path = [],"Class" },[pt]) ->
  1491. (match !(an.a_status) with
  1492. | Statics cl -> unify (TInst (cl,List.map (fun _ -> mk_mono()) cl.cl_params)) pt
  1493. | _ -> error [cannot_unify a b])
  1494. | TAnon an, TAbstract ({ a_path = [],"Enum" },[pt]) ->
  1495. (match !(an.a_status) with
  1496. | EnumStatics e -> unify (TEnum (e,List.map (fun _ -> mk_mono()) e.e_params)) pt
  1497. | _ -> error [cannot_unify a b])
  1498. | TEnum _, TAbstract ({ a_path = [],"EnumValue" },[]) ->
  1499. ()
  1500. | TEnum(en,_), TAbstract ({ a_path = ["haxe"],"FlatEnum" },[]) when Meta.has Meta.FlatEnum en.e_meta ->
  1501. ()
  1502. | TFun _, TAbstract ({ a_path = ["haxe"],"Function" },[]) ->
  1503. ()
  1504. | TDynamic t , _ ->
  1505. if t == a then
  1506. ()
  1507. else (match b with
  1508. | TDynamic t2 ->
  1509. if t2 != b then
  1510. (try
  1511. type_eq EqRightDynamic t t2
  1512. with
  1513. Unify_error l -> error (cannot_unify a b :: l));
  1514. | TAbstract(bb,tl) when (List.exists (unify_from bb tl a b) bb.a_from) ->
  1515. ()
  1516. | _ ->
  1517. error [cannot_unify a b])
  1518. | _ , TDynamic t ->
  1519. if t == b then
  1520. ()
  1521. else (match a with
  1522. | TDynamic t2 ->
  1523. if t2 != a then
  1524. (try
  1525. type_eq EqRightDynamic t t2
  1526. with
  1527. Unify_error l -> error (cannot_unify a b :: l));
  1528. | TAnon an ->
  1529. (try
  1530. (match !(an.a_status) with
  1531. | Statics _ | EnumStatics _ -> error []
  1532. | Opened -> an.a_status := Closed
  1533. | _ -> ());
  1534. PMap.iter (fun _ f ->
  1535. try
  1536. type_eq EqStrict (field_type f) t
  1537. with Unify_error l ->
  1538. error (invalid_field f.cf_name :: l)
  1539. ) an.a_fields
  1540. with Unify_error l ->
  1541. error (cannot_unify a b :: l))
  1542. | TAbstract(aa,tl) when (List.exists (unify_to aa tl b) aa.a_to) ->
  1543. ()
  1544. | _ ->
  1545. error [cannot_unify a b])
  1546. | TAbstract (aa,tl), _ ->
  1547. if not (List.exists (unify_to aa tl b) aa.a_to) then error [cannot_unify a b];
  1548. | TInst ({ cl_kind = KTypeParameter ctl } as c,pl), TAbstract (bb,tl) ->
  1549. (* one of the constraints must satisfy the abstract *)
  1550. if not (List.exists (fun t ->
  1551. let t = apply_params c.cl_params pl t in
  1552. try unify t b; true with Unify_error _ -> false
  1553. ) ctl) && not (List.exists (unify_from bb tl a b) bb.a_from) then error [cannot_unify a b];
  1554. | _, TAbstract (bb,tl) ->
  1555. if not (List.exists (unify_from bb tl a b) bb.a_from) then error [cannot_unify a b]
  1556. | _ , _ ->
  1557. error [cannot_unify a b]
  1558. and unify_abstracts a b a1 tl1 a2 tl2 =
  1559. let f1 = unify_to a1 tl1 b in
  1560. let f2 = unify_from a2 tl2 a b in
  1561. if (List.exists (f1 ~allow_transitive_cast:false) a1.a_to)
  1562. || (List.exists (f2 ~allow_transitive_cast:false) a2.a_from)
  1563. || (((Meta.has Meta.CoreType a1.a_meta) || (Meta.has Meta.CoreType a2.a_meta))
  1564. && ((List.exists f1 a1.a_to) || (List.exists f2 a2.a_from))) then
  1565. ()
  1566. else
  1567. error [cannot_unify a b]
  1568. and unify_anons a b a1 a2 =
  1569. (try
  1570. PMap.iter (fun n f2 ->
  1571. try
  1572. let f1 = PMap.find n a1.a_fields in
  1573. if not (unify_kind f1.cf_kind f2.cf_kind) then
  1574. (match !(a1.a_status), f1.cf_kind, f2.cf_kind with
  1575. | Opened, Var { v_read = AccNormal; v_write = AccNo }, Var { v_read = AccNormal; v_write = AccNormal } ->
  1576. f1.cf_kind <- f2.cf_kind;
  1577. | _ -> error [invalid_kind n f1.cf_kind f2.cf_kind]);
  1578. if f2.cf_public && not f1.cf_public then error [invalid_visibility n];
  1579. try
  1580. unify_with_access f1.cf_type f2;
  1581. (match !(a1.a_status) with
  1582. | Statics c when not (Meta.has Meta.MaybeUsed f1.cf_meta) -> f1.cf_meta <- (Meta.MaybeUsed,[],f1.cf_pos) :: f1.cf_meta
  1583. | _ -> ());
  1584. with
  1585. Unify_error l -> error (invalid_field n :: l)
  1586. with
  1587. Not_found ->
  1588. match !(a1.a_status) with
  1589. | Opened ->
  1590. if not (link (ref None) a f2.cf_type) then error [];
  1591. a1.a_fields <- PMap.add n f2 a1.a_fields
  1592. | Const when Meta.has Meta.Optional f2.cf_meta ->
  1593. ()
  1594. | _ ->
  1595. error [has_no_field a n];
  1596. ) a2.a_fields;
  1597. (match !(a1.a_status) with
  1598. | Const when not (PMap.is_empty a2.a_fields) ->
  1599. PMap.iter (fun n _ -> if not (PMap.mem n a2.a_fields) then error [has_extra_field a n]) a1.a_fields;
  1600. | Opened ->
  1601. a1.a_status := Closed
  1602. | _ -> ());
  1603. (match !(a2.a_status) with
  1604. | Statics c -> (match !(a1.a_status) with Statics c2 when c == c2 -> () | _ -> error [])
  1605. | EnumStatics e -> (match !(a1.a_status) with EnumStatics e2 when e == e2 -> () | _ -> error [])
  1606. | AbstractStatics a -> (match !(a1.a_status) with AbstractStatics a2 when a == a2 -> () | _ -> error [])
  1607. | Opened -> a2.a_status := Closed
  1608. | Const | Extend _ | Closed -> ())
  1609. with
  1610. Unify_error l -> error (cannot_unify a b :: l))
  1611. and unify_from ab tl a b ?(allow_transitive_cast=true) t =
  1612. if (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!abstract_cast_stack)) then false else begin
  1613. abstract_cast_stack := (a,b) :: !abstract_cast_stack;
  1614. let t = apply_params ab.a_params tl t in
  1615. let unify_func = if allow_transitive_cast then unify else type_eq EqStrict in
  1616. let b = try
  1617. unify_func a t;
  1618. true
  1619. with Unify_error _ ->
  1620. false
  1621. in
  1622. abstract_cast_stack := List.tl !abstract_cast_stack;
  1623. b
  1624. end
  1625. and unify_to ab tl b ?(allow_transitive_cast=true) t =
  1626. let t = apply_params ab.a_params tl t in
  1627. let unify_func = if allow_transitive_cast then unify else type_eq EqStrict in
  1628. try
  1629. unify_func t b;
  1630. true
  1631. with Unify_error _ ->
  1632. false
  1633. and unify_from_field ab tl a b ?(allow_transitive_cast=true) (t,cf) =
  1634. if (List.exists (fun (a2,b2) -> fast_eq a a2 && fast_eq b b2) (!abstract_cast_stack)) then false else begin
  1635. abstract_cast_stack := (a,b) :: !abstract_cast_stack;
  1636. let unify_func = if allow_transitive_cast then unify else type_eq EqStrict in
  1637. let b = try
  1638. begin match follow cf.cf_type with
  1639. | TFun(_,r) ->
  1640. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  1641. let map t = apply_params ab.a_params tl (apply_params cf.cf_params monos t) in
  1642. unify_func a (map t);
  1643. List.iter2 (fun m (name,t) -> match follow t with
  1644. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  1645. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> unify m (map tc) ) constr
  1646. | _ -> ()
  1647. ) monos cf.cf_params;
  1648. unify_func (map r) b;
  1649. | _ -> assert false
  1650. end;
  1651. true
  1652. with Unify_error _ -> false
  1653. in
  1654. abstract_cast_stack := List.tl !abstract_cast_stack;
  1655. b
  1656. end
  1657. and unify_to_field ab tl b ?(allow_transitive_cast=true) (t,cf) =
  1658. let a = TAbstract(ab,tl) in
  1659. if (List.exists (fun (b2,a2) -> fast_eq a a2 && fast_eq b b2) (!abstract_cast_stack)) then false else begin
  1660. abstract_cast_stack := (b,a) :: !abstract_cast_stack;
  1661. let unify_func = if allow_transitive_cast then unify else type_eq EqStrict in
  1662. let r = try
  1663. begin match follow cf.cf_type with
  1664. | TFun((_,_,ta) :: _,_) ->
  1665. let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
  1666. let map t = apply_params ab.a_params tl (apply_params cf.cf_params monos t) in
  1667. let athis = map ab.a_this in
  1668. (* we cannot allow implicit casts when the this type is not completely known yet *)
  1669. (* if has_mono athis then raise (Unify_error []); *)
  1670. with_variance (type_eq EqStrict) athis (map ta);
  1671. (* immediate constraints checking is ok here because we know there are no monomorphs *)
  1672. List.iter2 (fun m (name,t) -> match follow t with
  1673. | TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
  1674. List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> unify m (map tc) ) constr
  1675. | _ -> ()
  1676. ) monos cf.cf_params;
  1677. unify_func (map t) b;
  1678. | _ -> assert false
  1679. end;
  1680. true
  1681. with Unify_error _ -> false
  1682. in
  1683. abstract_cast_stack := List.tl !abstract_cast_stack;
  1684. r
  1685. end
  1686. and unify_with_variance f t1 t2 =
  1687. let allows_variance_to t tf = type_iseq tf t in
  1688. match follow t1,follow t2 with
  1689. | TInst(c1,tl1),TInst(c2,tl2) when c1 == c2 ->
  1690. List.iter2 f tl1 tl2
  1691. | TEnum(en1,tl1),TEnum(en2,tl2) when en1 == en2 ->
  1692. List.iter2 f tl1 tl2
  1693. | TAbstract(a1,tl1),TAbstract(a2,tl2) when a1 == a2 && Meta.has Meta.CoreType a1.a_meta ->
  1694. List.iter2 f tl1 tl2
  1695. | TAbstract(a1,pl1),TAbstract(a2,pl2) ->
  1696. if (Meta.has Meta.CoreType a1.a_meta) && (Meta.has Meta.CoreType a2.a_meta) then begin
  1697. let ta1 = apply_params a1.a_params pl1 a1.a_this in
  1698. let ta2 = apply_params a2.a_params pl2 a2.a_this in
  1699. type_eq EqStrict ta1 ta2;
  1700. end;
  1701. if not (List.exists (allows_variance_to t2) a1.a_to) && not (List.exists (allows_variance_to t1) a2.a_from) then
  1702. error [cannot_unify t1 t2]
  1703. | TAbstract(a,pl),t ->
  1704. type_eq EqBothDynamic (apply_params a.a_params pl a.a_this) t;
  1705. if not (List.exists (fun t2 -> allows_variance_to t (apply_params a.a_params pl t2)) a.a_to) then error [cannot_unify t1 t2]
  1706. | t,TAbstract(a,pl) ->
  1707. type_eq EqBothDynamic t (apply_params a.a_params pl a.a_this);
  1708. if not (List.exists (fun t2 -> allows_variance_to t (apply_params a.a_params pl t2)) a.a_from) then error [cannot_unify t1 t2]
  1709. | TAnon a1,TAnon a2 ->
  1710. unify_anons t1 t2 a1 a2
  1711. | _ ->
  1712. error [cannot_unify t1 t2]
  1713. and unify_type_params a b tl1 tl2 =
  1714. List.iter2 (fun t1 t2 ->
  1715. try
  1716. with_variance (type_eq EqRightDynamic) t1 t2
  1717. with Unify_error l ->
  1718. let err = cannot_unify a b in
  1719. error (err :: (Invariant_parameter (t1,t2)) :: l)
  1720. ) tl1 tl2
  1721. and with_variance f t1 t2 =
  1722. try
  1723. f t1 t2
  1724. with Unify_error l -> try
  1725. unify_with_variance (with_variance f) t1 t2
  1726. with Unify_error _ ->
  1727. raise (Unify_error l)
  1728. and unify_with_access t1 f2 =
  1729. match f2.cf_kind with
  1730. (* write only *)
  1731. | Var { v_read = AccNo } | Var { v_read = AccNever } -> unify f2.cf_type t1
  1732. (* read only *)
  1733. | Method MethNormal | Method MethInline | Var { v_write = AccNo } | Var { v_write = AccNever } -> unify t1 f2.cf_type
  1734. (* read/write *)
  1735. | _ -> with_variance (type_eq EqBothDynamic) t1 f2.cf_type
  1736. module Abstract = struct
  1737. open Ast
  1738. let find_to ab pl b =
  1739. if follow b == t_dynamic then
  1740. List.find (fun (t,_) -> follow t == t_dynamic) ab.a_to_field
  1741. else if List.exists (unify_to ab pl ~allow_transitive_cast:false b) ab.a_to then
  1742. raise Not_found (* legacy compatibility *)
  1743. else
  1744. List.find (unify_to_field ab pl b) ab.a_to_field
  1745. let find_from ab pl a b =
  1746. if follow a == t_dynamic then
  1747. List.find (fun (t,_) -> follow t == t_dynamic) ab.a_from_field
  1748. else if List.exists (unify_from ab pl a ~allow_transitive_cast:false b) ab.a_from then
  1749. raise Not_found (* legacy compatibility *)
  1750. else
  1751. List.find (unify_from_field ab pl a b) ab.a_from_field
  1752. let underlying_type_stack = ref []
  1753. let rec get_underlying_type a pl =
  1754. let maybe_recurse t =
  1755. underlying_type_stack := a :: !underlying_type_stack;
  1756. let t = match follow t with
  1757. | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
  1758. if List.mem a !underlying_type_stack then begin
  1759. let s = String.concat " -> " (List.map (fun a -> s_type_path a.a_path) (List.rev (a :: !underlying_type_stack))) in
  1760. raise (Error("Abstract chain detected: " ^ s,a.a_pos))
  1761. end;
  1762. get_underlying_type a tl
  1763. | _ ->
  1764. t
  1765. in
  1766. underlying_type_stack := List.tl !underlying_type_stack;
  1767. t
  1768. in
  1769. try
  1770. if not (Meta.has Meta.MultiType a.a_meta) then raise Not_found;
  1771. let m = mk_mono() in
  1772. let _ = find_to a pl m in
  1773. maybe_recurse (follow m)
  1774. with Not_found ->
  1775. if Meta.has Meta.CoreType a.a_meta then
  1776. t_dynamic
  1777. else
  1778. maybe_recurse (apply_params a.a_params pl a.a_this)
  1779. let rec follow_with_abstracts t = match follow t with
  1780. | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
  1781. follow_with_abstracts (get_underlying_type a tl)
  1782. | t ->
  1783. t
  1784. end
  1785. (* ======= Mapping and iterating ======= *)
  1786. let iter_dt f dt = match dt with
  1787. | DTBind(_,dt) -> f dt
  1788. | DTSwitch(_,cl,dto) ->
  1789. List.iter (fun (_,dt) -> f dt) cl;
  1790. (match dto with None -> () | Some dt -> f dt)
  1791. | DTGuard(_,dt1,dt2) ->
  1792. f dt1;
  1793. (match dt2 with None -> () | Some dt -> f dt)
  1794. | DTGoto _ | DTExpr _ -> ()
  1795. let iter f e =
  1796. match e.eexpr with
  1797. | TConst _
  1798. | TLocal _
  1799. | TBreak
  1800. | TContinue
  1801. | TTypeExpr _ ->
  1802. ()
  1803. | TArray (e1,e2)
  1804. | TBinop (_,e1,e2)
  1805. | TFor (_,e1,e2)
  1806. | TWhile (e1,e2,_) ->
  1807. f e1;
  1808. f e2;
  1809. | TThrow e
  1810. | TField (e,_)
  1811. | TEnumParameter (e,_,_)
  1812. | TParenthesis e
  1813. | TCast (e,_)
  1814. | TUnop (_,_,e)
  1815. | TMeta(_,e) ->
  1816. f e
  1817. | TArrayDecl el
  1818. | TNew (_,_,el)
  1819. | TBlock el ->
  1820. List.iter f el
  1821. | TObjectDecl fl ->
  1822. List.iter (fun (_,e) -> f e) fl
  1823. | TCall (e,el) ->
  1824. f e;
  1825. List.iter f el
  1826. | TVar (v,eo) ->
  1827. (match eo with None -> () | Some e -> f e)
  1828. | TFunction fu ->
  1829. f fu.tf_expr
  1830. | TIf (e,e1,e2) ->
  1831. f e;
  1832. f e1;
  1833. (match e2 with None -> () | Some e -> f e)
  1834. | TSwitch (e,cases,def) ->
  1835. f e;
  1836. List.iter (fun (el,e2) -> List.iter f el; f e2) cases;
  1837. (match def with None -> () | Some e -> f e)
  1838. | TTry (e,catches) ->
  1839. f e;
  1840. List.iter (fun (_,e) -> f e) catches
  1841. | TReturn eo ->
  1842. (match eo with None -> () | Some e -> f e)
  1843. let map_expr f e =
  1844. match e.eexpr with
  1845. | TConst _
  1846. | TLocal _
  1847. | TBreak
  1848. | TContinue
  1849. | TTypeExpr _ ->
  1850. e
  1851. | TArray (e1,e2) ->
  1852. let e1 = f e1 in
  1853. { e with eexpr = TArray (e1,f e2) }
  1854. | TBinop (op,e1,e2) ->
  1855. let e1 = f e1 in
  1856. { e with eexpr = TBinop (op,e1,f e2) }
  1857. | TFor (v,e1,e2) ->
  1858. let e1 = f e1 in
  1859. { e with eexpr = TFor (v,e1,f e2) }
  1860. | TWhile (e1,e2,flag) ->
  1861. let e1 = f e1 in
  1862. { e with eexpr = TWhile (e1,f e2,flag) }
  1863. | TThrow e1 ->
  1864. { e with eexpr = TThrow (f e1) }
  1865. | TEnumParameter (e1,ef,i) ->
  1866. { e with eexpr = TEnumParameter(f e1,ef,i) }
  1867. | TField (e1,v) ->
  1868. { e with eexpr = TField (f e1,v) }
  1869. | TParenthesis e1 ->
  1870. { e with eexpr = TParenthesis (f e1) }
  1871. | TUnop (op,pre,e1) ->
  1872. { e with eexpr = TUnop (op,pre,f e1) }
  1873. | TArrayDecl el ->
  1874. { e with eexpr = TArrayDecl (List.map f el) }
  1875. | TNew (t,pl,el) ->
  1876. { e with eexpr = TNew (t,pl,List.map f el) }
  1877. | TBlock el ->
  1878. { e with eexpr = TBlock (List.map f el) }
  1879. | TObjectDecl el ->
  1880. { e with eexpr = TObjectDecl (List.map (fun (v,e) -> v, f e) el) }
  1881. | TCall (e1,el) ->
  1882. { e with eexpr = TCall (f e1, List.map f el) }
  1883. | TVar (v,eo) ->
  1884. { e with eexpr = TVar (v, match eo with None -> None | Some e -> Some (f e)) }
  1885. | TFunction fu ->
  1886. { e with eexpr = TFunction { fu with tf_expr = f fu.tf_expr } }
  1887. | TIf (ec,e1,e2) ->
  1888. let ec = f ec in
  1889. let e1 = f e1 in
  1890. { e with eexpr = TIf (ec,e1,match e2 with None -> None | Some e -> Some (f e)) }
  1891. | TSwitch (e1,cases,def) ->
  1892. let e1 = f e1 in
  1893. let cases = List.map (fun (el,e2) -> List.map f el, f e2) cases in
  1894. { e with eexpr = TSwitch (e1, cases, match def with None -> None | Some e -> Some (f e)) }
  1895. | TTry (e1,catches) ->
  1896. let e1 = f e1 in
  1897. { e with eexpr = TTry (e1, List.map (fun (v,e) -> v, f e) catches) }
  1898. | TReturn eo ->
  1899. { e with eexpr = TReturn (match eo with None -> None | Some e -> Some (f e)) }
  1900. | TCast (e1,t) ->
  1901. { e with eexpr = TCast (f e1,t) }
  1902. | TMeta (m,e1) ->
  1903. {e with eexpr = TMeta(m,f e1)}
  1904. let map_expr_type f ft fv e =
  1905. match e.eexpr with
  1906. | TConst _
  1907. | TBreak
  1908. | TContinue
  1909. | TTypeExpr _ ->
  1910. { e with etype = ft e.etype }
  1911. | TLocal v ->
  1912. { e with eexpr = TLocal (fv v); etype = ft e.etype }
  1913. | TArray (e1,e2) ->
  1914. let e1 = f e1 in
  1915. { e with eexpr = TArray (e1,f e2); etype = ft e.etype }
  1916. | TBinop (op,e1,e2) ->
  1917. let e1 = f e1 in
  1918. { e with eexpr = TBinop (op,e1,f e2); etype = ft e.etype }
  1919. | TFor (v,e1,e2) ->
  1920. let v = fv v in
  1921. let e1 = f e1 in
  1922. { e with eexpr = TFor (v,e1,f e2); etype = ft e.etype }
  1923. | TWhile (e1,e2,flag) ->
  1924. let e1 = f e1 in
  1925. { e with eexpr = TWhile (e1,f e2,flag); etype = ft e.etype }
  1926. | TThrow e1 ->
  1927. { e with eexpr = TThrow (f e1); etype = ft e.etype }
  1928. | TEnumParameter (e1,ef,i) ->
  1929. { e with eexpr = TEnumParameter(f e1,ef,i); etype = ft e.etype }
  1930. | TField (e1,v) ->
  1931. let e1 = f e1 in
  1932. let v = try
  1933. let n = match v with
  1934. | FClosure _ -> raise Not_found
  1935. | FAnon f | FInstance (_,_,f) | FStatic (_,f) -> f.cf_name
  1936. | FEnum (_,f) -> f.ef_name
  1937. | FDynamic n -> n
  1938. in
  1939. quick_field e1.etype n
  1940. with Not_found ->
  1941. v
  1942. in
  1943. { e with eexpr = TField (e1,v); etype = ft e.etype }
  1944. | TParenthesis e1 ->
  1945. { e with eexpr = TParenthesis (f e1); etype = ft e.etype }
  1946. | TUnop (op,pre,e1) ->
  1947. { e with eexpr = TUnop (op,pre,f e1); etype = ft e.etype }
  1948. | TArrayDecl el ->
  1949. { e with eexpr = TArrayDecl (List.map f el); etype = ft e.etype }
  1950. | TNew (c,pl,el) ->
  1951. let et = ft e.etype in
  1952. (* make sure that we use the class corresponding to the replaced type *)
  1953. let t = match c.cl_kind with
  1954. | KTypeParameter _ | KGeneric ->
  1955. et
  1956. | _ ->
  1957. ft (TInst(c,pl))
  1958. in
  1959. let c, pl = (match follow t with TInst (c,pl) -> (c,pl) | TAbstract({a_impl = Some c},pl) -> c,pl | t -> error [has_no_field t "new"]) in
  1960. { e with eexpr = TNew (c,pl,List.map f el); etype = et }
  1961. | TBlock el ->
  1962. { e with eexpr = TBlock (List.map f el); etype = ft e.etype }
  1963. | TObjectDecl el ->
  1964. { e with eexpr = TObjectDecl (List.map (fun (v,e) -> v, f e) el); etype = ft e.etype }
  1965. | TCall (e1,el) ->
  1966. let e1 = f e1 in
  1967. { e with eexpr = TCall (e1, List.map f el); etype = ft e.etype }
  1968. | TVar (v,eo) ->
  1969. { e with eexpr = TVar (fv v, match eo with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1970. | TFunction fu ->
  1971. let fu = {
  1972. tf_expr = f fu.tf_expr;
  1973. tf_args = List.map (fun (v,o) -> fv v, o) fu.tf_args;
  1974. tf_type = ft fu.tf_type;
  1975. } in
  1976. { e with eexpr = TFunction fu; etype = ft e.etype }
  1977. | TIf (ec,e1,e2) ->
  1978. let ec = f ec in
  1979. let e1 = f e1 in
  1980. { e with eexpr = TIf (ec,e1,match e2 with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1981. | TSwitch (e1,cases,def) ->
  1982. let e1 = f e1 in
  1983. let cases = List.map (fun (el,e2) -> List.map f el, f e2) cases in
  1984. { e with eexpr = TSwitch (e1, cases, match def with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1985. | TTry (e1,catches) ->
  1986. let e1 = f e1 in
  1987. { e with eexpr = TTry (e1, List.map (fun (v,e) -> fv v, f e) catches); etype = ft e.etype }
  1988. | TReturn eo ->
  1989. { e with eexpr = TReturn (match eo with None -> None | Some e -> Some (f e)); etype = ft e.etype }
  1990. | TCast (e1,t) ->
  1991. { e with eexpr = TCast (f e1,t); etype = ft e.etype }
  1992. | TMeta (m,e1) ->
  1993. {e with eexpr = TMeta(m, f e1); etype = ft e.etype }
  1994. module TExprToExpr = struct
  1995. let tpath p mp pl =
  1996. if snd mp = snd p then
  1997. CTPath {
  1998. tpackage = fst p;
  1999. tname = snd p;
  2000. tparams = List.map (fun t -> TPType t) pl;
  2001. tsub = None;
  2002. }
  2003. else CTPath {
  2004. tpackage = fst mp;
  2005. tname = snd mp;
  2006. tparams = List.map (fun t -> TPType t) pl;
  2007. tsub = Some (snd p);
  2008. }
  2009. let rec convert_type = function
  2010. | TMono r ->
  2011. (match !r with
  2012. | None -> raise Exit
  2013. | Some t -> convert_type t)
  2014. | TInst ({cl_private = true; cl_path=_,name},tl)
  2015. | TEnum ({e_private = true; e_path=_,name},tl)
  2016. | TType ({t_private = true; t_path=_,name},tl)
  2017. | TAbstract ({a_private = true; a_path=_,name},tl) ->
  2018. CTPath {
  2019. tpackage = [];
  2020. tname = name;
  2021. tparams = List.map (fun t -> TPType (convert_type t)) tl;
  2022. tsub = None;
  2023. }
  2024. | TEnum (e,pl) ->
  2025. tpath e.e_path e.e_module.m_path (List.map convert_type pl)
  2026. | TInst({cl_kind = KTypeParameter _} as c,pl) ->
  2027. tpath ([],snd c.cl_path) ([],snd c.cl_path) (List.map convert_type pl)
  2028. | TInst (c,pl) ->
  2029. tpath c.cl_path c.cl_module.m_path (List.map convert_type pl)
  2030. | TType (t,pl) as tf ->
  2031. (* recurse on type-type *)
  2032. if (snd t.t_path).[0] = '#' then convert_type (follow tf) else tpath t.t_path t.t_module.m_path (List.map convert_type pl)
  2033. | TAbstract (a,pl) ->
  2034. tpath a.a_path a.a_module.m_path (List.map convert_type pl)
  2035. | TFun (args,ret) ->
  2036. CTFunction (List.map (fun (_,_,t) -> convert_type t) args, convert_type ret)
  2037. | TAnon a ->
  2038. begin match !(a.a_status) with
  2039. | Statics c -> tpath ([],"Class") ([],"Class") [tpath c.cl_path c.cl_path []]
  2040. | EnumStatics e -> tpath ([],"Enum") ([],"Enum") [tpath e.e_path e.e_path []]
  2041. | _ ->
  2042. CTAnonymous (PMap.foldi (fun _ f acc ->
  2043. {
  2044. cff_name = f.cf_name;
  2045. cff_kind = FVar (mk_ot f.cf_type,None);
  2046. cff_pos = f.cf_pos;
  2047. cff_doc = f.cf_doc;
  2048. cff_meta = f.cf_meta;
  2049. cff_access = [];
  2050. } :: acc
  2051. ) a.a_fields [])
  2052. end
  2053. | (TDynamic t2) as t ->
  2054. tpath ([],"Dynamic") ([],"Dynamic") (if t == t_dynamic then [] else [convert_type t2])
  2055. | TLazy f ->
  2056. convert_type ((!f)())
  2057. and mk_ot t =
  2058. match follow t with
  2059. | TMono _ -> None
  2060. | _ -> (try Some (convert_type t) with Exit -> None)
  2061. let rec convert_expr e =
  2062. let full_type_path t =
  2063. let mp,p = match t with
  2064. | TClassDecl c -> c.cl_module.m_path,c.cl_path
  2065. | TEnumDecl en -> en.e_module.m_path,en.e_path
  2066. | TAbstractDecl a -> a.a_module.m_path,a.a_path
  2067. | TTypeDecl t -> t.t_module.m_path,t.t_path
  2068. in
  2069. if snd mp = snd p then p else (fst mp) @ [snd mp],snd p
  2070. in
  2071. let mk_path = expr_of_type_path in
  2072. let mk_ident = function
  2073. | "`trace" -> Ident "trace"
  2074. | n -> Ident n
  2075. in
  2076. let eopt = function None -> None | Some e -> Some (convert_expr e) in
  2077. ((match e.eexpr with
  2078. | TConst c ->
  2079. EConst (tconst_to_const c)
  2080. | TLocal v -> EConst (mk_ident v.v_name)
  2081. | TArray (e1,e2) -> EArray (convert_expr e1,convert_expr e2)
  2082. | TBinop (op,e1,e2) -> EBinop (op, convert_expr e1, convert_expr e2)
  2083. | TField (e,f) -> EField (convert_expr e, field_name f)
  2084. | TTypeExpr t -> fst (mk_path (full_type_path t) e.epos)
  2085. | TParenthesis e -> EParenthesis (convert_expr e)
  2086. | TObjectDecl fl -> EObjectDecl (List.map (fun (f,e) -> f, convert_expr e) fl)
  2087. | TArrayDecl el -> EArrayDecl (List.map convert_expr el)
  2088. | TCall (e,el) -> ECall (convert_expr e,List.map convert_expr el)
  2089. | TNew (c,pl,el) -> ENew ((match (try convert_type (TInst (c,pl)) with Exit -> convert_type (TInst (c,[]))) with CTPath p -> p | _ -> assert false),List.map convert_expr el)
  2090. | TUnop (op,p,e) -> EUnop (op,p,convert_expr e)
  2091. | TFunction f ->
  2092. let arg (v,c) = v.v_name, false, mk_ot v.v_type, (match c with None -> None | Some c -> Some (EConst (tconst_to_const c),e.epos)) in
  2093. EFunction (None,{ f_params = []; f_args = List.map arg f.tf_args; f_type = mk_ot f.tf_type; f_expr = Some (convert_expr f.tf_expr) })
  2094. | TVar (v,eo) ->
  2095. EVars ([v.v_name, mk_ot v.v_type, eopt eo])
  2096. | TBlock el -> EBlock (List.map convert_expr el)
  2097. | TFor (v,it,e) ->
  2098. let ein = (EIn ((EConst (Ident v.v_name),it.epos),convert_expr it),it.epos) in
  2099. EFor (ein,convert_expr e)
  2100. | TIf (e,e1,e2) -> EIf (convert_expr e,convert_expr e1,eopt e2)
  2101. | TWhile (e1,e2,flag) -> EWhile (convert_expr e1, convert_expr e2, flag)
  2102. | TSwitch (e,cases,def) ->
  2103. let cases = List.map (fun (vl,e) ->
  2104. List.map convert_expr vl,None,(match e.eexpr with TBlock [] -> None | _ -> Some (convert_expr e))
  2105. ) cases in
  2106. let def = match eopt def with None -> None | Some (EBlock [],_) -> Some None | e -> Some e in
  2107. ESwitch (convert_expr e,cases,def)
  2108. | TEnumParameter _ ->
  2109. (* these are considered complex, so the AST is handled in TMeta(Meta.Ast) *)
  2110. assert false
  2111. | TTry (e,catches) -> ETry (convert_expr e,List.map (fun (v,e) -> v.v_name, (try convert_type v.v_type with Exit -> assert false), convert_expr e) catches)
  2112. | TReturn e -> EReturn (eopt e)
  2113. | TBreak -> EBreak
  2114. | TContinue -> EContinue
  2115. | TThrow e -> EThrow (convert_expr e)
  2116. | TCast (e,t) ->
  2117. let t = (match t with
  2118. | None -> None
  2119. | Some t ->
  2120. let t = (match t with TClassDecl c -> TInst (c,[]) | TEnumDecl e -> TEnum (e,[]) | TTypeDecl t -> TType (t,[]) | TAbstractDecl a -> TAbstract (a,[])) in
  2121. Some (try convert_type t with Exit -> assert false)
  2122. ) in
  2123. ECast (convert_expr e,t)
  2124. | TMeta ((Meta.Ast,[e1,_],_),_) -> e1
  2125. | TMeta (m,e) -> EMeta(m,convert_expr e))
  2126. ,e.epos)
  2127. end
  2128. let print_if b e =
  2129. if b then print_endline (s_expr_pretty "" (s_type (print_context())) e)