type.ml 75 KB

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