type.ml 66 KB

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