filters.ml 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. open Ast
  2. open Common
  3. open Type
  4. open Typecore
  5. (* PASS 1 begin *)
  6. let rec verify_ast e = match e.eexpr with
  7. | TField(_) ->
  8. ()
  9. | TTypeExpr(TClassDecl {cl_kind = KAbstractImpl a}) when not (Meta.has Meta.RuntimeValue a.a_meta) ->
  10. error "Cannot use abstract as value" e.epos
  11. | _ ->
  12. Type.iter verify_ast e
  13. (*
  14. Wraps implicit blocks in TIf, TFor, TWhile, TFunction and TTry with real ones
  15. *)
  16. let rec blockify_ast e =
  17. match e.eexpr with
  18. | TIf(e1,e2,eo) ->
  19. {e with eexpr = TIf(blockify_ast e1,mk_block (blockify_ast e2),match eo with None -> None | Some e -> Some (mk_block (blockify_ast e)))}
  20. | TFor(v,e1,e2) ->
  21. {e with eexpr = TFor(v,blockify_ast e1,mk_block (blockify_ast e2))}
  22. | TWhile(e1,e2,flag) ->
  23. {e with eexpr = TWhile(blockify_ast e1,mk_block (blockify_ast e2),flag)}
  24. | TFunction tf ->
  25. {e with eexpr = TFunction {tf with tf_expr = mk_block (blockify_ast tf.tf_expr)}}
  26. | TTry(e1,cl) ->
  27. {e with eexpr = TTry(mk_block (blockify_ast e1),List.map (fun (v,e) -> v,mk_block (blockify_ast e)) cl)}
  28. | TSwitch(e1,cases,def) ->
  29. let e1 = blockify_ast e1 in
  30. let cases = List.map (fun (el,e) ->
  31. el,mk_block (blockify_ast e)
  32. ) cases in
  33. let def = match def with None -> None | Some e -> Some (mk_block (blockify_ast e)) in
  34. {e with eexpr = TSwitch(e1,cases,def)}
  35. | _ ->
  36. Type.map_expr blockify_ast e
  37. (*
  38. Pushes complex right-hand side expression inwards.
  39. return { exprs; value; } -> { exprs; return value; }
  40. x = { exprs; value; } -> { exprs; x = value; }
  41. var x = { exprs; value; } -> { var x; exprs; x = value; }
  42. *)
  43. let promote_complex_rhs com e =
  44. let rec is_complex e = match e.eexpr with
  45. | TBlock _ | TSwitch _ | TIf _ | TTry _ | TCast(_,Some _) -> true
  46. | TBinop(_,e1,e2) -> is_complex e1 || is_complex e2
  47. | TParenthesis e | TMeta(_,e) | TCast(e, None) | TField(e,_) -> is_complex e
  48. | _ -> false
  49. in
  50. let rec loop f e = match e.eexpr with
  51. | TBlock(el) ->
  52. begin match List.rev el with
  53. | elast :: el -> {e with eexpr = TBlock(block (List.rev ((loop f elast) :: el)))}
  54. | [] -> e
  55. end
  56. | TSwitch(es,cases,edef) ->
  57. {e with eexpr = TSwitch(es,List.map (fun (el,e) -> List.map find el,loop f e) cases,match edef with None -> None | Some e -> Some (loop f e)); }
  58. | TIf(eif,ethen,eelse) ->
  59. {e with eexpr = TIf(find eif, loop f ethen, match eelse with None -> None | Some e -> Some (loop f e)); }
  60. | TTry(e1,el) ->
  61. {e with eexpr = TTry(loop f e1, List.map (fun (el,e) -> el,loop f e) el); }
  62. | TParenthesis e1 when not (Common.defined com Define.As3) ->
  63. {e with eexpr = TParenthesis(loop f e1)}
  64. | TMeta(m,e1) ->
  65. { e with eexpr = TMeta(m,loop f e1)}
  66. | TReturn _ | TThrow _ ->
  67. find e
  68. | TContinue | TBreak ->
  69. e
  70. | _ ->
  71. f (find e)
  72. and block el =
  73. let r = ref [] in
  74. List.iter (fun e ->
  75. match e.eexpr with
  76. | TVar(v,eo) ->
  77. begin match eo with
  78. | Some e when is_complex e ->
  79. let e = find e in
  80. r := (loop (fun e -> mk (TBinop(OpAssign,mk (TLocal v) v.v_type e.epos,e)) v.v_type e.epos) e)
  81. :: ((mk (TVar (v,None)) com.basic.tvoid e.epos))
  82. :: !r
  83. | Some e ->
  84. r := (mk (TVar (v,Some (find e))) com.basic.tvoid e.epos) :: !r
  85. | None -> r := (mk (TVar (v,None)) com.basic.tvoid e.epos) :: !r
  86. end
  87. | TReturn (Some e1) when (match follow e1.etype with TAbstract({a_path=[],"Void"},_) -> true | _ -> false) ->
  88. r := ({e with eexpr = TReturn None}) :: e1 :: !r
  89. | _ -> r := (find e) :: !r
  90. ) el;
  91. List.rev !r
  92. and find e = match e.eexpr with
  93. | TReturn (Some e1) -> loop (fun er -> {e with eexpr = TReturn (Some er)}) e1
  94. | TBinop(OpAssign | OpAssignOp _ as op, ({eexpr = TLocal _ | TField _ | TArray _} as e1), e2) -> loop (fun er -> {e with eexpr = TBinop(op, e1, er)}) e2
  95. | TBlock(el) -> {e with eexpr = TBlock (block el)}
  96. | _ -> Type.map_expr find e
  97. in
  98. find e
  99. (* Adds final returns to functions as required by some platforms *)
  100. let rec add_final_return e =
  101. let rec loop e t =
  102. let def_return p =
  103. let c = (match follow t with
  104. | TAbstract ({ a_path = [],"Int" },_) -> TInt 0l
  105. | TAbstract ({ a_path = [],"Float" },_) -> TFloat "0."
  106. | TAbstract ({ a_path = [],"Bool" },_) -> TBool false
  107. | _ -> TNull
  108. ) in
  109. { eexpr = TReturn (Some { eexpr = TConst c; epos = p; etype = t }); etype = t; epos = p }
  110. in
  111. match e.eexpr with
  112. | TBlock el ->
  113. (match List.rev el with
  114. | [] -> e
  115. | elast :: el ->
  116. match loop elast t with
  117. | { eexpr = TBlock el2 } -> { e with eexpr = TBlock ((List.rev el) @ el2) }
  118. | elast -> { e with eexpr = TBlock (List.rev (elast :: el)) })
  119. | TReturn _ ->
  120. e
  121. | _ ->
  122. { e with eexpr = TBlock [e;def_return e.epos] }
  123. in
  124. let e = Type.map_expr add_final_return e in
  125. match e.eexpr with
  126. | TFunction f ->
  127. let f = (match follow f.tf_type with
  128. | TAbstract ({ a_path = [],"Void" },[]) -> f
  129. | t -> { f with tf_expr = loop f.tf_expr t }
  130. ) in
  131. { e with eexpr = TFunction f }
  132. | _ -> e
  133. (* -------------------------------------------------------------------------- *)
  134. (* CHECK LOCAL VARS INIT *)
  135. let check_local_vars_init e =
  136. let intersect vl1 vl2 =
  137. PMap.mapi (fun v t -> t && PMap.find v vl2) vl1
  138. in
  139. let join vars cvars =
  140. List.iter (fun v -> vars := intersect !vars v) cvars
  141. in
  142. let restore vars old_vars declared =
  143. (* restore variables declared in this block to their previous state *)
  144. vars := List.fold_left (fun acc v ->
  145. try PMap.add v (PMap.find v old_vars) acc with Not_found -> PMap.remove v acc
  146. ) !vars declared;
  147. in
  148. let declared = ref [] in
  149. let rec loop vars e =
  150. match e.eexpr with
  151. | TLocal v ->
  152. let init = (try PMap.find v.v_id !vars with Not_found -> true) in
  153. if not init then begin
  154. if v.v_name = "this" then error "Missing this = value" e.epos
  155. else error ("Local variable " ^ v.v_name ^ " used without being initialized") e.epos
  156. end
  157. | TVar (v,eo) ->
  158. begin
  159. match eo with
  160. | None ->
  161. declared := v.v_id :: !declared;
  162. vars := PMap.add v.v_id false !vars
  163. | Some e ->
  164. loop vars e
  165. end
  166. | TBlock el ->
  167. let old = !declared in
  168. let old_vars = !vars in
  169. declared := [];
  170. List.iter (loop vars) el;
  171. restore vars old_vars (List.rev !declared);
  172. declared := old;
  173. | TBinop (OpAssign,{ eexpr = TLocal v },e) when PMap.mem v.v_id !vars ->
  174. loop vars e;
  175. vars := PMap.add v.v_id true !vars
  176. | TIf (e1,e2,eo) ->
  177. loop vars e1;
  178. let vbase = !vars in
  179. loop vars e2;
  180. (match eo with
  181. | None -> vars := vbase
  182. (* ignore else false cases (they are added by the side-effect handler) *)
  183. | Some {eexpr = TConst (TBool(false))} -> ()
  184. | Some e ->
  185. let v1 = !vars in
  186. vars := vbase;
  187. loop vars e;
  188. vars := intersect !vars v1)
  189. | TWhile (cond,e,flag) ->
  190. (match flag with
  191. | NormalWhile when (match cond.eexpr with TParenthesis {eexpr = TConst (TBool true)} -> false | _ -> true) ->
  192. loop vars cond;
  193. let old = !vars in
  194. loop vars e;
  195. vars := old;
  196. | _ ->
  197. loop vars e;
  198. loop vars cond)
  199. | TTry (e,catches) ->
  200. let cvars = List.map (fun (v,e) ->
  201. let old = !vars in
  202. loop vars e;
  203. let v = !vars in
  204. vars := old;
  205. v
  206. ) catches in
  207. loop vars e;
  208. join vars cvars;
  209. | TSwitch (e,cases,def) ->
  210. loop vars e;
  211. let cvars = List.map (fun (ec,e) ->
  212. let old = !vars in
  213. List.iter (loop vars) ec;
  214. vars := old;
  215. loop vars e;
  216. let v = !vars in
  217. vars := old;
  218. v
  219. ) cases in
  220. (match def with
  221. | None when (match e.eexpr with TMeta((Meta.Exhaustive,_,_),_) | TParenthesis({eexpr = TMeta((Meta.Exhaustive,_,_),_)}) -> true | _ -> false) ->
  222. (match cvars with
  223. | cv :: cvars ->
  224. PMap.iter (fun i b -> if b then vars := PMap.add i b !vars) cv;
  225. join vars cvars
  226. | [] -> ())
  227. | None -> ()
  228. | Some e ->
  229. loop vars e;
  230. join vars cvars)
  231. (* mark all reachable vars as initialized, since we don't exit the block *)
  232. | TBreak | TContinue | TReturn None ->
  233. vars := PMap.map (fun _ -> true) !vars
  234. | TThrow e | TReturn (Some e) ->
  235. loop vars e;
  236. vars := PMap.map (fun _ -> true) !vars
  237. | _ ->
  238. Type.iter (loop vars) e
  239. in
  240. loop (ref PMap.empty) e;
  241. e
  242. (* -------------------------------------------------------------------------- *)
  243. (* BLOCK VARIABLES CAPTURE *)
  244. (*
  245. For some platforms, it will simply mark the variables which are used in closures
  246. using the v_capture flag so it can be processed in a more optimized
  247. For Flash/JS platforms, it will ensure that variables used in loop sub-functions
  248. have an unique scope. It transforms the following expression :
  249. for( x in array )
  250. funs.push(function() return x++);
  251. Into the following :
  252. for( _x in array ) {
  253. var x = [_x];
  254. funs.push(function(x) { function() return x[0]++; }(x));
  255. }
  256. *)
  257. type usage =
  258. | Block of ((usage -> unit) -> unit)
  259. | Loop of ((usage -> unit) -> unit)
  260. | Function of ((usage -> unit) -> unit)
  261. | Declare of tvar
  262. | Use of tvar
  263. | Assign of tvar
  264. let rec local_usage f e =
  265. match e.eexpr with
  266. | TBinop ((OpAssign | OpAssignOp _), { eexpr = TLocal v }, e2) ->
  267. local_usage f e2;
  268. f (Assign v)
  269. | TUnop ((Increment | Decrement), _, { eexpr = TLocal v }) ->
  270. f (Assign v)
  271. | TLocal v ->
  272. f (Use v)
  273. | TVar (v,eo) ->
  274. (match eo with None -> () | Some e -> local_usage f e);
  275. f (Declare v);
  276. | TFunction tf ->
  277. let cc f =
  278. List.iter (fun (v,_) -> f (Declare v)) tf.tf_args;
  279. local_usage f tf.tf_expr;
  280. in
  281. f (Function cc)
  282. | TBlock l ->
  283. f (Block (fun f -> List.iter (local_usage f) l))
  284. | TFor (v,it,e) ->
  285. local_usage f it;
  286. f (Loop (fun f ->
  287. f (Declare v);
  288. local_usage f e;
  289. ))
  290. | TWhile _ ->
  291. f (Loop (fun f ->
  292. iter (local_usage f) e
  293. ))
  294. | TTry (e,catchs) ->
  295. local_usage f e;
  296. List.iter (fun (v,e) ->
  297. f (Block (fun f ->
  298. f (Declare v);
  299. local_usage f e;
  300. ))
  301. ) catchs;
  302. | _ ->
  303. iter (local_usage f) e
  304. let captured_vars com e =
  305. let t = com.basic in
  306. let impl = match com.platform with
  307. (* optimized version for C#/Java - use native arrays *)
  308. | Cs | Java ->
  309. let cnativearray =
  310. match (List.find (fun md -> match md with
  311. | TClassDecl ({ cl_path = ["cs"|"java"],"NativeArray" }) -> true
  312. | _ -> false
  313. ) com.types)
  314. with TClassDecl cl -> cl | _ -> assert false
  315. in
  316. object
  317. method captured_type t = TInst (cnativearray,[t])
  318. method mk_ref v ve p =
  319. match ve with
  320. | None ->
  321. let eone = mk (TConst (TInt (Int32.of_int 1))) t.tint p in
  322. let t = match v.v_type with TInst (_, [t]) -> t | _ -> assert false in
  323. mk (TNew (cnativearray,[t],[eone])) v.v_type p
  324. | Some e ->
  325. { (Optimizer.mk_untyped_call "__array__" p [e]) with etype = v.v_type }
  326. method mk_ref_access e v =
  327. mk (TArray ({ e with etype = v.v_type }, mk (TConst (TInt 0l)) t.tint e.epos)) e.etype e.epos
  328. method mk_init av v pos =
  329. let elocal = mk (TLocal v) v.v_type pos in
  330. let earray = { (Optimizer.mk_untyped_call "__array__" pos [elocal]) with etype = av.v_type } in
  331. mk (TVar (av,Some earray)) t.tvoid pos
  332. end
  333. (* default implementation - use haxe array *)
  334. | _ ->
  335. object
  336. method captured_type = t.tarray
  337. method mk_ref v ve p =
  338. mk (TArrayDecl (match ve with None -> [] | Some e -> [e])) v.v_type p
  339. method mk_ref_access e v =
  340. mk (TArray ({ e with etype = v.v_type }, mk (TConst (TInt 0l)) t.tint e.epos)) e.etype e.epos
  341. method mk_init av v pos =
  342. mk (TVar (av,Some (mk (TArrayDecl [mk (TLocal v) v.v_type pos]) av.v_type pos))) t.tvoid pos
  343. end
  344. in
  345. let mk_var v used =
  346. let v2 = alloc_var v.v_name (PMap.find v.v_id used) in
  347. v2.v_meta <- v.v_meta;
  348. v2
  349. in
  350. let rec wrap used e =
  351. match e.eexpr with
  352. | TVar (v,ve) ->
  353. let v,ve =
  354. if PMap.mem v.v_id used then
  355. v, Some (impl#mk_ref v (Option.map (wrap used) ve) e.epos)
  356. else
  357. v, (match ve with None -> None | Some e -> Some (wrap used e))
  358. in
  359. { e with eexpr = TVar (v,ve) }
  360. | TLocal v when PMap.mem v.v_id used ->
  361. impl#mk_ref_access e v
  362. | TFor (v,it,expr) when PMap.mem v.v_id used ->
  363. let vtmp = mk_var v used in
  364. let it = wrap used it in
  365. let expr = wrap used expr in
  366. mk (TFor (vtmp,it,Type.concat (impl#mk_init v vtmp e.epos) expr)) e.etype e.epos
  367. | TTry (expr,catchs) ->
  368. let catchs = List.map (fun (v,e) ->
  369. let e = wrap used e in
  370. try
  371. let vtmp = mk_var v used in
  372. vtmp, Type.concat (impl#mk_init v vtmp e.epos) e
  373. with Not_found ->
  374. v, e
  375. ) catchs in
  376. mk (TTry (wrap used expr,catchs)) e.etype e.epos
  377. | TFunction f ->
  378. (*
  379. list variables that are marked as used, but also used in that
  380. function and which are not declared inside it !
  381. *)
  382. let fused = ref PMap.empty in
  383. let tmp_used = ref used in
  384. let rec browse = function
  385. | Block f | Loop f | Function f -> f browse
  386. | Use v | Assign v ->
  387. if PMap.mem v.v_id !tmp_used then fused := PMap.add v.v_id v !fused;
  388. | Declare v ->
  389. tmp_used := PMap.remove v.v_id !tmp_used
  390. in
  391. local_usage browse e;
  392. let vars = PMap.fold (fun v acc -> v :: acc) !fused [] in
  393. (* in case the variable has been marked as used in a parallel scope... *)
  394. let fexpr = ref (wrap used f.tf_expr) in
  395. let fargs = List.map (fun (v,o) ->
  396. if PMap.mem v.v_id used then
  397. let vtmp = mk_var v used in
  398. fexpr := Type.concat (impl#mk_init v vtmp e.epos) !fexpr;
  399. vtmp, o
  400. else
  401. v, o
  402. ) f.tf_args in
  403. let e = { e with eexpr = TFunction { f with tf_args = fargs; tf_expr = !fexpr } } in
  404. (*
  405. Create a new function scope to make sure that the captured loop variable
  406. will not be overwritten in next loop iteration
  407. *)
  408. if com.config.pf_capture_policy = CPLoopVars then
  409. mk (TCall (
  410. Codegen.mk_parent (mk (TFunction {
  411. tf_args = List.map (fun v -> v, None) vars;
  412. tf_type = e.etype;
  413. tf_expr = mk_block (mk (TReturn (Some e)) e.etype e.epos);
  414. }) (TFun (List.map (fun v -> v.v_name,false,v.v_type) vars,e.etype)) e.epos),
  415. List.map (fun v -> mk (TLocal v) v.v_type e.epos) vars)
  416. ) e.etype e.epos
  417. else
  418. e
  419. | _ ->
  420. map_expr (wrap used) e
  421. and do_wrap used e =
  422. if PMap.is_empty used then
  423. e
  424. else
  425. let used = PMap.map (fun v ->
  426. let vt = v.v_type in
  427. v.v_type <- impl#captured_type vt;
  428. v.v_capture <- true;
  429. vt
  430. ) used in
  431. wrap used e
  432. and out_loop e =
  433. match e.eexpr with
  434. | TFor _ | TWhile _ ->
  435. (*
  436. collect variables that are declared in loop but used in subfunctions
  437. *)
  438. let vars = ref PMap.empty in
  439. let used = ref PMap.empty in
  440. let depth = ref 0 in
  441. let rec collect_vars in_loop = function
  442. | Block f ->
  443. let old = !vars in
  444. f (collect_vars in_loop);
  445. vars := old;
  446. | Loop f ->
  447. let old = !vars in
  448. f (collect_vars true);
  449. vars := old;
  450. | Function f ->
  451. incr depth;
  452. f (collect_vars false);
  453. decr depth;
  454. | Declare v ->
  455. if in_loop then vars := PMap.add v.v_id !depth !vars;
  456. | Use v | Assign v ->
  457. try
  458. let d = PMap.find v.v_id !vars in
  459. if d <> !depth then used := PMap.add v.v_id v !used;
  460. with Not_found ->
  461. ()
  462. in
  463. local_usage (collect_vars false) e;
  464. do_wrap !used e
  465. | _ ->
  466. map_expr out_loop e
  467. and all_vars e =
  468. let vars = ref PMap.empty in
  469. let used = ref PMap.empty in
  470. let assigned = ref PMap.empty in
  471. let depth = ref 0 in
  472. let rec collect_vars = function
  473. | Block f ->
  474. let old = !vars in
  475. f collect_vars;
  476. vars := old;
  477. | Loop f ->
  478. let old = !vars in
  479. f collect_vars;
  480. vars := old;
  481. | Function f ->
  482. incr depth;
  483. f collect_vars;
  484. decr depth;
  485. | Declare v ->
  486. vars := PMap.add v.v_id !depth !vars;
  487. | Use v ->
  488. (try
  489. let d = PMap.find v.v_id !vars in
  490. if d <> !depth then used := PMap.add v.v_id v !used;
  491. with Not_found -> ())
  492. | Assign v ->
  493. (try
  494. let d = PMap.find v.v_id !vars in
  495. (* different depth - needs wrap *)
  496. if d <> !depth then begin
  497. used := PMap.add v.v_id v !used;
  498. assigned := PMap.add v.v_id v !assigned;
  499. end
  500. (* same depth but assigned after being used on a different depth - needs wrap *)
  501. else if PMap.mem v.v_id !used then
  502. assigned := PMap.add v.v_id v !assigned;
  503. with Not_found -> ())
  504. in
  505. local_usage collect_vars e;
  506. (* mark all capture variables - also used in rename_local_vars at later stage *)
  507. PMap.iter (fun _ v -> v.v_capture <- true) !used;
  508. !assigned
  509. in
  510. let captured = all_vars e in
  511. match com.config.pf_capture_policy with
  512. | CPNone -> e
  513. | CPWrapRef -> do_wrap captured e
  514. | CPLoopVars -> out_loop e
  515. (* -------------------------------------------------------------------------- *)
  516. (* RENAME LOCAL VARS *)
  517. let rename_local_vars ctx e =
  518. let cfg = ctx.com.config in
  519. let all_scope = (not cfg.pf_captured_scope) || (not cfg.pf_locals_scope) in
  520. let vars = ref PMap.empty in
  521. let all_vars = ref PMap.empty in
  522. let vtemp = alloc_var "~" t_dynamic in
  523. let rebuild_vars = ref false in
  524. let rebuild m =
  525. PMap.fold (fun v acc -> PMap.add v.v_name v acc) m PMap.empty
  526. in
  527. let save() =
  528. let old = !vars in
  529. if cfg.pf_unique_locals || not cfg.pf_locals_scope then (fun() -> ()) else (fun() -> vars := if !rebuild_vars then rebuild old else old)
  530. in
  531. let rename vars v =
  532. let count = ref 1 in
  533. while PMap.mem (v.v_name ^ string_of_int !count) vars do
  534. incr count;
  535. done;
  536. v.v_name <- v.v_name ^ string_of_int !count;
  537. in
  538. let declare v p =
  539. (match follow v.v_type with
  540. | TAbstract ({a_path = [],"Void"},_) -> error "Arguments and variables of type Void are not allowed" p
  541. | _ -> ());
  542. (* chop escape char for all local variables generated *)
  543. if is_gen_local v then v.v_name <- "_g" ^ String.sub v.v_name 1 (String.length v.v_name - 1);
  544. let look_vars = (if not cfg.pf_captured_scope && v.v_capture then !all_vars else !vars) in
  545. (try
  546. let v2 = PMap.find v.v_name look_vars in
  547. (*
  548. block_vars will create some wrapper-functions that are declaring
  549. the same variable twice. In that case do not perform a rename since
  550. we are sure it's actually the same variable
  551. *)
  552. if v == v2 then raise Not_found;
  553. rename look_vars v;
  554. with Not_found ->
  555. ());
  556. vars := PMap.add v.v_name v !vars;
  557. if all_scope then all_vars := PMap.add v.v_name v !all_vars;
  558. in
  559. (*
  560. This is quite a rare case, when a local variable would otherwise prevent
  561. accessing a type because it masks the type value or the package name.
  562. *)
  563. let check t =
  564. match (t_infos t).mt_path with
  565. | [], name | name :: _, _ ->
  566. let vars = if cfg.pf_locals_scope then vars else all_vars in
  567. (try
  568. let v = PMap.find name !vars in
  569. if v == vtemp then raise Not_found; (* ignore *)
  570. rename (!vars) v;
  571. rebuild_vars := true;
  572. vars := PMap.add v.v_name v !vars
  573. with Not_found ->
  574. ());
  575. vars := PMap.add name vtemp !vars
  576. in
  577. let check_type t =
  578. match follow t with
  579. | TInst (c,_) -> check (TClassDecl c)
  580. | TEnum (e,_) -> check (TEnumDecl e)
  581. | TType (t,_) -> check (TTypeDecl t)
  582. | TAbstract (a,_) -> check (TAbstractDecl a)
  583. | TMono _ | TLazy _ | TAnon _ | TDynamic _ | TFun _ -> ()
  584. in
  585. let rec loop e =
  586. match e.eexpr with
  587. | TVar (v,eo) ->
  588. if not cfg.pf_locals_scope then declare v e.epos;
  589. (match eo with None -> () | Some e -> loop e);
  590. if cfg.pf_locals_scope then declare v e.epos;
  591. | TFunction tf ->
  592. let old = save() in
  593. List.iter (fun (v,_) -> declare v e.epos) tf.tf_args;
  594. loop tf.tf_expr;
  595. old()
  596. | TBlock el ->
  597. let old = save() in
  598. (* we have to look ahead for vars on these targets (issue #3344) *)
  599. begin match ctx.com.platform with
  600. | Js | Flash8 ->
  601. let rec check_var e = match e.eexpr with
  602. | TVar (v,eo) ->
  603. (match eo with None -> () | Some e -> loop e);
  604. declare v e.epos
  605. | TBlock _ ->
  606. ()
  607. | _ ->
  608. Type.iter check_var e
  609. in
  610. List.iter check_var el
  611. | _ ->
  612. ()
  613. end;
  614. List.iter loop el;
  615. old()
  616. | TFor (v,it,e1) ->
  617. loop it;
  618. let old = save() in
  619. declare v e.epos;
  620. loop e1;
  621. old()
  622. | TTry (e,catchs) ->
  623. loop e;
  624. List.iter (fun (v,e) ->
  625. let old = save() in
  626. declare v e.epos;
  627. check_type v.v_type;
  628. loop e;
  629. old()
  630. ) catchs;
  631. | TTypeExpr t ->
  632. check t
  633. | TNew (c,_,_) ->
  634. Type.iter loop e;
  635. check (TClassDecl c);
  636. | TCast (e,Some t) ->
  637. loop e;
  638. check t;
  639. | TConst TSuper ->
  640. check_type e.etype
  641. | _ ->
  642. Type.iter loop e
  643. in
  644. declare (alloc_var "this" t_dynamic) Ast.null_pos; (* force renaming of 'this' vars in abstract *)
  645. begin match ctx.curclass.cl_path with
  646. | s :: _,_ | [],s -> declare (alloc_var s t_dynamic) Ast.null_pos
  647. end;
  648. loop e;
  649. e
  650. let check_unification ctx e t =
  651. begin match follow e.etype,follow t with
  652. | TEnum _,TDynamic _ ->
  653. Hashtbl.replace ctx.curclass.cl_module.m_extra.m_features "may_print_enum" true;
  654. | _ ->
  655. ()
  656. end;
  657. begin match e.eexpr,t with
  658. | TLocal v,TType({t_path = ["cs"],("Ref" | "Out")},_) ->
  659. (* TODO: this smells of hack, but we have to deal with it somehow *)
  660. v.v_capture <- true
  661. | _ ->
  662. ()
  663. end;
  664. e
  665. (* PASS 1 end *)
  666. (* Saves a class state so it can be restored later, e.g. after DCE or native path rewrite *)
  667. let save_class_state ctx t = match t with
  668. | TClassDecl c ->
  669. let meta = c.cl_meta and path = c.cl_path and ext = c.cl_extern in
  670. let fl = c.cl_fields and ofl = c.cl_ordered_fields and st = c.cl_statics and ost = c.cl_ordered_statics in
  671. let cst = c.cl_constructor and over = c.cl_overrides in
  672. let oflk = List.map (fun f -> f.cf_kind,f.cf_expr,f.cf_type) ofl in
  673. let ostk = List.map (fun f -> f.cf_kind,f.cf_expr,f.cf_type) ost in
  674. c.cl_restore <- (fun() ->
  675. c.cl_meta <- meta;
  676. c.cl_extern <- ext;
  677. c.cl_path <- path;
  678. c.cl_fields <- fl;
  679. c.cl_ordered_fields <- ofl;
  680. c.cl_statics <- st;
  681. c.cl_ordered_statics <- ost;
  682. c.cl_constructor <- cst;
  683. c.cl_overrides <- over;
  684. (* DCE might modify the cf_kind, so let's restore it as well *)
  685. List.iter2 (fun f (k,e,t) -> f.cf_kind <- k; f.cf_expr <- e; f.cf_type <- t;) ofl oflk;
  686. List.iter2 (fun f (k,e,t) -> f.cf_kind <- k; f.cf_expr <- e; f.cf_type <- t;) ost ostk;
  687. )
  688. | _ ->
  689. ()
  690. (* PASS 2 begin *)
  691. let is_removable_class c = c.cl_kind = KGeneric && (Codegen.has_ctor_constraint c || Meta.has Meta.Remove c.cl_meta)
  692. let remove_generic_base ctx t = match t with
  693. | TClassDecl c when is_removable_class c ->
  694. c.cl_extern <- true
  695. | _ ->
  696. ()
  697. (* Removes extern and macro fields, also checks for Void fields *)
  698. let remove_extern_fields ctx t = match t with
  699. | TClassDecl c ->
  700. if not (Common.defined ctx.com Define.DocGen) then begin
  701. c.cl_ordered_fields <- List.filter (fun f ->
  702. let b = Codegen.is_removable_field ctx f in
  703. if b then c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
  704. not b
  705. ) c.cl_ordered_fields;
  706. c.cl_ordered_statics <- List.filter (fun f ->
  707. let b = Codegen.is_removable_field ctx f in
  708. if b then c.cl_statics <- PMap.remove f.cf_name c.cl_statics;
  709. not b
  710. ) c.cl_ordered_statics;
  711. end
  712. | _ ->
  713. ()
  714. (* PASS 2 end *)
  715. (* PASS 3 begin *)
  716. (* Checks if a private class' path clashes with another path *)
  717. let check_private_path ctx t = match t with
  718. | TClassDecl c when c.cl_private ->
  719. let rpath = (fst c.cl_module.m_path,"_" ^ snd c.cl_module.m_path) in
  720. if Hashtbl.mem ctx.g.types_module rpath then error ("This private class name will clash with " ^ s_type_path rpath) c.cl_pos;
  721. | _ ->
  722. ()
  723. (* Rewrites class or enum paths if @:native metadata is set *)
  724. let apply_native_paths ctx t =
  725. let get_native_name meta =
  726. let rec get_native meta = match meta with
  727. | [] -> raise Not_found
  728. | (Meta.Native,[v],p as meta) :: _ ->
  729. meta
  730. | _ :: meta ->
  731. get_native meta
  732. in
  733. let (_,e,mp) = get_native meta in
  734. match e with
  735. | [Ast.EConst (Ast.String name),p] ->
  736. name,p
  737. | [] ->
  738. raise Not_found
  739. | _ ->
  740. error "String expected" mp
  741. in
  742. let get_real_name meta name =
  743. let name',p = get_native_name meta in
  744. (Meta.RealPath,[Ast.EConst (Ast.String (name)), p], p), name'
  745. in
  746. let get_real_path meta path =
  747. let name,p = get_native_name meta in
  748. (Meta.RealPath,[Ast.EConst (Ast.String (s_type_path path)), p], p), parse_path name
  749. in
  750. try
  751. (match t with
  752. | TClassDecl c ->
  753. let did_change = ref false in
  754. let field cf = try
  755. let meta,name = get_real_name cf.cf_meta cf.cf_name in
  756. cf.cf_name <- name;
  757. cf.cf_meta <- meta :: cf.cf_meta;
  758. List.iter (fun cf -> cf.cf_name <- name) cf.cf_overloads;
  759. did_change := true
  760. with Not_found ->
  761. ()
  762. in
  763. let fields cfs old_map =
  764. did_change := false;
  765. List.iter field cfs;
  766. if !did_change then
  767. List.fold_left (fun map f -> PMap.add f.cf_name f map) PMap.empty cfs
  768. else
  769. old_map
  770. in
  771. c.cl_fields <- fields c.cl_ordered_fields c.cl_fields;
  772. c.cl_statics <- fields c.cl_ordered_statics c.cl_statics;
  773. let meta,path = get_real_path c.cl_meta c.cl_path in
  774. c.cl_meta <- meta :: c.cl_meta;
  775. c.cl_path <- path;
  776. | TEnumDecl e ->
  777. let meta,path = get_real_path e.e_meta e.e_path in
  778. e.e_meta <- meta :: e.e_meta;
  779. e.e_path <- path;
  780. | TAbstractDecl a ->
  781. let meta,path = get_real_path a.a_meta a.a_path in
  782. a.a_meta <- meta :: a.a_meta;
  783. a.a_path <- path;
  784. | _ ->
  785. ())
  786. with Not_found ->
  787. ()
  788. (* Adds the __rtti field if required *)
  789. let add_rtti ctx t =
  790. let rec has_rtti c =
  791. Meta.has Meta.Rtti c.cl_meta || match c.cl_super with None -> false | Some (csup,_) -> has_rtti csup
  792. in
  793. match t with
  794. | TClassDecl c when has_rtti c && not (PMap.mem "__rtti" c.cl_statics) ->
  795. let f = mk_field "__rtti" ctx.t.tstring c.cl_pos in
  796. let str = Genxml.gen_type_string ctx.com t in
  797. f.cf_expr <- Some (mk (TConst (TString str)) f.cf_type c.cl_pos);
  798. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  799. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  800. | _ ->
  801. ()
  802. (* Adds member field initializations as assignments to the constructor *)
  803. let add_field_inits ctx t =
  804. let is_as3 = Common.defined ctx.com Define.As3 && not ctx.in_macro in
  805. let apply c =
  806. let ethis = mk (TConst TThis) (TInst (c,List.map snd c.cl_params)) c.cl_pos in
  807. (* TODO: we have to find a variable name which is not used in any of the functions *)
  808. let v = alloc_var "_g" ethis.etype in
  809. let need_this = ref false in
  810. let inits,fields = List.fold_left (fun (inits,fields) cf ->
  811. match cf.cf_kind,cf.cf_expr with
  812. | Var _, Some _ ->
  813. if is_as3 then (inits, cf :: fields) else (cf :: inits, cf :: fields)
  814. | Method MethDynamic, Some e when is_as3 ->
  815. (* TODO : this would have a better place in genSWF9 I think - NC *)
  816. (* we move the initialization of dynamic functions to the constructor and also solve the
  817. 'this' problem along the way *)
  818. let rec use_this v e = match e.eexpr with
  819. | TConst TThis ->
  820. need_this := true;
  821. mk (TLocal v) v.v_type e.epos
  822. | _ -> Type.map_expr (use_this v) e
  823. in
  824. let e = Type.map_expr (use_this v) e in
  825. let cf2 = {cf with cf_expr = Some e} in
  826. (* if the method is an override, we have to remove the class field to not get invalid overrides *)
  827. let fields = if List.memq cf c.cl_overrides then begin
  828. c.cl_fields <- PMap.remove cf.cf_name c.cl_fields;
  829. fields
  830. end else
  831. cf2 :: fields
  832. in
  833. (cf2 :: inits, fields)
  834. | _ -> (inits, cf :: fields)
  835. ) ([],[]) c.cl_ordered_fields in
  836. c.cl_ordered_fields <- (List.rev fields);
  837. match inits with
  838. | [] -> ()
  839. | _ ->
  840. let el = List.map (fun cf ->
  841. match cf.cf_expr with
  842. | None -> assert false
  843. | Some e ->
  844. let lhs = mk (TField(ethis,FInstance (c,List.map snd c.cl_params,cf))) cf.cf_type e.epos in
  845. cf.cf_expr <- None;
  846. let eassign = mk (TBinop(OpAssign,lhs,e)) e.etype e.epos in
  847. if is_as3 then begin
  848. let echeck = mk (TBinop(OpEq,lhs,(mk (TConst TNull) lhs.etype e.epos))) ctx.com.basic.tbool e.epos in
  849. mk (TIf(echeck,eassign,None)) eassign.etype e.epos
  850. end else
  851. eassign;
  852. ) inits in
  853. let el = if !need_this then (mk (TVar((v, Some ethis))) ethis.etype ethis.epos) :: el else el in
  854. match c.cl_constructor with
  855. | None ->
  856. let ct = TFun([],ctx.com.basic.tvoid) in
  857. let ce = mk (TFunction {
  858. tf_args = [];
  859. tf_type = ctx.com.basic.tvoid;
  860. tf_expr = mk (TBlock el) ctx.com.basic.tvoid c.cl_pos;
  861. }) ct c.cl_pos in
  862. let ctor = mk_field "new" ct c.cl_pos in
  863. ctor.cf_kind <- Method MethNormal;
  864. c.cl_constructor <- Some { ctor with cf_expr = Some ce };
  865. | Some cf ->
  866. match cf.cf_expr with
  867. | Some { eexpr = TFunction f } ->
  868. let bl = match f.tf_expr with {eexpr = TBlock b } -> b | x -> [x] in
  869. let ce = mk (TFunction {f with tf_expr = mk (TBlock (el @ bl)) ctx.com.basic.tvoid c.cl_pos }) cf.cf_type cf.cf_pos in
  870. c.cl_constructor <- Some {cf with cf_expr = Some ce }
  871. | _ ->
  872. assert false
  873. in
  874. match t with
  875. | TClassDecl c ->
  876. apply c
  877. | _ ->
  878. ()
  879. (* Adds the __meta__ field if required *)
  880. let add_meta_field ctx t = match t with
  881. | TClassDecl c ->
  882. (match Codegen.build_metadata ctx.com t with
  883. | None -> ()
  884. | Some e ->
  885. let f = mk_field "__meta__" t_dynamic c.cl_pos in
  886. f.cf_expr <- Some e;
  887. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  888. c.cl_statics <- PMap.add f.cf_name f c.cl_statics)
  889. | _ ->
  890. ()
  891. (* Removes interfaces tagged with @:remove metadata *)
  892. let check_remove_metadata ctx t = match t with
  893. | TClassDecl c ->
  894. c.cl_implements <- List.filter (fun (c,_) -> not (Meta.has Meta.Remove c.cl_meta)) c.cl_implements;
  895. | _ ->
  896. ()
  897. (* Checks for Void class fields *)
  898. let check_void_field ctx t = match t with
  899. | TClassDecl c ->
  900. let check f =
  901. match follow f.cf_type with TAbstract({a_path=[],"Void"},_) -> error "Fields of type Void are not allowed" f.cf_pos | _ -> ();
  902. in
  903. List.iter check c.cl_ordered_fields;
  904. List.iter check c.cl_ordered_statics;
  905. | _ ->
  906. ()
  907. (* Interfaces have no 'super', but can extend many other interfaces.
  908. This makes the first extended (implemented) interface the super for efficiency reasons (you can get one for 'free')
  909. and leaves the remaining ones as 'implemented' *)
  910. let promote_first_interface_to_super ctx t = match t with
  911. | TClassDecl c when c.cl_interface ->
  912. begin match c.cl_implements with
  913. | ({ cl_path = ["cpp";"rtti"],_ },_ ) :: _ -> ()
  914. | first_interface :: remaining ->
  915. c.cl_super <- Some first_interface;
  916. c.cl_implements <- remaining
  917. | _ -> ()
  918. end
  919. | _ ->
  920. ()
  921. let commit_features ctx t =
  922. let m = (t_infos t).mt_module in
  923. Hashtbl.iter (fun k v ->
  924. Common.add_feature ctx.com k;
  925. ) m.m_extra.m_features
  926. let check_reserved_type_paths ctx t =
  927. let m = t_infos t in
  928. if List.mem m.mt_path ctx.com.config.pf_reserved_type_paths then
  929. ctx.com.warning ("Type path " ^ (s_type_path m.mt_path) ^ " is reserved on this target") m.mt_pos
  930. (* PASS 3 end *)
  931. let run_expression_filters ctx filters t =
  932. let run e =
  933. List.fold_left (fun e f -> f e) e filters
  934. in
  935. match t with
  936. | TClassDecl c when is_removable_class c -> ()
  937. | TClassDecl c ->
  938. ctx.curclass <- c;
  939. let rec process_field f =
  940. (match f.cf_expr with
  941. | Some e when not (Codegen.is_removable_field ctx f) ->
  942. Codegen.AbstractCast.cast_stack := f :: !Codegen.AbstractCast.cast_stack;
  943. f.cf_expr <- Some (run e);
  944. Codegen.AbstractCast.cast_stack := List.tl !Codegen.AbstractCast.cast_stack;
  945. | _ -> ());
  946. List.iter process_field f.cf_overloads
  947. in
  948. List.iter process_field c.cl_ordered_fields;
  949. List.iter process_field c.cl_ordered_statics;
  950. (match c.cl_constructor with
  951. | None -> ()
  952. | Some f -> process_field f);
  953. (match c.cl_init with
  954. | None -> ()
  955. | Some e ->
  956. c.cl_init <- Some (run e));
  957. | TEnumDecl _ -> ()
  958. | TTypeDecl _ -> ()
  959. | TAbstractDecl _ -> ()
  960. let pp_counter = ref 1
  961. let is_cached t =
  962. let m = (t_infos t).mt_module.m_extra in
  963. if m.m_processed = 0 then m.m_processed <- !pp_counter;
  964. m.m_processed <> !pp_counter
  965. let apply_filters_once ctx filters t =
  966. if not (is_cached t) then run_expression_filters ctx filters t
  967. let next_compilation() =
  968. incr pp_counter
  969. let iter_expressions fl mt =
  970. match mt with
  971. | TClassDecl c ->
  972. let field cf = match cf.cf_expr with
  973. | None -> ()
  974. | Some e -> List.iter (fun f -> f e) fl
  975. in
  976. List.iter field c.cl_ordered_statics;
  977. List.iter field c.cl_ordered_fields;
  978. (match c.cl_constructor with None -> () | Some cf -> field cf)
  979. | _ ->
  980. ()
  981. let run com tctx main =
  982. begin match com.display with
  983. | DMUsage | DMPosition ->
  984. Codegen.detect_usage com;
  985. | _ ->
  986. ()
  987. end;
  988. if not (Common.defined com Define.NoDeprecationWarnings) then
  989. Codegen.DeprecationCheck.run com;
  990. let use_static_analyzer = Common.defined com Define.Analyzer in
  991. (* this part will be a bit messy until we make the analyzer the default *)
  992. let new_types = List.filter (fun t -> not (is_cached t)) com.types in
  993. if use_static_analyzer then begin
  994. (* PASS 1: general expression filters *)
  995. let filters = [
  996. Codegen.UnificationCallback.run (check_unification tctx);
  997. Codegen.AbstractCast.handle_abstract_casts tctx;
  998. Optimizer.inline_constructors tctx;
  999. Optimizer.reduce_expression tctx;
  1000. blockify_ast;
  1001. captured_vars com;
  1002. ] in
  1003. List.iter (run_expression_filters tctx filters) new_types;
  1004. Analyzer.Run.run_on_types tctx new_types;
  1005. List.iter (iter_expressions [verify_ast]) new_types;
  1006. let filters = [
  1007. Optimizer.sanitize com;
  1008. if com.config.pf_add_final_return then add_final_return else (fun e -> e);
  1009. rename_local_vars tctx;
  1010. ] in
  1011. List.iter (run_expression_filters tctx filters) new_types;
  1012. end else begin
  1013. (* PASS 1: general expression filters *)
  1014. let filters = [
  1015. Codegen.UnificationCallback.run (check_unification tctx);
  1016. Codegen.AbstractCast.handle_abstract_casts tctx;
  1017. blockify_ast;
  1018. ( if (Common.defined com Define.NoSimplify) || (Common.defined com Define.Cppia) ||
  1019. ( match com.platform with Cpp | Flash8 -> false | _ -> true ) then
  1020. fun e -> e
  1021. else
  1022. fun e ->
  1023. let save = save_locals tctx in
  1024. let timer = timer "analyzer-simplify-apply" in
  1025. let e = try snd (Analyzer.Simplifier.apply com e) with Exit -> e in
  1026. timer();
  1027. save();
  1028. e );
  1029. if com.foptimize then (fun e -> Optimizer.reduce_expression tctx (Optimizer.inline_constructors tctx e)) else Optimizer.sanitize com;
  1030. check_local_vars_init;
  1031. captured_vars com;
  1032. promote_complex_rhs com;
  1033. if com.config.pf_add_final_return then add_final_return else (fun e -> e);
  1034. rename_local_vars tctx;
  1035. ] in
  1036. List.iter (run_expression_filters tctx filters) new_types;
  1037. List.iter (iter_expressions [verify_ast]) new_types;
  1038. end;
  1039. next_compilation();
  1040. List.iter (fun f -> f()) (List.rev com.filters); (* macros onGenerate etc. *)
  1041. List.iter (save_class_state tctx) new_types;
  1042. List.iter (fun t ->
  1043. remove_generic_base tctx t;
  1044. remove_extern_fields tctx t;
  1045. ) com.types;
  1046. (* update cache dependencies before DCE is run *)
  1047. Codegen.update_cache_dependencies com;
  1048. (* check @:remove metadata before DCE so it is ignored there (issue #2923) *)
  1049. List.iter (check_remove_metadata tctx) com.types;
  1050. (* DCE *)
  1051. let dce_mode = if Common.defined com Define.As3 then
  1052. "no"
  1053. else
  1054. (try Common.defined_value com Define.Dce with _ -> "no")
  1055. in
  1056. begin match dce_mode with
  1057. | "full" -> Dce.run com main (not (Common.defined com Define.Interp))
  1058. | "std" -> Dce.run com main false
  1059. | "no" -> Dce.fix_accessors com
  1060. | _ -> failwith ("Unknown DCE mode " ^ dce_mode)
  1061. end;
  1062. (* always filter empty abstract implementation classes (issue #1885) *)
  1063. List.iter (fun mt -> match mt with
  1064. | TClassDecl({cl_kind = KAbstractImpl _} as c) when c.cl_ordered_statics = [] && c.cl_ordered_fields = [] && not (Meta.has Meta.Used c.cl_meta) ->
  1065. c.cl_extern <- true
  1066. | TClassDecl({cl_kind = KAbstractImpl a} as c) when Meta.has Meta.Enum a.a_meta ->
  1067. let is_runtime_field cf =
  1068. not (Meta.has Meta.Enum cf.cf_meta)
  1069. in
  1070. (* also filter abstract implementation classes that have only @:enum fields (issue #2858) *)
  1071. if not (Meta.has Meta.Used c.cl_meta || Common.defined com Define.As3) || not (List.exists is_runtime_field c.cl_ordered_statics) then
  1072. c.cl_extern <- true
  1073. | _ -> ()
  1074. ) com.types;
  1075. (* PASS 3: type filters *)
  1076. let type_filters = [
  1077. check_private_path;
  1078. apply_native_paths;
  1079. add_rtti;
  1080. (match com.platform with | Java | Cs -> (fun _ _ -> ()) | _ -> add_field_inits);
  1081. add_meta_field;
  1082. check_void_field;
  1083. (match com.platform with | Cpp -> promote_first_interface_to_super | _ -> (fun _ _ -> ()) );
  1084. commit_features;
  1085. (if com.config.pf_reserved_type_paths <> [] then check_reserved_type_paths else (fun _ _ -> ()));
  1086. ] in
  1087. List.iter (fun t -> List.iter (fun f -> f tctx t) type_filters) com.types