typer.ml 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. (*
  2. The Haxe Compiler
  3. Copyright (C) 2005-2019 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 Extlib_leftovers
  17. open Ast
  18. open DisplayTypes.DisplayMode
  19. open DisplayException
  20. open DisplayTypes.CompletionResultKind
  21. open CompletionItem.ClassFieldOrigin
  22. open Common
  23. open Type
  24. open Typecore
  25. open Error
  26. open Globals
  27. open TyperBase
  28. open Fields
  29. open CallUnification
  30. open Calls
  31. open Operators
  32. (* ---------------------------------------------------------------------- *)
  33. (* TOOLS *)
  34. let get_iterator_param t =
  35. match follow t with
  36. | TAnon a ->
  37. if !(a.a_status) <> Closed then raise Not_found;
  38. (match follow (PMap.find "hasNext" a.a_fields).cf_type, follow (PMap.find "next" a.a_fields).cf_type with
  39. | TFun ([],tb), TFun([],t) when (match follow tb with TAbstract ({ a_path = [],"Bool" },[]) -> true | _ -> false) ->
  40. if PMap.fold (fun _ acc -> acc + 1) a.a_fields 0 <> 2 then raise Not_found;
  41. t
  42. | _ ->
  43. raise Not_found)
  44. | _ ->
  45. raise Not_found
  46. let get_iterable_param t =
  47. match follow t with
  48. | TAnon a ->
  49. if !(a.a_status) <> Closed then raise Not_found;
  50. (match follow (PMap.find "iterator" a.a_fields).cf_type with
  51. | TFun ([],it) ->
  52. let t = get_iterator_param it in
  53. if PMap.fold (fun _ acc -> acc + 1) a.a_fields 0 <> 1 then raise Not_found;
  54. t
  55. | _ ->
  56. raise Not_found)
  57. | _ -> raise Not_found
  58. let maybe_type_against_enum ctx f with_type iscall p =
  59. try
  60. begin match with_type with
  61. | WithType.WithType(t,_) ->
  62. let rec loop stack t = match follow t with
  63. | TEnum (en,_) ->
  64. true,en.e_path,en.e_names,TEnumDecl en
  65. | TAbstract ({a_impl = Some c} as a,_) when a.a_enum ->
  66. let fields = ExtList.List.filter_map (fun cf ->
  67. if has_class_field_flag cf CfEnum then Some cf.cf_name else None
  68. ) c.cl_ordered_statics in
  69. false,a.a_path,fields,TAbstractDecl a
  70. | TAbstract (a,pl) when not (Meta.has Meta.CoreType a.a_meta) ->
  71. begin match get_abstract_froms a pl with
  72. | [t2] ->
  73. if (List.exists (shallow_eq t) stack) then raise Exit;
  74. loop (t :: stack) t2
  75. | _ -> raise Exit
  76. end
  77. | _ ->
  78. raise Exit
  79. in
  80. let is_enum,path,fields,mt = loop [] t in
  81. let old = ctx.m.curmod.m_types in
  82. let restore () = ctx.m.curmod.m_types <- old in
  83. ctx.m.curmod.m_types <- ctx.m.curmod.m_types @ [mt];
  84. let e = try
  85. f()
  86. with
  87. | Error (Unknown_ident n,_) ->
  88. restore();
  89. raise_or_display_message ctx (StringError.string_error n fields ("Identifier '" ^ n ^ "' is not part of " ^ s_type_path path)) p;
  90. AKExpr (mk (TConst TNull) (mk_mono()) p)
  91. | exc ->
  92. restore();
  93. raise exc;
  94. in
  95. restore();
  96. begin match e with
  97. | AKExpr e ->
  98. begin match follow e.etype with
  99. | TFun(_,t') when is_enum ->
  100. (* TODO: this is a dodge for #7603 *)
  101. (try Type.unify t' t with Unify_error _ -> ());
  102. AKExpr e
  103. | _ ->
  104. if iscall then
  105. AKExpr e
  106. else begin
  107. AKExpr (AbstractCast.cast_or_unify ctx t e e.epos)
  108. end
  109. end
  110. | _ -> e (* ??? *)
  111. end
  112. | _ ->
  113. raise Exit
  114. end
  115. with Exit ->
  116. f()
  117. let check_error ctx err p = match err with
  118. | Module_not_found ([],name) when Diagnostics.is_diagnostics_run ctx.com p ->
  119. DisplayToplevel.handle_unresolved_identifier ctx name p true
  120. | _ ->
  121. display_error ctx (error_msg err) p
  122. (* ---------------------------------------------------------------------- *)
  123. (* PASS 3 : type expression & check structure *)
  124. let rec unify_min_raise ctx (el:texpr list) : t =
  125. let basic = ctx.com.basic in
  126. match el with
  127. | [] -> spawn_monomorph ctx null_pos
  128. | [e] -> e.etype
  129. | _ ->
  130. let rec chk_null e = is_null e.etype || is_explicit_null e.etype ||
  131. match e.eexpr with
  132. | TConst TNull -> true
  133. | TBlock el ->
  134. (match List.rev el with
  135. | [] -> false
  136. | e :: _ -> chk_null e)
  137. | TParenthesis e | TMeta(_,e) -> chk_null e
  138. | _ -> false
  139. in
  140. (* First pass: Try normal unification and find out if null is involved. *)
  141. let rec loop t = function
  142. | [] ->
  143. false, t
  144. | e :: el ->
  145. let t = if chk_null e then basic.tnull t else t in
  146. try
  147. Type.unify e.etype t;
  148. loop t el
  149. with Unify_error _ -> try
  150. Type.unify t e.etype;
  151. loop (if is_null t then basic.tnull e.etype else e.etype) el
  152. with Unify_error _ ->
  153. true, t
  154. in
  155. let has_error, t = loop (spawn_monomorph ctx null_pos) el in
  156. if not has_error then
  157. t
  158. else try
  159. (* specific case for const anon : we don't want to hide fields but restrict their common type *)
  160. let fcount = ref (-1) in
  161. let field_count a =
  162. PMap.fold (fun _ acc -> acc + 1) a.a_fields 0
  163. in
  164. let expr f = match f.cf_expr with None -> mk (TBlock []) f.cf_type f.cf_pos | Some e -> e in
  165. let fields = List.fold_left (fun acc e ->
  166. match follow e.etype with
  167. | TAnon a when !(a.a_status) = Const ->
  168. if !fcount = -1 then begin
  169. fcount := field_count a;
  170. PMap.map (fun f -> [expr f]) a.a_fields
  171. end else begin
  172. if !fcount <> field_count a then raise Not_found;
  173. PMap.mapi (fun n el -> expr (PMap.find n a.a_fields) :: el) acc
  174. end
  175. | _ ->
  176. raise Not_found
  177. ) PMap.empty el in
  178. let fields = PMap.foldi (fun n el acc ->
  179. let t = try unify_min_raise ctx el with Unify_error _ -> raise Not_found in
  180. PMap.add n (mk_field n t (List.hd el).epos null_pos) acc
  181. ) fields PMap.empty in
  182. mk_anon ~fields (ref Closed)
  183. with Not_found -> try
  184. (* specific case for TFun, see #9579 *)
  185. let e0,el = match el with
  186. | e0 :: el -> e0,el
  187. | _ -> raise Exit
  188. in
  189. let args,tr0 = match follow e0.etype with
  190. | TFun(tl,tr) ->
  191. Array.of_list tl,tr
  192. | _ ->
  193. raise Exit
  194. in
  195. let arity = Array.length args in
  196. let rets = List.map (fun e -> match follow e.etype with
  197. | TFun(tl,tr) ->
  198. let ta = Array.of_list tl in
  199. if Array.length ta <> arity then raise Exit;
  200. for i = 0 to arity - 1 do
  201. let (_,_,tcur) = args.(i) in
  202. let (_,_,tnew) as argnew = ta.(i) in
  203. if Type.does_unify tnew tcur then
  204. args.(i) <- argnew
  205. else if not (Type.does_unify tcur tnew) then
  206. raise Exit
  207. done;
  208. tr
  209. | _ ->
  210. raise Exit
  211. ) el in
  212. let common_types = UnifyMinT.collect_base_types tr0 in
  213. let tr = match UnifyMinT.unify_min' default_unification_context common_types rets with
  214. | UnifyMinOk t ->
  215. t
  216. | UnifyMinError(l,index) ->
  217. raise Exit
  218. in
  219. TFun(Array.to_list args,tr)
  220. with Exit ->
  221. (* Second pass: Get all base types (interfaces, super classes and their interfaces) of most general type.
  222. Then for each additional type filter all types that do not unify. *)
  223. let common_types = UnifyMinT.collect_base_types t in
  224. let dyn_types = List.fold_left (fun acc t ->
  225. let rec loop c =
  226. Meta.has Meta.UnifyMinDynamic c.cl_meta || (match c.cl_super with None -> false | Some (c,_) -> loop c)
  227. in
  228. match t with
  229. | TInst (c,params) when params <> [] && loop c ->
  230. TInst (c,List.map (fun _ -> t_dynamic) params) :: acc
  231. | _ -> acc
  232. ) [] common_types in
  233. let common_types = (match List.rev dyn_types with [] -> common_types | l -> common_types @ l) in
  234. let el = List.tl el in
  235. let tl = List.map (fun e -> e.etype) el in
  236. begin match UnifyMinT.unify_min' default_unification_context common_types tl with
  237. | UnifyMinOk t ->
  238. t
  239. | UnifyMinError(l,index) ->
  240. raise_error (Unify l) (List.nth el index).epos
  241. end
  242. let unify_min ctx el =
  243. try unify_min_raise ctx el
  244. with Error (Unify l,p) ->
  245. if not ctx.untyped then display_error ctx (error_msg (Unify l)) p;
  246. (List.hd el).etype
  247. let unify_min_for_type_source ctx el src =
  248. match src with
  249. | Some WithType.ImplicitReturn when List.exists (fun e -> ExtType.is_void (follow e.etype)) el ->
  250. ctx.com.basic.tvoid
  251. | _ ->
  252. unify_min ctx el
  253. let rec type_ident_raise ctx i p mode with_type =
  254. let is_set = match mode with MSet _ -> true | _ -> false in
  255. match i with
  256. | "true" ->
  257. if mode = MGet then
  258. AKExpr (mk (TConst (TBool true)) ctx.t.tbool p)
  259. else
  260. AKNo i
  261. | "false" ->
  262. if mode = MGet then
  263. AKExpr (mk (TConst (TBool false)) ctx.t.tbool p)
  264. else
  265. AKNo i
  266. | "this" ->
  267. if is_set then add_class_field_flag ctx.curfield CfModifiesThis;
  268. (match mode, ctx.curclass.cl_kind with
  269. | MSet _, KAbstractImpl _ ->
  270. if not (assign_to_this_is_allowed ctx) then
  271. error "Abstract 'this' value can only be modified inside an inline function" p;
  272. AKExpr (get_this ctx p)
  273. | (MCall _, KAbstractImpl _) | (MGet, _)-> AKExpr(get_this ctx p)
  274. | _ -> AKNo i)
  275. | "super" ->
  276. let t = (match ctx.curclass.cl_super with
  277. | None -> error "Current class does not have a superclass" p
  278. | Some (c,params) -> TInst(c,params)
  279. ) in
  280. (match ctx.curfun with
  281. | FunMember | FunConstructor -> ()
  282. | FunMemberAbstract -> error "Cannot access super inside an abstract function" p
  283. | FunStatic -> error "Cannot access super inside a static function" p;
  284. | FunMemberClassLocal | FunMemberAbstractLocal -> error "Cannot access super inside a local function" p);
  285. AKExpr (mk (TConst TSuper) t p)
  286. | "null" ->
  287. if mode = MGet then
  288. AKExpr (null (spawn_monomorph ctx p) p)
  289. else
  290. AKNo i
  291. | _ ->
  292. try
  293. let v = PMap.find i ctx.locals in
  294. (match v.v_extra with
  295. | Some ve ->
  296. let (params,e) = (ve.v_params,ve.v_expr) in
  297. let t = apply_params params (Monomorph.spawn_constrained_monos (fun t -> t) params) v.v_type in
  298. (match e with
  299. | Some ({ eexpr = TFunction f } as e) when ctx.com.display.dms_inline ->
  300. begin match mode with
  301. | MSet _ -> error "Cannot set inline closure" p
  302. | MGet -> error "Cannot create closure on inline closure" p
  303. | MCall _ ->
  304. (* create a fake class with a fake field to emulate inlining *)
  305. let c = mk_class ctx.m.curmod (["local"],v.v_name) e.epos null_pos in
  306. let cf = { (mk_field v.v_name v.v_type e.epos null_pos) with cf_params = params; cf_expr = Some e; cf_kind = Method MethInline } in
  307. add_class_flag c CExtern;
  308. c.cl_fields <- PMap.add cf.cf_name cf PMap.empty;
  309. let e = mk (TConst TNull) (TInst (c,[])) p in
  310. AKField (FieldAccess.create e cf (FHInstance(c,[])) true p)
  311. end
  312. | _ ->
  313. AKExpr (mk (TLocal v) t p))
  314. | _ ->
  315. AKExpr (mk (TLocal v) v.v_type p))
  316. with Not_found -> try
  317. (* member variable lookup *)
  318. if ctx.curfun = FunStatic then raise Not_found;
  319. let c , t , f = class_field ctx ctx.curclass (List.map snd ctx.curclass.cl_params) i p in
  320. field_access ctx mode f (match c with None -> FHAnon | Some (c,tl) -> FHInstance (c,tl)) (get_this ctx p) p
  321. with Not_found -> try
  322. (* static variable lookup *)
  323. let f = PMap.find i ctx.curclass.cl_statics in
  324. let is_impl = has_class_field_flag f CfImpl in
  325. let is_enum = has_class_field_flag f CfEnum in
  326. if is_impl && not (has_class_field_flag ctx.curfield CfImpl) && not is_enum then
  327. error (Printf.sprintf "Cannot access non-static field %s from static method" f.cf_name) p;
  328. let e,fa = match ctx.curclass.cl_kind with
  329. | KAbstractImpl a when is_impl && not is_enum ->
  330. let tl = List.map snd a.a_params in
  331. let e = get_this ctx p in
  332. let e = {e with etype = TAbstract(a,tl)} in
  333. e,FHAbstract(a,tl,ctx.curclass)
  334. | _ ->
  335. let e = type_type ctx ctx.curclass.cl_path p in
  336. e,FHStatic ctx.curclass
  337. in
  338. field_access ctx mode f fa e p
  339. with Not_found -> try
  340. (* module-level statics *)
  341. (match ctx.m.curmod.m_statics with
  342. | None -> raise Not_found
  343. | Some c ->
  344. let f = PMap.find i c.cl_statics in
  345. let e = type_module_type ctx (TClassDecl c) None p in
  346. field_access ctx mode f (FHStatic c) e p
  347. )
  348. with Not_found -> try
  349. let wrap e = if is_set then
  350. AKNo i
  351. else
  352. AKExpr e
  353. in
  354. (* lookup imported enums *)
  355. let rec loop l =
  356. match l with
  357. | [] -> raise Not_found
  358. | (t,pt) :: l ->
  359. match t with
  360. | TAbstractDecl ({a_impl = Some c} as a) when a.a_enum ->
  361. begin try
  362. let cf = PMap.find i c.cl_statics in
  363. if not (has_class_field_flag cf CfEnum) then
  364. loop l
  365. else begin
  366. let et = type_module_type ctx (TClassDecl c) None p in
  367. let inline = match cf.cf_kind with
  368. | Var {v_read = AccInline} -> true
  369. | _ -> false
  370. in
  371. let fa = FieldAccess.create et cf (FHAbstract(a,List.map snd a.a_params,c)) inline p in
  372. ImportHandling.mark_import_position ctx pt;
  373. AKField fa
  374. end
  375. with Not_found ->
  376. loop l
  377. end
  378. | TClassDecl _ | TAbstractDecl _ ->
  379. loop l
  380. | TTypeDecl t ->
  381. (match follow t.t_type with
  382. | TEnum (e,_) -> loop ((TEnumDecl e,pt) :: l)
  383. | TAbstract (a,_) when a.a_enum -> loop ((TAbstractDecl a,pt) :: l)
  384. | _ -> loop l)
  385. | TEnumDecl e ->
  386. try
  387. let ef = PMap.find i e.e_constrs in
  388. let et = type_module_type ctx t None p in
  389. ImportHandling.mark_import_position ctx pt;
  390. wrap (mk (TField (et,FEnum (e,ef))) (enum_field_type ctx e ef p) p)
  391. with
  392. Not_found -> loop l
  393. in
  394. (try loop (List.rev_map (fun t -> t,null_pos) ctx.m.curmod.m_types) with Not_found -> loop ctx.m.module_types)
  395. with Not_found ->
  396. (* lookup imported globals *)
  397. let t, name, pi = PMap.find i ctx.m.module_globals in
  398. ImportHandling.mark_import_position ctx pi;
  399. let e = type_module_type ctx t None p in
  400. type_field_default_cfg ctx e name p mode with_type
  401. and type_ident ctx i p mode with_type =
  402. try
  403. type_ident_raise ctx i p mode with_type
  404. with Not_found -> try
  405. (* lookup type *)
  406. if is_lower_ident i p then raise Not_found;
  407. let e = (try type_type ctx ([],i) p with Error (Module_not_found ([],name),_) when name = i -> raise Not_found) in
  408. AKExpr e
  409. with Not_found ->
  410. let resolved_to_type_parameter = ref false in
  411. try
  412. let t = List.find (fun (i2,_) -> i2 = i) ctx.type_params in
  413. resolved_to_type_parameter := true;
  414. let c = match follow (snd t) with TInst(c,_) -> c | _ -> die "" __LOC__ in
  415. if TypeloadCheck.is_generic_parameter ctx c && Meta.has Meta.Const c.cl_meta then begin
  416. let e = type_module_type ctx (TClassDecl c) None p in
  417. AKExpr {e with etype = (snd t)}
  418. end else
  419. raise Not_found
  420. with Not_found ->
  421. if ctx.untyped then begin
  422. if i = "__this__" then
  423. AKExpr (mk (TConst TThis) ctx.tthis p)
  424. else
  425. let t = mk_mono() in
  426. AKExpr ((mk (TIdent i)) t p)
  427. end else begin
  428. if ctx.curfun = FunStatic && PMap.mem i ctx.curclass.cl_fields then error ("Cannot access " ^ i ^ " in static function") p;
  429. if !resolved_to_type_parameter then begin
  430. display_error ctx ("Only @:const type parameters on @:generic classes can be used as value") p;
  431. AKExpr (mk (TConst TNull) t_dynamic p)
  432. end else begin
  433. let err = Unknown_ident i in
  434. if ctx.in_display then begin
  435. raise (Error (err,p))
  436. end;
  437. match ctx.com.display.dms_kind with
  438. | DMNone ->
  439. raise (Error(err,p))
  440. | DMDiagnostics _ ->
  441. DisplayToplevel.handle_unresolved_identifier ctx i p false;
  442. DisplayFields.handle_missing_ident ctx i mode with_type p;
  443. let t = mk_mono() in
  444. AKExpr (mk (TIdent i) t p)
  445. | _ ->
  446. display_error ctx (error_msg err) p;
  447. let t = mk_mono() in
  448. (* Add a fake local for #8751. *)
  449. if !ServerConfig.legacy_completion then
  450. ignore(add_local ctx VGenerated i t p);
  451. AKExpr (mk (TIdent i) t p)
  452. end
  453. end
  454. and handle_efield ctx e p0 mode with_type =
  455. let open TyperDotPath in
  456. let dot_path first pnext =
  457. let name,_,p = first in
  458. try
  459. (* first, try to resolve the first ident in the chain and access its fields.
  460. this doesn't support untyped identifiers yet, because we want to check fully-qualified
  461. paths first (even in an untyped block) *)
  462. field_chain ctx pnext (type_ident_raise ctx name p MGet WithType.value)
  463. with Not_found ->
  464. (* first ident couldn't be resolved, it's probably a fully qualified path - resolve it *)
  465. let path = (first :: pnext) in
  466. try
  467. resolve_dot_path ctx path mode with_type
  468. with Not_found ->
  469. (* dot-path resolution failed, it could be an untyped field access that happens to look like a dot-path, e.g. `untyped __global__.String` *)
  470. try
  471. (* TODO: we don't really want to do full type_ident again, just the second part of it *)
  472. field_chain ctx pnext (type_ident ctx name p MGet WithType.value)
  473. with Error (Unknown_ident _,p2) as e when p = p2 ->
  474. try
  475. (* try raising a more sensible error if there was an uppercase-first (module name) part *)
  476. begin
  477. (* TODO: we should pass the actual resolution error from resolve_dot_path instead of Not_found *)
  478. let rec loop pack_acc first_uppercase path =
  479. match path with
  480. | (name,PLowercase,_) :: rest ->
  481. (match first_uppercase with
  482. | None -> loop (name :: pack_acc) None rest
  483. | Some (n,p) -> List.rev pack_acc, n, None, p)
  484. | (name,PUppercase,p) :: rest ->
  485. (match first_uppercase with
  486. | None -> loop pack_acc (Some (name,p)) rest
  487. | Some (n,_) -> List.rev pack_acc, n, Some name, p)
  488. | [] ->
  489. (match first_uppercase with
  490. | None -> raise Not_found
  491. | Some (n,p) -> List.rev pack_acc, n, None, p)
  492. in
  493. let pack,name,sub,p = loop [] None path in
  494. let mpath = (pack,name) in
  495. if Hashtbl.mem ctx.g.modules mpath then
  496. let tname = Option.default name sub in
  497. raise (Error (Type_not_found (mpath,tname,Not_defined),p))
  498. else
  499. raise (Error (Module_not_found mpath,p))
  500. end
  501. with Not_found ->
  502. (* if there was no module name part, last guess is that we're trying to get package completion *)
  503. if ctx.in_display then begin
  504. let sl = List.map (fun (n,_,_) -> n) path in
  505. if is_legacy_completion ctx.com then
  506. raise (Parser.TypePath (sl,None,false,p))
  507. else
  508. DisplayToplevel.collect_and_raise ctx TKType WithType.no_value (CRToplevel None) (String.concat "." sl,p0) p0
  509. end;
  510. raise e
  511. in
  512. (* loop through the given EField expression to figure out whether it's a dot-path that we have to resolve,
  513. or a simple field access chain *)
  514. let rec loop dot_path_acc (e,p) =
  515. match e with
  516. | EField (e,s) ->
  517. (* field access - accumulate and check further *)
  518. loop ((mk_dot_path_part s p) :: dot_path_acc) e
  519. | EConst (Ident i) ->
  520. (* it's a dot-path, so it might be either fully-qualified access (pack.Class.field)
  521. or normal field access of a local/global/field identifier, proceed figuring this out *)
  522. dot_path (mk_dot_path_part i p) dot_path_acc
  523. | _ ->
  524. (* non-ident expr occured: definitely NOT a fully-qualified access,
  525. resolve the field chain against this expression *)
  526. let e = type_access ctx e p MGet WithType.value in
  527. field_chain ctx dot_path_acc e
  528. in
  529. loop [] (e,p0) mode with_type
  530. and type_access ctx e p mode with_type =
  531. match e with
  532. | EConst (Ident s) ->
  533. type_ident ctx s p mode with_type
  534. | EField (e1,"new") ->
  535. let e1 = type_expr ctx e1 WithType.value in
  536. begin match e1.eexpr with
  537. | TTypeExpr (TClassDecl c) ->
  538. begin match mode with
  539. | MSet _ -> error "Cannot set constructor" p;
  540. | MCall _ -> error ("Cannot call constructor like this, use 'new " ^ (s_type_path c.cl_path) ^ "()' instead") p;
  541. | MGet -> ()
  542. end;
  543. let monos = Monomorph.spawn_constrained_monos (fun t -> t) (match c.cl_kind with KAbstractImpl a -> a.a_params | _ -> c.cl_params) in
  544. let fa = FieldAccess.get_constructor_access c monos p in
  545. let cf = fa.fa_field in
  546. no_abstract_constructor c p;
  547. check_constructor_access ctx c cf p;
  548. let args = match follow (FieldAccess.get_map_function fa cf.cf_type) with TFun(args,ret) -> args | _ -> die "" __LOC__ in
  549. let vl = List.map (fun (n,_,t) -> alloc_var VGenerated n t c.cl_pos) args in
  550. let vexpr v = mk (TLocal v) v.v_type p in
  551. let el = List.map vexpr vl in
  552. let ec,t = match c.cl_kind with
  553. | KAbstractImpl a ->
  554. let t = TAbstract(a,monos) in
  555. (new call_dispatcher ctx (MCall []) WithType.value p)#field_call fa el [],t
  556. | _ ->
  557. let t = TInst(c,monos) in
  558. mk (TNew(c,monos,el)) t p,t
  559. in
  560. AKExpr(mk (TFunction {
  561. tf_args = List.map (fun v -> v,None) vl;
  562. tf_type = t;
  563. tf_expr = mk (TReturn (Some ec)) t p;
  564. }) (TFun ((List.map (fun v -> v.v_name,false,v.v_type) vl),t)) p)
  565. | _ -> error "Binding new is only allowed on class types" p
  566. end;
  567. | EField _ ->
  568. handle_efield ctx e p mode with_type
  569. | EArray (e1,e2) ->
  570. type_array_access ctx e1 e2 p mode
  571. | EDisplay (e,dk) ->
  572. AKExpr (TyperDisplay.handle_edisplay ctx e dk mode WithType.value)
  573. | _ ->
  574. AKExpr (type_expr ~mode ctx (e,p) WithType.value)
  575. and type_array_access ctx e1 e2 p mode =
  576. let e1 = type_expr ctx e1 WithType.value in
  577. let e2 = type_expr ctx e2 WithType.value in
  578. Calls.array_access ctx e1 e2 mode p
  579. and type_vars ctx vl p =
  580. let vl = List.map (fun ev ->
  581. let n = fst ev.ev_name
  582. and pv = snd ev.ev_name in
  583. DeprecationCheck.check_is ctx.com n ev.ev_meta pv;
  584. try
  585. let t = Typeload.load_type_hint ctx p ev.ev_type in
  586. let e = (match ev.ev_expr with
  587. | None -> None
  588. | Some e ->
  589. let e = type_expr ctx e (WithType.with_type t) in
  590. let e = AbstractCast.cast_or_unify ctx t e p in
  591. Some e
  592. ) in
  593. let v = add_local_with_origin ctx TVOLocalVariable n t pv in
  594. v.v_meta <- ev.ev_meta;
  595. if ev.ev_final then add_var_flag v VFinal;
  596. if ctx.in_display && DisplayPosition.display_position#enclosed_in pv then
  597. DisplayEmitter.display_variable ctx v pv;
  598. v,e
  599. with
  600. Error (e,p) ->
  601. check_error ctx e p;
  602. add_local ctx VGenerated n t_dynamic pv, None (* TODO: What to do with this... *)
  603. ) vl in
  604. delay ctx PTypeField (fun() ->
  605. List.iter
  606. (fun (v,_) ->
  607. if ExtType.is_void (follow v.v_type) then
  608. error "Variables of type Void are not allowed" v.v_pos
  609. )
  610. vl
  611. );
  612. match vl with
  613. | [v,eo] ->
  614. mk (TVar (v,eo)) ctx.t.tvoid p
  615. | _ ->
  616. let e = mk (TBlock (List.map (fun (v,e) -> (mk (TVar (v,e)) ctx.t.tvoid p)) vl)) ctx.t.tvoid p in
  617. mk (TMeta((Meta.MergeBlock,[],p), e)) e.etype e.epos
  618. and format_string ctx s p =
  619. let e = ref None in
  620. let pmin = ref p.pmin in
  621. let min = ref (p.pmin + 1) in
  622. let add_expr (enext,p) len =
  623. min := !min + len;
  624. let enext = if ctx.in_display && DisplayPosition.display_position#enclosed_in p then
  625. Display.ExprPreprocessing.process_expr ctx.com (enext,p)
  626. else
  627. enext,p
  628. in
  629. match !e with
  630. | None -> e := Some enext
  631. | Some prev ->
  632. e := Some (EBinop (OpAdd,prev,enext),punion (pos prev) p)
  633. in
  634. let add enext len =
  635. let p = { p with pmin = !min; pmax = !min + len } in
  636. add_expr (enext,p) len
  637. in
  638. let add_sub start pos =
  639. let len = pos - start in
  640. if len > 0 || !e = None then add (EConst (String (String.sub s start len,SDoubleQuotes))) len
  641. in
  642. let len = String.length s in
  643. let rec parse start pos =
  644. if pos = len then add_sub start pos else
  645. let c = String.unsafe_get s pos in
  646. let pos = pos + 1 in
  647. if c = '\'' then begin
  648. incr pmin;
  649. incr min;
  650. end;
  651. if c <> '$' || pos = len then parse start pos else
  652. match String.unsafe_get s pos with
  653. | '$' ->
  654. (* double $ *)
  655. add_sub start pos;
  656. parse (pos + 1) (pos + 1)
  657. | '{' ->
  658. parse_group start pos '{' '}' "brace"
  659. | 'a'..'z' | 'A'..'Z' | '_' ->
  660. add_sub start (pos - 1);
  661. incr min;
  662. let rec loop i =
  663. if i = len then i else
  664. let c = String.unsafe_get s i in
  665. match c with
  666. | 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' -> loop (i+1)
  667. | _ -> i
  668. in
  669. let iend = loop (pos + 1) in
  670. let len = iend - pos in
  671. add (EConst (Ident (String.sub s pos len))) len;
  672. parse (pos + len) (pos + len)
  673. | _ ->
  674. (* keep as-it *)
  675. parse start pos
  676. and parse_group start pos gopen gclose gname =
  677. add_sub start (pos - 1);
  678. let rec loop groups i =
  679. if i = len then
  680. match groups with
  681. | [] -> die "" __LOC__
  682. | g :: _ -> error ("Unclosed " ^ gname) { p with pmin = !pmin + g + 1; pmax = !pmin + g + 2 }
  683. else
  684. let c = String.unsafe_get s i in
  685. if c = gopen then
  686. loop (i :: groups) (i + 1)
  687. else if c = gclose then begin
  688. let groups = List.tl groups in
  689. if groups = [] then i else loop groups (i + 1)
  690. end else
  691. loop groups (i + 1)
  692. in
  693. let send = loop [pos] (pos + 1) in
  694. let slen = send - pos - 1 in
  695. let scode = String.sub s (pos + 1) slen in
  696. min := !min + 2;
  697. begin
  698. let e =
  699. let ep = { p with pmin = !pmin + pos + 2; pmax = !pmin + send + 1 } in
  700. let error msg pos =
  701. if Lexer.string_is_whitespace scode then error "Expression cannot be empty" ep
  702. else error msg pos
  703. in
  704. match ParserEntry.parse_expr_string ctx.com.defines scode ep error true with
  705. | ParseSuccess(data,_,_) -> data
  706. | ParseError(_,(msg,p),_) -> error (Parser.error_msg msg) p
  707. in
  708. add_expr e slen
  709. end;
  710. min := !min + 1;
  711. parse (send + 1) (send + 1)
  712. in
  713. parse 0 0;
  714. match !e with
  715. | None -> die "" __LOC__
  716. | Some e -> e
  717. and type_block ctx el with_type p =
  718. let merge acc e = match e.eexpr with
  719. | TMeta((Meta.MergeBlock,_,_), {eexpr = TBlock el}) ->
  720. List.rev el @ acc
  721. | _ ->
  722. e :: acc
  723. in
  724. let rec loop acc = function
  725. | [] -> List.rev acc
  726. | e :: l ->
  727. let acc = try merge acc (type_expr ctx e (if l = [] then with_type else WithType.no_value)) with Error (e,p) -> check_error ctx e p; acc in
  728. loop acc l
  729. in
  730. let l = loop [] el in
  731. let rec loop = function
  732. | [] -> ctx.t.tvoid
  733. | [e] -> e.etype
  734. | _ :: l -> loop l
  735. in
  736. mk (TBlock l) (loop l) p
  737. and type_object_decl ctx fl with_type p =
  738. let dynamic_parameter = ref None in
  739. let a = (match with_type with
  740. | WithType.WithType(t,_) ->
  741. let rec loop seen t =
  742. match follow t with
  743. | TAnon a -> ODKWithStructure a
  744. | TAbstract (a,pl) as t
  745. when not (Meta.has Meta.CoreType a.a_meta)
  746. && not (List.exists (fun t' -> shallow_eq t t') seen) ->
  747. let froms = get_abstract_froms a pl
  748. and fold = fun acc t' -> match loop (t :: seen) t' with ODKPlain -> acc | t -> t :: acc in
  749. (match List.fold_left fold [] froms with
  750. | [t] -> t
  751. | _ -> ODKPlain)
  752. | TDynamic t when (follow t != t_dynamic) ->
  753. dynamic_parameter := Some t;
  754. ODKWithStructure {
  755. a_status = ref Closed;
  756. a_fields = PMap.empty;
  757. }
  758. | TInst(c,tl) when Meta.has Meta.StructInit c.cl_meta ->
  759. ODKWithClass(c,tl)
  760. | _ ->
  761. ODKPlain
  762. in
  763. loop [] t
  764. | _ ->
  765. ODKPlain
  766. ) in
  767. let type_fields field_map =
  768. let fields = ref PMap.empty in
  769. let extra_fields = ref [] in
  770. let fl = List.map (fun ((n,pn,qs),e) ->
  771. let is_valid = Lexer.is_valid_identifier n in
  772. if PMap.mem n !fields then error ("Duplicate field in object declaration : " ^ n) p;
  773. let is_final = ref false in
  774. let e = try
  775. let t = match !dynamic_parameter with
  776. | Some t -> t
  777. | None ->
  778. let cf = PMap.find n field_map in
  779. if (has_class_field_flag cf CfFinal) then is_final := true;
  780. if ctx.in_display && DisplayPosition.display_position#enclosed_in pn then DisplayEmitter.display_field ctx Unknown CFSMember cf pn;
  781. cf.cf_type
  782. in
  783. let e = type_expr ctx e (WithType.with_structure_field t n) in
  784. let e = AbstractCast.cast_or_unify ctx t e e.epos in
  785. let e = if is_null t && not (is_null e.etype) then mk (TCast(e,None)) (ctx.t.tnull e.etype) e.epos else e in
  786. (try type_eq EqStrict e.etype t; e with Unify_error _ -> mk (TCast (e,None)) t e.epos)
  787. with Not_found ->
  788. if is_valid then
  789. extra_fields := n :: !extra_fields;
  790. type_expr ctx e WithType.value
  791. in
  792. if is_valid then begin
  793. if starts_with n '$' then error "Field names starting with a dollar are not allowed" p;
  794. let cf = mk_field n e.etype (punion pn e.epos) pn in
  795. if !is_final then add_class_field_flag cf CfFinal;
  796. fields := PMap.add n cf !fields;
  797. end;
  798. ((n,pn,qs),e)
  799. ) fl in
  800. let t = mk_anon ~fields:!fields (ref Const) in
  801. if not ctx.untyped then begin
  802. (match PMap.foldi (fun n cf acc -> if not (Meta.has Meta.Optional cf.cf_meta) && not (PMap.mem n !fields) then n :: acc else acc) field_map [] with
  803. | [] -> ()
  804. | [n] -> raise_or_display ctx [Unify_custom ("Object requires field " ^ n)] p
  805. | nl -> raise_or_display ctx [Unify_custom ("Object requires fields: " ^ (String.concat ", " nl))] p);
  806. (match !extra_fields with
  807. | [] -> ()
  808. | _ -> raise_or_display ctx (List.map (fun n -> has_extra_field t n) !extra_fields) p);
  809. end;
  810. t, fl
  811. in
  812. let type_plain_fields () =
  813. let rec loop (l,acc) ((f,pf,qs),e) =
  814. let is_valid = Lexer.is_valid_identifier f in
  815. if PMap.mem f acc then error ("Duplicate field in object declaration : " ^ f) p;
  816. let e = type_expr ctx e (WithType.named_structure_field f) in
  817. (match follow e.etype with TAbstract({a_path=[],"Void"},_) -> error "Fields of type Void are not allowed in structures" e.epos | _ -> ());
  818. let cf = mk_field f e.etype (punion pf e.epos) pf in
  819. if ctx.in_display && DisplayPosition.display_position#enclosed_in pf then DisplayEmitter.display_field ctx Unknown CFSMember cf pf;
  820. (((f,pf,qs),e) :: l, if is_valid then begin
  821. if starts_with f '$' then error "Field names starting with a dollar are not allowed" p;
  822. PMap.add f cf acc
  823. end else acc)
  824. in
  825. let fields , types = List.fold_left loop ([],PMap.empty) fl in
  826. let x = ref Const in
  827. ctx.opened <- x :: ctx.opened;
  828. mk (TObjectDecl (List.rev fields)) (mk_anon ~fields:types x) p
  829. in
  830. (match a with
  831. | ODKPlain -> type_plain_fields()
  832. | ODKWithStructure a when PMap.is_empty a.a_fields && !dynamic_parameter = None -> type_plain_fields()
  833. | ODKWithStructure a ->
  834. let t, fl = type_fields a.a_fields in
  835. mk (TObjectDecl fl) t p
  836. | ODKWithClass (c,tl) ->
  837. let fa = FieldAccess.get_constructor_access c tl p in
  838. let ctor = fa.fa_field in
  839. let args = match follow (FieldAccess.get_map_function fa ctor.cf_type) with
  840. | TFun(args,_) -> args
  841. | _ -> die "" __LOC__
  842. in
  843. let fields = List.fold_left (fun acc (n,opt,t) ->
  844. let f = mk_field n t ctor.cf_pos ctor.cf_name_pos in
  845. if opt then f.cf_meta <- [(Meta.Optional,[],ctor.cf_pos)];
  846. PMap.add n f acc
  847. ) PMap.empty args in
  848. let t,fl = type_fields fields in
  849. let evars,fl,_ = List.fold_left (fun (evars,elocs,had_side_effect) (s,e) ->
  850. begin match e.eexpr with
  851. | TConst _ | TTypeExpr _ | TFunction _ ->
  852. evars,(s,e) :: elocs,had_side_effect
  853. | _ ->
  854. if had_side_effect then begin
  855. let v = gen_local ctx e.etype e.epos in
  856. let ev = mk (TVar(v,Some e)) e.etype e.epos in
  857. let eloc = mk (TLocal v) v.v_type e.epos in
  858. (ev :: evars),((s,eloc) :: elocs),had_side_effect
  859. end else
  860. evars,(s,e) :: elocs,OptimizerTexpr.has_side_effect e
  861. end
  862. ) ([],[],false) (List.rev fl) in
  863. let el = List.map (fun (n,_,t) ->
  864. try Expr.field_assoc n fl
  865. with Not_found ->
  866. try
  867. match ctor.cf_expr with
  868. | Some { eexpr = TFunction fn } ->
  869. Option.get (snd (List.find (fun (v,e) -> n = v.v_name && Option.is_some e) fn.tf_args))
  870. | _ ->
  871. raise Not_found
  872. with Not_found | Option.No_value ->
  873. let t =
  874. if type_has_meta (Abstract.follow_with_abstracts_without_null t) Meta.NotNull then ctx.t.tnull t
  875. else t
  876. in
  877. mk (TConst TNull) t p
  878. ) args in
  879. let e = mk (TNew(c,tl,el)) (TInst(c,tl)) p in
  880. mk (TBlock (List.rev (e :: (List.rev evars)))) e.etype e.epos
  881. )
  882. and type_new ctx path el with_type force_inline p =
  883. let path =
  884. if snd path <> null_pos then
  885. path
  886. (*
  887. Since macros don't have placed_type_path structure on Haxe side any ENew will have null_pos in `path`.
  888. Try to calculate a better pos.
  889. *)
  890. else begin
  891. match el with
  892. | (_,p1) :: _ when p1.pfile = p.pfile && p.pmin < p1.pmin ->
  893. let pmin = p.pmin + (String.length "new ")
  894. and pmax = p1.pmin - 2 (* Additional "1" for an opening bracket *)
  895. in
  896. fst path, { p with
  897. pmin = if pmin < pmax then pmin else p.pmin;
  898. pmax = pmax;
  899. }
  900. | _ -> fst path, p
  901. end
  902. in
  903. let unify_constructor_call c fa =
  904. try
  905. let fcc = unify_field_call ctx fa [] el p fa.fa_inline in
  906. check_constructor_access ctx c fcc.fc_field p;
  907. fcc
  908. with Error (e,p) ->
  909. error (error_msg e) p;
  910. in
  911. let t = if (fst path).tparams <> [] then begin
  912. try
  913. Typeload.load_instance ctx path false
  914. with Error _ as exc when ctx.com.display.dms_display ->
  915. (* If we fail for some reason, process the arguments in case we want to display them (#7650). *)
  916. List.iter (fun e -> ignore(type_expr ctx e WithType.value)) el;
  917. raise exc
  918. end else try
  919. ctx.call_argument_stack <- el :: ctx.call_argument_stack;
  920. let t = Typeload.load_instance ctx path true in
  921. let t_follow = follow t in
  922. ctx.call_argument_stack <- List.tl ctx.call_argument_stack;
  923. (* Try to properly build @:generic classes here (issue #2016) *)
  924. begin match t_follow with
  925. | TInst({cl_kind = KGeneric } as c,tl) -> follow (Generic.build_generic ctx c p tl)
  926. | _ -> t
  927. end
  928. with
  929. | Generic.Generic_Exception _ ->
  930. (* Try to infer generic parameters from the argument list (issue #2044) *)
  931. begin match resolve_typedef (Typeload.load_type_def ctx p (fst path)) with
  932. | TClassDecl ({cl_constructor = Some cf} as c) ->
  933. let monos = Monomorph.spawn_constrained_monos (fun t -> t) c.cl_params in
  934. let fa = FieldAccess.get_constructor_access c monos p in
  935. no_abstract_constructor c p;
  936. ignore (unify_constructor_call c fa);
  937. begin try
  938. Generic.build_generic ctx c p monos
  939. with Generic.Generic_Exception _ as exc ->
  940. (* If we have an expected type, just use that (issue #3804) *)
  941. begin match with_type with
  942. | WithType.WithType(t,_) ->
  943. begin match follow t with
  944. | TMono _ -> raise exc
  945. | t -> t
  946. end
  947. | _ ->
  948. raise exc
  949. end
  950. end
  951. | mt ->
  952. error ((s_type_path (t_infos mt).mt_path) ^ " cannot be constructed") p
  953. end
  954. | Error _ as exc when ctx.com.display.dms_display ->
  955. List.iter (fun e -> ignore(type_expr ctx e WithType.value)) el;
  956. raise exc
  957. in
  958. DisplayEmitter.check_display_type ctx t path;
  959. let t = follow t in
  960. let build_constructor_call ao c tl =
  961. let fa = FieldAccess.get_constructor_access c tl p in
  962. let fa = if force_inline then {fa with fa_inline = true} else fa in
  963. let cf = fa.fa_field in
  964. no_abstract_constructor c p;
  965. begin match cf.cf_kind with
  966. | Var { v_read = AccRequire (r,msg) } -> (match msg with Some msg -> error msg p | None -> error_require r p)
  967. | _ -> ()
  968. end;
  969. unify_constructor_call c fa
  970. in
  971. try begin match Abstract.follow_with_forward_ctor t with
  972. | TInst ({cl_kind = KTypeParameter tl} as c,params) ->
  973. if not (TypeloadCheck.is_generic_parameter ctx c) then error "Only generic type parameters can be constructed" p;
  974. begin match get_constructible_constraint ctx tl p with
  975. | None ->
  976. raise_error (No_constructor (TClassDecl c)) p
  977. | Some(tl,tr) ->
  978. let el,_ = unify_call_args ctx el tl tr p false false in
  979. mk (TNew (c,params,el)) t p
  980. end
  981. | TAbstract({a_impl = Some c} as a,tl) when not (Meta.has Meta.MultiType a.a_meta) ->
  982. let fcc = build_constructor_call (Some a) c tl in
  983. { (fcc.fc_data()) with etype = t }
  984. | TInst (c,params) | TAbstract({a_impl = Some c},params) ->
  985. let fcc = build_constructor_call None c params in
  986. let el = List.map fst fcc.fc_args in
  987. mk (TNew (c,params,el)) t p
  988. | _ ->
  989. error (s_type (print_context()) t ^ " cannot be constructed") p
  990. end with Error(No_constructor _ as err,p) when ctx.com.display.dms_kind <> DMNone ->
  991. display_error ctx (error_msg err) p;
  992. Diagnostics.secure_generated_code ctx (mk (TConst TNull) t p)
  993. and type_try ctx e1 catches with_type p =
  994. let e1 = type_expr ctx (Expr.ensure_block e1) with_type in
  995. let rec check_unreachable cases t p = match cases with
  996. | (v,e) :: cases ->
  997. let unreachable () =
  998. display_error ctx "This block is unreachable" p;
  999. let st = s_type (print_context()) in
  1000. display_error ctx (Printf.sprintf "%s can be caught to %s, which is handled here" (st t) (st v.v_type)) e.epos
  1001. in
  1002. begin try
  1003. begin match follow t,follow v.v_type with
  1004. | _, TDynamic _
  1005. | _, TInst({ cl_path = ["haxe"],"Error"},_) ->
  1006. unreachable()
  1007. | _, TInst({ cl_path = path },_) when path = ctx.com.config.pf_exceptions.ec_wildcard_catch ->
  1008. unreachable()
  1009. | TDynamic _,_ ->
  1010. ()
  1011. | _ ->
  1012. Type.unify t v.v_type;
  1013. unreachable()
  1014. end
  1015. with Unify_error _ ->
  1016. check_unreachable cases t p
  1017. end
  1018. | [] ->
  1019. ()
  1020. in
  1021. let check_catch_type_params params p =
  1022. List.iter (fun pt ->
  1023. if Abstract.follow_with_abstracts pt != t_dynamic then error "Catch class parameter must be Dynamic" p;
  1024. ) params
  1025. in
  1026. let catches,el = List.fold_left (fun (acc1,acc2) ((v,pv),t,e_ast,pc) ->
  1027. let th = Option.default (CTPath { tpackage = ["haxe"]; tname = "Exception"; tsub = None; tparams = [] },null_pos) t in
  1028. let t = Typeload.load_complex_type ctx true th in
  1029. let rec loop t = match follow t with
  1030. | TInst ({ cl_kind = KTypeParameter _} as c,_) when not (TypeloadCheck.is_generic_parameter ctx c) ->
  1031. error "Cannot catch non-generic type parameter" p
  1032. | TInst (_,params) | TEnum (_,params) ->
  1033. check_catch_type_params params (snd th);
  1034. t
  1035. | TAbstract(a,params) when Meta.has Meta.RuntimeValue a.a_meta ->
  1036. check_catch_type_params params (snd th);
  1037. t
  1038. | TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
  1039. loop (Abstract.get_underlying_type a tl)
  1040. | TDynamic _ -> t
  1041. | _ -> error "Catch type must be a class, an enum or Dynamic" (pos e_ast)
  1042. in
  1043. let t2 = loop t in
  1044. check_unreachable acc1 t2 (pos e_ast);
  1045. let locals = save_locals ctx in
  1046. let v = add_local_with_origin ctx TVOCatchVariable v t pv in
  1047. if ctx.is_display_file && DisplayPosition.display_position#enclosed_in pv then
  1048. DisplayEmitter.display_variable ctx v pv;
  1049. let e = type_expr ctx e_ast with_type in
  1050. (* If the catch position is the display position it means we get completion on the catch keyword or some
  1051. punctuation. Otherwise we wouldn't reach this point. *)
  1052. if ctx.is_display_file && DisplayPosition.display_position#enclosed_in pc then ignore(TyperDisplay.display_expr ctx e_ast e DKMarked MGet with_type pc);
  1053. v.v_type <- t2;
  1054. locals();
  1055. ((v,e) :: acc1),(e :: acc2)
  1056. ) ([],[e1]) catches in
  1057. let e1,catches,t = match with_type with
  1058. | WithType.NoValue -> e1,catches,ctx.t.tvoid
  1059. | WithType.Value _ -> e1,catches,unify_min ctx el
  1060. | WithType.WithType(t,src) when (match follow t with TMono _ -> true | t -> ExtType.is_void t) ->
  1061. e1,catches,unify_min_for_type_source ctx el src
  1062. | WithType.WithType(t,_) ->
  1063. let e1 = AbstractCast.cast_or_unify ctx t e1 e1.epos in
  1064. let catches = List.map (fun (v,e) ->
  1065. v,AbstractCast.cast_or_unify ctx t e e.epos
  1066. ) catches in
  1067. e1,catches,t
  1068. in
  1069. mk (TTry (e1,List.rev catches)) t p
  1070. and type_map_declaration ctx e1 el with_type p =
  1071. let (tkey,tval,has_type) =
  1072. let get_map_params t = match follow t with
  1073. | TAbstract({a_path=["haxe";"ds"],"Map"},[tk;tv]) -> tk,tv,true
  1074. | TInst({cl_path=["haxe";"ds"],"IntMap"},[tv]) -> ctx.t.tint,tv,true
  1075. | TInst({cl_path=["haxe";"ds"],"StringMap"},[tv]) -> ctx.t.tstring,tv,true
  1076. | TInst({cl_path=["haxe";"ds"],("ObjectMap" | "EnumValueMap")},[tk;tv]) -> tk,tv,true
  1077. | _ -> spawn_monomorph ctx p,spawn_monomorph ctx p,false
  1078. in
  1079. match with_type with
  1080. | WithType.WithType(t,_) -> get_map_params t
  1081. | _ -> (spawn_monomorph ctx p,spawn_monomorph ctx p,false)
  1082. in
  1083. let keys = Hashtbl.create 0 in
  1084. let check_key e_key =
  1085. try
  1086. let p = Hashtbl.find keys e_key.eexpr in
  1087. display_error ctx "Duplicate key" e_key.epos;
  1088. error (compl_msg "Previously defined here") p
  1089. with Not_found ->
  1090. begin match e_key.eexpr with
  1091. | TConst _ -> Hashtbl.add keys e_key.eexpr e_key.epos;
  1092. | _ -> ()
  1093. end
  1094. in
  1095. let el = e1 :: el in
  1096. let el_kv = List.map (fun e -> match fst e with
  1097. | EBinop(OpArrow,e1,e2) -> e1,e2
  1098. | EDisplay _ ->
  1099. ignore(type_expr ctx e (WithType.with_type tkey));
  1100. error "Expected a => b" (pos e)
  1101. | _ -> error "Expected a => b" (pos e)
  1102. ) el in
  1103. let el_k,el_v,tkey,tval = if has_type then begin
  1104. let el_k,el_v = List.fold_left (fun (el_k,el_v) (e1,e2) ->
  1105. let e1 = type_expr ctx e1 (WithType.with_type tkey) in
  1106. check_key e1;
  1107. let e1 = AbstractCast.cast_or_unify ctx tkey e1 e1.epos in
  1108. let e2 = type_expr ctx e2 (WithType.with_type tval) in
  1109. let e2 = AbstractCast.cast_or_unify ctx tval e2 e2.epos in
  1110. (e1 :: el_k,e2 :: el_v)
  1111. ) ([],[]) el_kv in
  1112. el_k,el_v,tkey,tval
  1113. end else begin
  1114. let el_k,el_v = List.fold_left (fun (el_k,el_v) (e1,e2) ->
  1115. let e1 = type_expr ctx e1 WithType.value in
  1116. check_key e1;
  1117. let e2 = type_expr ctx e2 WithType.value in
  1118. (e1 :: el_k,e2 :: el_v)
  1119. ) ([],[]) el_kv in
  1120. let unify_min_resume el = try
  1121. unify_min_raise ctx el
  1122. with Error (Unify l,p) when ctx.in_call_args ->
  1123. raise (WithTypeError(Unify l,p))
  1124. in
  1125. let tkey = unify_min_resume el_k in
  1126. let tval = unify_min_resume el_v in
  1127. el_k,el_v,tkey,tval
  1128. end in
  1129. let m = TypeloadModule.load_module ctx (["haxe";"ds"],"Map") null_pos in
  1130. let a,c = match m.m_types with
  1131. | (TAbstractDecl ({a_impl = Some c} as a)) :: _ -> a,c
  1132. | _ -> die "" __LOC__
  1133. in
  1134. let tmap = TAbstract(a,[tkey;tval]) in
  1135. let cf = PMap.find "set" c.cl_statics in
  1136. let v = gen_local ctx tmap p in
  1137. let ev = mk (TLocal v) tmap p in
  1138. let ec = type_module_type ctx (TClassDecl c) None p in
  1139. let ef = mk (TField(ec,FStatic(c,cf))) (tfun [tkey;tval] ctx.t.tvoid) p in
  1140. let el = ev :: List.map2 (fun e1 e2 -> (make_call ctx ef [ev;e1;e2] ctx.com.basic.tvoid p)) el_k el_v in
  1141. let enew = mk (TNew(c,[tkey;tval],[])) tmap p in
  1142. let el = (mk (TVar (v,Some enew)) t_dynamic p) :: (List.rev el) in
  1143. mk (TBlock el) tmap p
  1144. and type_local_function ctx kind f with_type p =
  1145. let name,inline = match kind with FKNamed (name,inline) -> Some name,inline | _ -> None,false in
  1146. let params = TypeloadFunction.type_function_params ctx f (match name with None -> "localfun" | Some (n,_) -> n) p in
  1147. if params <> [] then begin
  1148. if name = None then display_error ctx "Type parameters not supported in unnamed local functions" p;
  1149. if with_type <> WithType.NoValue then error "Type parameters are not supported for rvalue functions" p
  1150. end;
  1151. let v,pname = (match name with
  1152. | None -> None,p
  1153. | Some (v,pn) -> Some v,pn
  1154. ) in
  1155. let old_tp,old_in_loop = ctx.type_params,ctx.in_loop in
  1156. ctx.type_params <- params @ ctx.type_params;
  1157. if not inline then ctx.in_loop <- false;
  1158. let rt = Typeload.load_type_hint ctx p f.f_type in
  1159. let args = List.map (fun ((s,_),opt,_,t,c) ->
  1160. let t = Typeload.load_type_hint ctx p t in
  1161. let t, c = TypeloadFunction.type_function_arg ctx t c opt p in
  1162. s, c, t
  1163. ) f.f_args in
  1164. (match with_type with
  1165. | WithType.WithType(t,_) ->
  1166. let rec loop t =
  1167. (match follow t with
  1168. | TFun (args2,tr) when List.length args2 = List.length args ->
  1169. List.iter2 (fun (_,_,t1) (_,_,t2) ->
  1170. match follow t1 with
  1171. | TMono _ -> unify ctx t2 t1 p
  1172. | _ -> ()
  1173. ) args args2;
  1174. (* unify for top-down inference unless we are expecting Void *)
  1175. begin
  1176. match follow tr,follow rt with
  1177. | TAbstract({a_path = [],"Void"},_),_ when kind <> FKArrow -> ()
  1178. | _,TMono _ -> unify ctx rt tr p
  1179. | _ -> ()
  1180. end
  1181. | TAbstract(a,tl) ->
  1182. loop (Abstract.get_underlying_type a tl)
  1183. | _ -> ())
  1184. in
  1185. loop t
  1186. | WithType.NoValue ->
  1187. if name = None then display_error ctx "Unnamed lvalue functions are not supported" p
  1188. | _ ->
  1189. ());
  1190. let ft = TFun (fun_args args,rt) in
  1191. let v = (match v with
  1192. | None -> None
  1193. | Some v ->
  1194. let v = (add_local_with_origin ctx TVOLocalFunction v ft pname) in
  1195. if params <> [] then v.v_extra <- Some (var_extra params None);
  1196. Some v
  1197. ) in
  1198. let curfun = match ctx.curfun with
  1199. | FunStatic -> FunStatic
  1200. | FunMemberAbstract
  1201. | FunMemberAbstractLocal -> FunMemberAbstractLocal
  1202. | _ -> FunMemberClassLocal
  1203. in
  1204. let fargs = TypeloadFunction.convert_fargs f in
  1205. let e , fargs = TypeloadFunction.type_function ctx args fargs rt curfun f.f_expr ctx.in_display p in
  1206. ctx.type_params <- old_tp;
  1207. ctx.in_loop <- old_in_loop;
  1208. let tf = {
  1209. tf_args = fargs;
  1210. tf_type = rt;
  1211. tf_expr = e;
  1212. } in
  1213. let e = mk (TFunction tf) ft p in
  1214. match v with
  1215. | None -> e
  1216. | Some v ->
  1217. Typeload.generate_args_meta ctx.com None (fun m -> v.v_meta <- m :: v.v_meta) f.f_args;
  1218. let open LocalUsage in
  1219. if params <> [] || inline then v.v_extra <- Some (var_extra params (if inline then Some e else None));
  1220. if ctx.in_display && DisplayPosition.display_position#enclosed_in v.v_pos then
  1221. DisplayEmitter.display_variable ctx v v.v_pos;
  1222. let rec loop = function
  1223. | LocalUsage.Block f | LocalUsage.Loop f | LocalUsage.Function f -> f loop
  1224. | LocalUsage.Use v2 | LocalUsage.Assign v2 when v == v2 -> raise Exit
  1225. | LocalUsage.Use _ | LocalUsage.Assign _ | LocalUsage.Declare _ -> ()
  1226. in
  1227. let is_rec = (try local_usage loop e; false with Exit -> true) in
  1228. let exprs =
  1229. if with_type <> WithType.NoValue && not inline then [mk (TLocal v) v.v_type p]
  1230. else []
  1231. in
  1232. let exprs =
  1233. if is_rec then begin
  1234. if inline then display_error ctx "Inline function cannot be recursive" e.epos;
  1235. (mk (TVar (v,Some (mk (TConst TNull) ft p))) ctx.t.tvoid p) ::
  1236. (mk (TBinop (OpAssign,mk (TLocal v) ft p,e)) ft p) ::
  1237. exprs
  1238. end else if inline && not ctx.com.display.dms_display then
  1239. (mk (TBlock []) ctx.t.tvoid p) :: exprs (* do not add variable since it will be inlined *)
  1240. else
  1241. (mk (TVar (v,Some e)) ctx.t.tvoid p) :: exprs
  1242. in
  1243. match exprs with
  1244. | [e] -> e
  1245. | _ ->
  1246. let block = mk (TBlock exprs) v.v_type p in
  1247. mk (TMeta ((Meta.MergeBlock, [], null_pos), block)) v.v_type p
  1248. and type_array_decl ctx el with_type p =
  1249. let allow_array_dynamic = ref false in
  1250. let tp = (match with_type with
  1251. | WithType.WithType(t,_) ->
  1252. let rec loop seen t =
  1253. (match follow t with
  1254. | TInst ({ cl_path = [],"Array" },[tp]) ->
  1255. (match follow tp with
  1256. | TMono _ -> None
  1257. | _ as t ->
  1258. if t == t_dynamic then allow_array_dynamic := true;
  1259. Some tp)
  1260. | TAnon _ ->
  1261. (try
  1262. Some (get_iterable_param t)
  1263. with Not_found ->
  1264. None)
  1265. | TAbstract (a,pl) as t when not (List.exists (fun t' -> shallow_eq t t') seen) ->
  1266. let types =
  1267. List.fold_left
  1268. (fun acc t' -> match loop (t :: seen) t' with
  1269. | None -> acc
  1270. | Some t -> t :: acc
  1271. )
  1272. []
  1273. (get_abstract_froms a pl)
  1274. in
  1275. (match types with
  1276. | [t] -> Some t
  1277. | _ -> None)
  1278. | t ->
  1279. if t == t_dynamic then begin
  1280. allow_array_dynamic := true;
  1281. Some t
  1282. end else
  1283. None
  1284. )
  1285. in
  1286. loop [] t
  1287. | _ ->
  1288. None
  1289. ) in
  1290. (match tp with
  1291. | None ->
  1292. let el = List.map (fun e -> type_expr ctx e WithType.value) el in
  1293. let t = try
  1294. unify_min_raise ctx el
  1295. with Error (Unify l,p) ->
  1296. if !allow_array_dynamic || ctx.untyped || ctx.com.display.dms_error_policy = EPIgnore then
  1297. t_dynamic
  1298. else begin
  1299. display_error ctx "Arrays of mixed types are only allowed if the type is forced to Array<Dynamic>" p;
  1300. raise (Error (Unify l, p))
  1301. end
  1302. in
  1303. mk (TArrayDecl el) (ctx.t.tarray t) p
  1304. | Some t ->
  1305. let el = List.map (fun e ->
  1306. let e = type_expr ctx e (WithType.with_type t) in
  1307. AbstractCast.cast_or_unify ctx t e p;
  1308. ) el in
  1309. mk (TArrayDecl el) (ctx.t.tarray t) p)
  1310. and type_array_comprehension ctx e with_type p =
  1311. let v = gen_local ctx (spawn_monomorph ctx p) p in
  1312. let et = ref (EConst(Ident "null"),p) in
  1313. let comprehension_pos = p in
  1314. let rec map_compr (e,p) =
  1315. match e with
  1316. | EFor(it,e2) -> (EFor (it, map_compr e2),p)
  1317. | EWhile(cond,e2,flag) -> (EWhile (cond,map_compr e2,flag),p)
  1318. | EIf (cond,e2,None) -> (EIf (cond,map_compr e2,None),p)
  1319. | EIf (cond,e2,Some e3) -> (EIf (cond,map_compr e2,Some (map_compr e3)),p)
  1320. | EBlock [e] -> (EBlock [map_compr e],p)
  1321. | EBlock el -> begin match List.rev el with
  1322. | e :: el -> (EBlock ((List.rev el) @ [map_compr e]),p)
  1323. | [] -> e,p
  1324. end
  1325. | EParenthesis e2 -> (EParenthesis (map_compr e2),p)
  1326. | EBinop(OpArrow,a,b) ->
  1327. et := (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),comprehension_pos);
  1328. (ECall ((EField ((EConst (Ident v.v_name),p),"set"),p),[a;b]),p)
  1329. | _ ->
  1330. et := (EArrayDecl [],comprehension_pos);
  1331. (ECall ((EField ((EConst (Ident v.v_name),p),"push"),p),[(e,p)]),p)
  1332. in
  1333. let e = map_compr e in
  1334. let ea = type_expr ctx !et with_type in
  1335. unify ctx v.v_type ea.etype p;
  1336. let efor = type_expr ctx e WithType.NoValue in
  1337. mk (TBlock [
  1338. mk (TVar (v,Some ea)) ctx.t.tvoid p;
  1339. efor;
  1340. mk (TLocal v) v.v_type p;
  1341. ]) v.v_type p
  1342. and type_return ?(implicit=false) ctx e with_type p =
  1343. let is_abstract_ctor = ctx.curfun = FunMemberAbstract && ctx.curfield.cf_name = "_new" in
  1344. match e with
  1345. | None when is_abstract_ctor ->
  1346. let e_cast = mk (TCast(get_this ctx p,None)) ctx.ret p in
  1347. mk (TReturn (Some e_cast)) t_dynamic p
  1348. | None ->
  1349. let v = ctx.t.tvoid in
  1350. unify ctx v ctx.ret p;
  1351. let expect_void = match with_type with
  1352. | WithType.WithType(t,_) -> ExtType.is_void (follow t)
  1353. | WithType.Value (Some ImplicitReturn) -> true
  1354. | _ -> false
  1355. in
  1356. mk (TReturn None) (if expect_void then v else t_dynamic) p
  1357. | Some e ->
  1358. if is_abstract_ctor then begin
  1359. match fst e with
  1360. | ECast((EConst(Ident "this"),_),None) -> ()
  1361. | _ -> display_error ctx "Cannot return a value from constructor" p
  1362. end;
  1363. try
  1364. let with_expected_type =
  1365. if implicit then WithType.of_implicit_return ctx.ret
  1366. else WithType.with_type ctx.ret
  1367. in
  1368. let e = type_expr ctx e with_expected_type in
  1369. match follow ctx.ret with
  1370. | TAbstract({a_path=[],"Void"},_) when implicit ->
  1371. e
  1372. | _ ->
  1373. let e = AbstractCast.cast_or_unify ctx ctx.ret e p in
  1374. match follow e.etype with
  1375. | TAbstract({a_path=[],"Void"},_) ->
  1376. begin match (Texpr.skip e).eexpr with
  1377. | TConst TNull -> error "Cannot return `null` from Void-function" p
  1378. | _ -> ()
  1379. end;
  1380. (* if we get a Void expression (e.g. from inlining) we don't want to return it (issue #4323) *)
  1381. mk (TBlock [
  1382. e;
  1383. mk (TReturn None) t_dynamic p
  1384. ]) t_dynamic e.epos;
  1385. | _ ->
  1386. mk (TReturn (Some e)) t_dynamic p
  1387. with Error(err,p) ->
  1388. check_error ctx err p;
  1389. (* If we have a bad return, let's generate a return null expression at least. This surpresses various
  1390. follow-up errors that come from the fact that the function no longer has a return expression (issue #6445). *)
  1391. let e_null = mk (TConst TNull) (mk_mono()) p in
  1392. mk (TReturn (Some e_null)) t_dynamic p
  1393. and type_cast ctx e t p =
  1394. let tpos = pos t in
  1395. let t = Typeload.load_complex_type ctx true t in
  1396. let check_param pt = match follow pt with
  1397. | TMono _ -> () (* This probably means that Dynamic wasn't bound (issue #4675). *)
  1398. | t when t == t_dynamic -> ()
  1399. | _ -> error "Cast type parameters must be Dynamic" tpos
  1400. in
  1401. let rec loop t = match follow t with
  1402. | TInst (_,params) | TEnum (_,params) ->
  1403. List.iter check_param params;
  1404. (match follow t with
  1405. | TInst (c,_) ->
  1406. (match c.cl_kind with KTypeParameter _ -> error "Can't cast to a type parameter" tpos | _ -> ());
  1407. TClassDecl c
  1408. | TEnum (e,_) -> TEnumDecl e
  1409. | _ -> die "" __LOC__);
  1410. | TAbstract (a,params) when Meta.has Meta.RuntimeValue a.a_meta ->
  1411. List.iter check_param params;
  1412. TAbstractDecl a
  1413. | TAbstract (a,params) ->
  1414. loop (Abstract.get_underlying_type a params)
  1415. | _ ->
  1416. error "Cast type must be a class or an enum" tpos
  1417. in
  1418. let texpr = loop t in
  1419. mk (TCast (type_expr ctx e WithType.value,Some texpr)) t p
  1420. and type_if ctx e e1 e2 with_type p =
  1421. let e = type_expr ctx e WithType.value in
  1422. let e = AbstractCast.cast_or_unify ctx ctx.t.tbool e p in
  1423. let e1 = type_expr ctx (Expr.ensure_block e1) with_type in
  1424. (match e2 with
  1425. | None ->
  1426. mk (TIf (e,e1,None)) ctx.t.tvoid p
  1427. | Some e2 ->
  1428. let e2 = type_expr ctx (Expr.ensure_block e2) with_type in
  1429. let e1,e2,t = match with_type with
  1430. | WithType.NoValue -> e1,e2,ctx.t.tvoid
  1431. | WithType.Value _ -> e1,e2,unify_min ctx [e1; e2]
  1432. | WithType.WithType(t,src) when (match follow t with TMono _ -> true | t -> ExtType.is_void t) ->
  1433. e1,e2,unify_min_for_type_source ctx [e1; e2] src
  1434. | WithType.WithType(t,_) ->
  1435. let e1 = AbstractCast.cast_or_unify ctx t e1 e1.epos in
  1436. let e2 = AbstractCast.cast_or_unify ctx t e2 e2.epos in
  1437. e1,e2,t
  1438. in
  1439. mk (TIf (e,e1,Some e2)) t p)
  1440. and type_meta ?(mode=MGet) ctx m e1 with_type p =
  1441. if ctx.is_display_file then DisplayEmitter.check_display_metadata ctx [m];
  1442. let old = ctx.meta in
  1443. ctx.meta <- m :: ctx.meta;
  1444. let e () = type_expr ~mode ctx e1 with_type in
  1445. let e = match m with
  1446. | (Meta.ToString,_,_) ->
  1447. let e = e() in
  1448. (match follow e.etype with
  1449. | TAbstract({a_impl = Some c},_) when PMap.mem "toString" c.cl_statics -> call_to_string ctx e
  1450. | _ -> e)
  1451. | (Meta.Markup,_,_) ->
  1452. error "Markup literals must be processed by a macro" p
  1453. | (Meta.This,_,_) ->
  1454. let e = match ctx.this_stack with
  1455. | [] -> error "Cannot type @:this this here" p
  1456. | e :: _ -> e
  1457. in
  1458. let rec loop e = match e.eexpr with
  1459. | TConst TThis -> get_this ctx e.epos
  1460. | _ -> Type.map_expr loop e
  1461. in
  1462. loop e
  1463. | (Meta.Analyzer,_,_) ->
  1464. let e = e() in
  1465. {e with eexpr = TMeta(m,e)}
  1466. | (Meta.MergeBlock,_,_) ->
  1467. begin match fst e1 with
  1468. | EBlock el ->
  1469. let e = type_block ctx el with_type p in
  1470. {e with eexpr = TMeta(m,e)}
  1471. | _ -> e()
  1472. end
  1473. | (Meta.StoredTypedExpr,_,_) ->
  1474. MacroContext.type_stored_expr ctx e1
  1475. | (Meta.NoPrivateAccess,_,_) ->
  1476. ctx.meta <- List.filter (fun(m,_,_) -> m <> Meta.PrivateAccess) ctx.meta;
  1477. e()
  1478. | (Meta.Fixed,_,_) when ctx.com.platform=Cpp ->
  1479. let e = e() in
  1480. {e with eexpr = TMeta(m,e)}
  1481. | (Meta.NullSafety, [(EConst (Ident "Off"), _)],_) ->
  1482. let e = e() in
  1483. {e with eexpr = TMeta(m,e)}
  1484. | (Meta.BypassAccessor,_,p) ->
  1485. let old_counter = ctx.bypass_accessor in
  1486. ctx.bypass_accessor <- old_counter + 1;
  1487. let e = e () in
  1488. (if ctx.bypass_accessor > old_counter then display_error ctx "Field access expression expected after @:bypassAccessor metadata" p);
  1489. e
  1490. | (Meta.Inline,_,_) ->
  1491. begin match fst e1 with
  1492. | ECall(e1,el) ->
  1493. type_call ctx e1 el WithType.value true p
  1494. | ENew (t,el) ->
  1495. let e = type_new ctx t el with_type true p in
  1496. {e with eexpr = TMeta((Meta.Inline,[],null_pos),e)}
  1497. | _ ->
  1498. display_error ctx "Call or function expected after inline keyword" p;
  1499. e();
  1500. end
  1501. | (Meta.ImplicitReturn,_,_) ->
  1502. begin match e1 with
  1503. | (EReturn e, p) -> type_return ~implicit:true ctx e with_type p
  1504. | _ -> e()
  1505. end
  1506. | _ -> e()
  1507. in
  1508. ctx.meta <- old;
  1509. e
  1510. and type_call_target ctx e el with_type inline p =
  1511. let e = maybe_type_against_enum ctx (fun () -> type_access ctx (fst e) (snd e) (MCall el) with_type) with_type true p in
  1512. let check_inline cf =
  1513. if (has_class_field_flag cf CfAbstract) then display_error ctx "Cannot force inline on abstract method" p
  1514. in
  1515. if not inline then
  1516. e
  1517. else match e with
  1518. | AKField fa ->
  1519. check_inline fa.fa_field;
  1520. AKField({fa with fa_inline = true})
  1521. | AKUsingField sea ->
  1522. check_inline sea.se_access.fa_field;
  1523. AKUsingField {sea with se_access = {sea.se_access with fa_inline = true}}
  1524. | AKExpr {eexpr = TLocal _} ->
  1525. display_error ctx "Cannot force inline on local functions" p;
  1526. e
  1527. | _ ->
  1528. e
  1529. and type_call ?(mode=MGet) ctx e el (with_type:WithType.t) inline p =
  1530. let def () =
  1531. let e = type_call_target ctx e el with_type inline p in
  1532. build_call ~mode ctx e el with_type p;
  1533. in
  1534. match e, el with
  1535. | (EConst (Ident "trace"),p) , e :: el ->
  1536. if Common.defined ctx.com Define.NoTraces then
  1537. null ctx.t.tvoid p
  1538. else
  1539. let mk_to_string_meta e = EMeta((Meta.ToString,[],null_pos),e),pos e in
  1540. let params = (match el with [] -> [] | _ -> [("customParams",null_pos,NoQuotes),(EArrayDecl (List.map mk_to_string_meta el) , p)]) in
  1541. let infos = mk_infos ctx p params in
  1542. if (platform ctx.com Js || platform ctx.com Python) && el = [] && has_dce ctx.com then
  1543. let e = type_expr ctx e WithType.value in
  1544. let infos = type_expr ctx infos WithType.value in
  1545. let e = match follow e.etype with
  1546. | TAbstract({a_impl = Some c},_) when PMap.mem "toString" c.cl_statics ->
  1547. call_to_string ctx e
  1548. | _ ->
  1549. e
  1550. in
  1551. let e_trace = mk (TIdent "`trace") t_dynamic p in
  1552. mk (TCall (e_trace,[e;infos])) ctx.t.tvoid p
  1553. else
  1554. type_expr ctx (ECall ((EField ((EField ((EConst (Ident "haxe"),p),"Log"),p),"trace"),p),[mk_to_string_meta e;infos]),p) WithType.NoValue
  1555. | (EField ((EConst (Ident "super"),_),_),_), _ ->
  1556. (match def() with
  1557. | { eexpr = TCall ({ eexpr = TField (_, FInstance(_, _, { cf_kind = Method MethDynamic; cf_name = name })); epos = p }, _) } as e ->
  1558. ctx.com.error ("Cannot call super." ^ name ^ " since it's a dynamic method") p;
  1559. e
  1560. | e -> e
  1561. )
  1562. | (EField (e,"bind"),p), args ->
  1563. let e = type_expr ctx e WithType.value in
  1564. (match follow e.etype with
  1565. | TFun signature -> type_bind ctx e signature args p
  1566. | _ -> def ())
  1567. | (EConst (Ident "$type"),_) , [e] ->
  1568. let e = type_expr ctx e WithType.value in
  1569. ctx.com.warning (s_type (print_context()) e.etype) e.epos;
  1570. let e = Diagnostics.secure_generated_code ctx e in
  1571. e
  1572. | (EField(e,"match"),p), [epat] ->
  1573. let et = type_expr ctx e WithType.value in
  1574. let rec has_enum_match t = match follow t with
  1575. | TEnum _ -> true
  1576. | TAbstract (a,tl) when (Meta.has Meta.Forward a.a_meta) && not (Meta.has Meta.CoreType a.a_meta) ->
  1577. (match a.a_impl with
  1578. | Some c when (PMap.exists "match" c.cl_statics) && (has_class_field_flag (PMap.find "match" c.cl_statics) CfImpl) -> false
  1579. | _ -> has_enum_match (Abstract.get_underlying_type ~return_first:true a tl))
  1580. | _ -> false
  1581. in
  1582. if has_enum_match et.etype then
  1583. Matcher.Match.match_expr ctx e [[epat],None,Some (EConst(Ident "true"),p),p] (Some (Some (EConst(Ident "false"),p),p)) (WithType.with_type ctx.t.tbool) true p
  1584. else
  1585. def ()
  1586. | (EConst (Ident "__unprotect__"),_) , [(EConst (String _),_) as e] ->
  1587. let e = type_expr ctx e WithType.value in
  1588. if Common.platform ctx.com Flash then
  1589. let t = tfun [e.etype] e.etype in
  1590. let e_unprotect = mk (TIdent "__unprotect__") t p in
  1591. mk (TCall (e_unprotect,[e])) e.etype e.epos
  1592. else
  1593. e
  1594. | (EDisplay((EConst (Ident "super"),_ as e1),dk),_),_ ->
  1595. TyperDisplay.handle_display ctx (ECall(e1,el),p) dk mode with_type
  1596. | (EConst (Ident "super"),sp) , el ->
  1597. if ctx.curfun <> FunConstructor then error "Cannot call super constructor outside class constructor" p;
  1598. let el, t = (match ctx.curclass.cl_super with
  1599. | None -> error "Current class does not have a super" p
  1600. | Some (c,params) ->
  1601. let fa = FieldAccess.get_constructor_access c params p in
  1602. let cf = fa.fa_field in
  1603. let t = TInst (c,params) in
  1604. let e = mk (TConst TSuper) t sp in
  1605. if (Meta.has Meta.CompilerGenerated cf.cf_meta) then display_error ctx (error_msg (No_constructor (TClassDecl c))) p;
  1606. let fa = FieldAccess.create e cf (FHInstance(c,params)) false p in
  1607. let fcc = unify_field_call ctx fa [] el p false in
  1608. let el = List.map fst fcc.fc_args in
  1609. el,t
  1610. ) in
  1611. mk (TCall (mk (TConst TSuper) t sp,el)) ctx.t.tvoid p
  1612. | _ ->
  1613. def ()
  1614. and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
  1615. match e with
  1616. | EField ((EConst (String(s,_)),ps),"code") ->
  1617. if UTF8.length s <> 1 then error "String must be a single UTF8 char" ps;
  1618. mk (TConst (TInt (Int32.of_int (UCharExt.code (UTF8.get s 0))))) ctx.t.tint p
  1619. | EField(_,n) when starts_with n '$' ->
  1620. error "Field names starting with $ are not allowed" p
  1621. | EConst (Ident s) ->
  1622. if s = "super" && with_type <> WithType.NoValue && not ctx.in_display then error "Cannot use super as value" p;
  1623. let e = maybe_type_against_enum ctx (fun () -> type_ident ctx s p mode with_type) with_type false p in
  1624. acc_get ctx e p
  1625. | EField _
  1626. | EArray _ ->
  1627. acc_get ctx (type_access ctx e p mode with_type) p
  1628. | EConst (Regexp (r,opt)) ->
  1629. let str = mk (TConst (TString r)) ctx.t.tstring p in
  1630. let opt = mk (TConst (TString opt)) ctx.t.tstring p in
  1631. let t = Typeload.load_core_type ctx "EReg" in
  1632. mk (TNew ((match t with TInst (c,[]) -> c | _ -> die "" __LOC__),[],[str;opt])) t p
  1633. | EConst (String(s,SSingleQuotes)) when s <> "" ->
  1634. type_expr ctx (format_string ctx s p) with_type
  1635. | EConst c ->
  1636. Texpr.type_constant ctx.com.basic c p
  1637. | EBinop (op,e1,e2) ->
  1638. type_binop ctx op e1 e2 false with_type p
  1639. | EBlock [] when (match with_type with
  1640. | NoValue -> false
  1641. (*
  1642. If expected type is unknown then treat `(...) -> {}` as an empty function
  1643. (just like `function(...) {}`) instead of returning an object.
  1644. *)
  1645. | WithType (t, Some ImplicitReturn) -> not (ExtType.is_mono (follow t))
  1646. | _ -> true
  1647. ) ->
  1648. type_expr ctx (EObjectDecl [],p) with_type
  1649. | EBlock l ->
  1650. let locals = save_locals ctx in
  1651. let e = type_block ctx l with_type p in
  1652. locals();
  1653. e
  1654. | EParenthesis e ->
  1655. let e = type_expr ctx e with_type in
  1656. mk (TParenthesis e) e.etype p
  1657. | EObjectDecl fl ->
  1658. type_object_decl ctx fl with_type p
  1659. | EArrayDecl [(EFor _,_) | (EWhile _,_) as e] ->
  1660. type_array_comprehension ctx e with_type p
  1661. | EArrayDecl ((EBinop(OpArrow,_,_),_) as e1 :: el) ->
  1662. type_map_declaration ctx e1 el with_type p
  1663. | EArrayDecl el ->
  1664. begin match with_type with
  1665. | WithType(t,_) ->
  1666. begin match follow t with
  1667. | TAbstract({a_path = (["haxe";"ds"],"Map")},[tk;tv]) ->
  1668. begin match el with
  1669. | [] ->
  1670. type_expr ctx (ENew(({tpackage=["haxe";"ds"];tname="Map";tparams=[];tsub=None},null_pos),[]),p) with_type
  1671. | [(EDisplay _,_) as e1] ->
  1672. (* This must mean we're just typing the first key of a map declaration (issue #9133). *)
  1673. type_expr ctx e1 (WithType.with_type tk)
  1674. | _ ->
  1675. type_array_decl ctx el with_type p
  1676. end
  1677. | _ ->
  1678. type_array_decl ctx el with_type p
  1679. end
  1680. | _ ->
  1681. type_array_decl ctx el with_type p
  1682. end
  1683. | EVars vl ->
  1684. type_vars ctx vl p
  1685. | EFor (it,e2) ->
  1686. ForLoop.type_for_loop ctx TyperDisplay.handle_display it e2 p
  1687. | ETernary (e1,e2,e3) ->
  1688. type_expr ctx (EIf (e1,e2,Some e3),p) with_type
  1689. | EIf (e,e1,e2) ->
  1690. type_if ctx e e1 e2 with_type p
  1691. | EWhile (cond,e,NormalWhile) ->
  1692. let old_loop = ctx.in_loop in
  1693. let cond = type_expr ctx cond WithType.value in
  1694. let cond = AbstractCast.cast_or_unify ctx ctx.t.tbool cond p in
  1695. ctx.in_loop <- true;
  1696. let e = type_expr ctx (Expr.ensure_block e) WithType.NoValue in
  1697. ctx.in_loop <- old_loop;
  1698. mk (TWhile (cond,e,NormalWhile)) ctx.t.tvoid p
  1699. | EWhile (cond,e,DoWhile) ->
  1700. let old_loop = ctx.in_loop in
  1701. ctx.in_loop <- true;
  1702. let e = type_expr ctx (Expr.ensure_block e) WithType.NoValue in
  1703. ctx.in_loop <- old_loop;
  1704. let cond = type_expr ctx cond WithType.value in
  1705. let cond = AbstractCast.cast_or_unify ctx ctx.t.tbool cond cond.epos in
  1706. mk (TWhile (cond,e,DoWhile)) ctx.t.tvoid p
  1707. | ESwitch (e1,cases,def) ->
  1708. let wrap e1 = mk (TMeta((Meta.Ast,[e,p],p),e1)) e1.etype e1.epos in
  1709. let e = Matcher.Match.match_expr ctx e1 cases def with_type false p in
  1710. wrap e
  1711. | EReturn e ->
  1712. if not ctx.in_function then begin
  1713. display_error ctx "Return outside function" p;
  1714. match e with
  1715. | None ->
  1716. Texpr.Builder.make_null t_dynamic p
  1717. | Some e ->
  1718. (* type the return expression to see if there are more errors
  1719. as well as use its type as if there was no `return`, since
  1720. that is most likely what was meant *)
  1721. type_expr ctx e WithType.value
  1722. end else
  1723. type_return ctx e with_type p
  1724. | EBreak ->
  1725. if not ctx.in_loop then display_error ctx "Break outside loop" p;
  1726. mk TBreak t_dynamic p
  1727. | EContinue ->
  1728. if not ctx.in_loop then display_error ctx "Continue outside loop" p;
  1729. mk TContinue t_dynamic p
  1730. | ETry (e1,[]) ->
  1731. type_expr ctx e1 with_type
  1732. | ETry (e1,catches) ->
  1733. type_try ctx e1 catches with_type p
  1734. | EThrow e ->
  1735. let e = type_expr ctx e WithType.value in
  1736. mk (TThrow e) (spawn_monomorph ctx p) p
  1737. | ECall (e,el) ->
  1738. type_call ~mode ctx e el with_type false p
  1739. | ENew (t,el) ->
  1740. type_new ctx t el with_type false p
  1741. | EUnop (op,flag,e) ->
  1742. type_unop ctx op flag e with_type p
  1743. | EFunction (kind,f) ->
  1744. type_local_function ctx kind f with_type p
  1745. | EUntyped e ->
  1746. let old = ctx.untyped in
  1747. ctx.untyped <- true;
  1748. if not (Meta.has Meta.HasUntyped ctx.curfield.cf_meta) then ctx.curfield.cf_meta <- (Meta.HasUntyped,[],p) :: ctx.curfield.cf_meta;
  1749. let e = type_expr ctx e with_type in
  1750. ctx.untyped <- old;
  1751. {
  1752. eexpr = e.eexpr;
  1753. etype = mk_mono();
  1754. epos = e.epos;
  1755. }
  1756. | ECast (e,None) ->
  1757. let e = type_expr ctx e WithType.value in
  1758. mk (TCast (e,None)) (spawn_monomorph ctx p) p
  1759. | ECast (e, Some t) ->
  1760. type_cast ctx e t p
  1761. | EDisplay (e,dk) ->
  1762. TyperDisplay.handle_edisplay ctx e dk mode with_type
  1763. | EDisplayNew t ->
  1764. die "" __LOC__
  1765. | ECheckType (e,t) ->
  1766. let t = Typeload.load_complex_type ctx true t in
  1767. let e = type_expr ctx e (WithType.with_type t) in
  1768. let e = AbstractCast.cast_or_unify ctx t e p in
  1769. if e.etype == t then e else mk (TCast (e,None)) t p
  1770. | EMeta (m,e1) ->
  1771. type_meta ~mode ctx m e1 with_type p
  1772. | EIs (e,(t,p_t)) ->
  1773. match t with
  1774. | CTPath tp ->
  1775. if tp.tparams <> [] then display_error ctx "Type parameters are not supported for the `is` operator" p_t;
  1776. let e = type_expr ctx e WithType.value in
  1777. let e_t = type_type ctx (tp.tpackage,tp.tname) p_t in
  1778. let e_Std_isOfType =
  1779. match Typeload.load_type_raise ctx ([],"Std") "Std" p with
  1780. | TClassDecl c ->
  1781. let cf =
  1782. try PMap.find "isOfType" c.cl_statics
  1783. with Not_found -> die "" __LOC__
  1784. in
  1785. Texpr.Builder.make_static_field c cf (mk_zero_range_pos p)
  1786. | _ -> die "" __LOC__
  1787. in
  1788. mk (TCall (e_Std_isOfType, [e; e_t])) ctx.com.basic.tbool p
  1789. | _ ->
  1790. display_error ctx "Unsupported type for `is` operator" p_t;
  1791. Texpr.Builder.make_bool ctx.com.basic false p
  1792. (* ---------------------------------------------------------------------- *)
  1793. (* TYPER INITIALIZATION *)
  1794. let rec create com =
  1795. let ctx = {
  1796. com = com;
  1797. t = com.basic;
  1798. g = {
  1799. core_api = None;
  1800. macros = None;
  1801. modules = Hashtbl.create 0;
  1802. types_module = Hashtbl.create 0;
  1803. type_patches = Hashtbl.create 0;
  1804. global_metadata = [];
  1805. module_check_policies = [];
  1806. delayed = [];
  1807. debug_delayed = [];
  1808. doinline = com.display.dms_inline && not (Common.defined com Define.NoInline);
  1809. hook_generate = [];
  1810. std = null_module;
  1811. global_using = [];
  1812. complete = false;
  1813. type_hints = [];
  1814. do_inherit = MagicTypes.on_inherit;
  1815. do_create = create;
  1816. do_macro = MacroContext.type_macro;
  1817. do_load_macro = MacroContext.load_macro';
  1818. do_load_module = TypeloadModule.load_module;
  1819. do_load_type_def = Typeload.load_type_def;
  1820. do_optimize = Optimizer.reduce_expression;
  1821. do_build_instance = InstanceBuilder.build_instance;
  1822. do_format_string = format_string;
  1823. do_finalize = Finalization.finalize;
  1824. do_generate = Finalization.generate;
  1825. do_load_core_class = Typeload.load_core_class;
  1826. };
  1827. m = {
  1828. curmod = null_module;
  1829. module_types = [];
  1830. module_using = [];
  1831. module_globals = PMap.empty;
  1832. wildcard_packages = [];
  1833. module_imports = [];
  1834. };
  1835. is_display_file = false;
  1836. bypass_accessor = 0;
  1837. meta = [];
  1838. this_stack = [];
  1839. with_type_stack = [];
  1840. call_argument_stack = [];
  1841. pass = PBuildModule;
  1842. macro_depth = 0;
  1843. untyped = false;
  1844. curfun = FunStatic;
  1845. in_function = false;
  1846. in_loop = false;
  1847. in_display = false;
  1848. get_build_infos = (fun() -> None);
  1849. in_macro = Common.defined com Define.Macro;
  1850. ret = mk_mono();
  1851. locals = PMap.empty;
  1852. type_params = [];
  1853. curclass = null_class;
  1854. curfield = null_field;
  1855. tthis = mk_mono();
  1856. opened = [];
  1857. vthis = None;
  1858. in_call_args = false;
  1859. monomorphs = {
  1860. perfunction = [];
  1861. };
  1862. on_error = (fun ctx msg p -> ctx.com.error msg p);
  1863. memory_marker = Typecore.memory_marker;
  1864. } in
  1865. ctx.g.std <- (try
  1866. TypeloadModule.load_module ctx ([],"StdTypes") null_pos
  1867. with
  1868. Error (Module_not_found ([],"StdTypes"),_) ->
  1869. try
  1870. let std_path = Sys.getenv "HAXE_STD_PATH" in
  1871. error ("Standard library not found. Please check your `HAXE_STD_PATH` environment variable (current value: \"" ^ std_path ^ "\")") null_pos
  1872. with Not_found ->
  1873. error "Standard library not found. You may need to set your `HAXE_STD_PATH` environment variable" null_pos
  1874. );
  1875. (* We always want core types to be available so we add them as default imports (issue #1904 and #3131). *)
  1876. ctx.m.module_types <- List.map (fun t -> t,null_pos) ctx.g.std.m_types;
  1877. List.iter (fun t ->
  1878. match t with
  1879. | TAbstractDecl a ->
  1880. (match snd a.a_path with
  1881. | "Void" -> ctx.t.tvoid <- TAbstract (a,[]);
  1882. | "Float" -> ctx.t.tfloat <- TAbstract (a,[]);
  1883. | "Int" -> ctx.t.tint <- TAbstract (a,[])
  1884. | "Bool" -> ctx.t.tbool <- TAbstract (a,[])
  1885. | "Dynamic" -> t_dynamic_def := TAbstract(a,List.map snd a.a_params);
  1886. | "Null" ->
  1887. let mk_null t =
  1888. try
  1889. if not (is_null ~no_lazy:true t || is_explicit_null t) then TAbstract (a,[t]) else t
  1890. with Exit ->
  1891. (* don't force lazy evaluation *)
  1892. let r = ref (lazy_available t_dynamic) in
  1893. r := lazy_wait (fun() ->
  1894. let t = (if not (is_null t) then TAbstract (a,[t]) else t) in
  1895. r := lazy_available t;
  1896. t
  1897. );
  1898. TLazy r
  1899. in
  1900. ctx.t.tnull <- mk_null;
  1901. | _ -> ())
  1902. | TEnumDecl _ | TClassDecl _ | TTypeDecl _ ->
  1903. ()
  1904. ) ctx.g.std.m_types;
  1905. let m = TypeloadModule.load_module ctx ([],"String") null_pos in
  1906. List.iter (fun mt -> match mt with
  1907. | TClassDecl c -> ctx.t.tstring <- TInst (c,[])
  1908. | _ -> ()
  1909. ) m.m_types;
  1910. let m = TypeloadModule.load_module ctx ([],"Array") null_pos in
  1911. (try
  1912. List.iter (fun t -> (
  1913. match t with
  1914. | TClassDecl ({cl_path = ([],"Array")} as c) ->
  1915. ctx.t.tarray <- (fun t -> TInst (c,[t]));
  1916. raise Exit
  1917. | _ -> ()
  1918. )) m.m_types;
  1919. die "" __LOC__
  1920. with Exit -> ());
  1921. let m = TypeloadModule.load_module ctx (["haxe"],"EnumTools") null_pos in
  1922. (match m.m_types with
  1923. | [TClassDecl c1;TClassDecl c2] -> ctx.g.global_using <- (c1,c1.cl_pos) :: (c2,c2.cl_pos) :: ctx.g.global_using
  1924. | [TClassDecl c1] ->
  1925. let m = TypeloadModule.load_module ctx (["haxe"],"EnumWithType.valueTools") null_pos in
  1926. (match m.m_types with
  1927. | [TClassDecl c2 ] -> ctx.g.global_using <- (c1,c1.cl_pos) :: (c2,c2.cl_pos) :: ctx.g.global_using
  1928. | _ -> die "" __LOC__);
  1929. | _ -> die "" __LOC__);
  1930. ignore(TypeloadModule.load_module ctx (["haxe"],"Exception") null_pos);
  1931. ctx.g.complete <- true;
  1932. ctx
  1933. ;;
  1934. unify_min_ref := unify_min;
  1935. unify_min_for_type_source_ref := unify_min_for_type_source;
  1936. make_call_ref := make_call;
  1937. type_call_target_ref := type_call_target;
  1938. type_access_ref := type_access;
  1939. type_block_ref := type_block