typeload.ml 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043
  1. (*
  2. * Copyright (C)2005-2013 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. open Ast
  23. open Type
  24. open Common
  25. open Typecore
  26. let locate_macro_error = ref true
  27. (*
  28. Build module structure : should be atomic - no type loading is possible
  29. *)
  30. let make_module ctx mpath file tdecls loadp =
  31. let decls = ref [] in
  32. let make_path name priv =
  33. if List.exists (fun (t,_) -> snd (t_path t) = name) !decls then error ("Type name " ^ name ^ " is already defined in this module") loadp;
  34. if priv then (fst mpath @ ["_" ^ snd mpath], name) else (fst mpath, name)
  35. in
  36. let m = {
  37. m_id = alloc_mid();
  38. m_path = mpath;
  39. m_types = [];
  40. m_extra = module_extra (Common.unique_full_path file) (Common.get_signature ctx.com) (file_time file) (if ctx.in_macro then MMacro else MCode);
  41. } in
  42. let pt = ref None in
  43. let rec make_decl acc decl =
  44. let p = snd decl in
  45. let acc = (match fst decl with
  46. | EImport _ | EUsing _ ->
  47. (match !pt with
  48. | None -> acc
  49. | Some pt ->
  50. display_error ctx "import and using may not appear after a type declaration" p;
  51. error "Previous type declaration found here" pt)
  52. | EClass d ->
  53. pt := Some p;
  54. let priv = List.mem HPrivate d.d_flags in
  55. let path = make_path d.d_name priv in
  56. let c = mk_class m path p in
  57. c.cl_module <- m;
  58. c.cl_private <- priv;
  59. c.cl_doc <- d.d_doc;
  60. c.cl_meta <- d.d_meta;
  61. decls := (TClassDecl c, decl) :: !decls;
  62. acc
  63. | EEnum d ->
  64. pt := Some p;
  65. let priv = List.mem EPrivate d.d_flags in
  66. let path = make_path d.d_name priv in
  67. let e = {
  68. e_path = path;
  69. e_module = m;
  70. e_pos = p;
  71. e_doc = d.d_doc;
  72. e_meta = d.d_meta;
  73. e_params = [];
  74. e_private = priv;
  75. e_extern = List.mem EExtern d.d_flags;
  76. e_constrs = PMap.empty;
  77. e_names = [];
  78. e_type = {
  79. t_path = [], "Enum<" ^ (s_type_path path) ^ ">";
  80. t_module = m;
  81. t_doc = None;
  82. t_pos = p;
  83. t_type = mk_mono();
  84. t_private = true;
  85. t_params = [];
  86. t_meta = [];
  87. };
  88. } in
  89. decls := (TEnumDecl e, decl) :: !decls;
  90. acc
  91. | ETypedef d ->
  92. pt := Some p;
  93. let priv = List.mem EPrivate d.d_flags in
  94. let path = make_path d.d_name priv in
  95. let t = {
  96. t_path = path;
  97. t_module = m;
  98. t_pos = p;
  99. t_doc = d.d_doc;
  100. t_private = priv;
  101. t_params = [];
  102. t_type = mk_mono();
  103. t_meta = d.d_meta;
  104. } in
  105. decls := (TTypeDecl t, decl) :: !decls;
  106. acc
  107. | EAbstract d ->
  108. let priv = List.mem APrivAbstract d.d_flags in
  109. let path = make_path d.d_name priv in
  110. let a = {
  111. a_path = path;
  112. a_private = priv;
  113. a_module = m;
  114. a_pos = p;
  115. a_doc = d.d_doc;
  116. a_params = [];
  117. a_meta = d.d_meta;
  118. a_from = [];
  119. a_to = [];
  120. a_from_field = [];
  121. a_to_field = [];
  122. a_ops = [];
  123. a_unops = [];
  124. a_impl = None;
  125. a_array = [];
  126. a_this = mk_mono();
  127. } in
  128. decls := (TAbstractDecl a, decl) :: !decls;
  129. match d.d_data with
  130. | [] when Meta.has Meta.CoreType a.a_meta ->
  131. a.a_this <- t_dynamic;
  132. acc
  133. | fields ->
  134. let a_t =
  135. let params = List.map (fun t -> TPType (CTPath { tname = t.tp_name; tparams = []; tsub = None; tpackage = [] })) d.d_params in
  136. CTPath { tpackage = []; tname = d.d_name; tparams = params; tsub = None }
  137. in
  138. let rec loop = function
  139. | [] -> a_t
  140. | AIsType t :: _ -> t
  141. | _ :: l -> loop l
  142. in
  143. let this_t = loop d.d_flags in
  144. let fields = List.map (fun f ->
  145. let stat = List.mem AStatic f.cff_access in
  146. let p = f.cff_pos in
  147. match f.cff_kind with
  148. | FProp (("get" | "never"),("set" | "never"),_,_) when not stat ->
  149. (* TODO: hack to avoid issues with abstract property generation on As3 *)
  150. if Common.defined ctx.com Define.As3 then f.cff_meta <- (Meta.Extern,[],p) :: f.cff_meta;
  151. { f with cff_access = AStatic :: f.cff_access; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
  152. | FProp _ when not stat ->
  153. display_error ctx "Member property accessors must be get/set or never" p;
  154. f
  155. | FFun fu when f.cff_name = "new" && not stat ->
  156. let init p = (EVars ["this",Some this_t,None],p) in
  157. let cast e = (ECast(e,None)),pos e in
  158. let check_type e ct = (ECheckType(e,ct)),pos e in
  159. let ret p = (EReturn (Some (cast (EConst (Ident "this"),p))),p) in
  160. if Meta.has Meta.MultiType a.a_meta then begin
  161. if List.mem AInline f.cff_access then error "MultiType constructors cannot be inline" f.cff_pos;
  162. if fu.f_expr <> None then error "MultiType constructors cannot have a body" f.cff_pos;
  163. end;
  164. let has_call e =
  165. let rec loop e = match fst e with
  166. | ECall _ -> raise Exit
  167. | _ -> Ast.map_expr loop e
  168. in
  169. try ignore(loop e); false with Exit -> true
  170. in
  171. let fu = {
  172. fu with
  173. f_expr = (match fu.f_expr with
  174. | None -> if Meta.has Meta.MultiType a.a_meta then Some (EConst (Ident "null"),p) else None
  175. | Some (EBlock [EBinop (OpAssign,(EConst (Ident "this"),_),e),_],_ | EBinop (OpAssign,(EConst (Ident "this"),_),e),_) when not (has_call e) ->
  176. Some (EReturn (Some (cast (check_type e this_t))), pos e)
  177. | Some (EBlock el,p) -> Some (EBlock (init p :: el @ [ret p]),p)
  178. | Some e -> Some (EBlock [init p;e;ret p],p)
  179. );
  180. f_type = Some a_t;
  181. } in
  182. { f with cff_name = "_new"; cff_access = AStatic :: f.cff_access; cff_kind = FFun fu; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
  183. | FFun fu when not stat ->
  184. if Meta.has Meta.From f.cff_meta then error "@:from cast functions must be static" f.cff_pos;
  185. let fu = { fu with f_args = (if List.mem AMacro f.cff_access then fu.f_args else ("this",false,Some this_t,None) :: fu.f_args) } in
  186. { f with cff_kind = FFun fu; cff_access = AStatic :: f.cff_access; cff_meta = (Meta.Impl,[],p) :: f.cff_meta }
  187. | _ ->
  188. f
  189. ) fields in
  190. let meta = ref [] in
  191. if has_meta Meta.Dce a.a_meta then meta := (Meta.Dce,[],p) :: !meta;
  192. let acc = make_decl acc (EClass { d_name = d.d_name ^ "_Impl_"; d_flags = [HPrivate]; d_data = fields; d_doc = None; d_params = []; d_meta = !meta },p) in
  193. (match !decls with
  194. | (TClassDecl c,_) :: _ ->
  195. List.iter (fun m -> match m with
  196. | ((Meta.Build | Meta.CoreApi | Meta.Allow | Meta.Access | Meta.Enum | Meta.Dce),_,_) ->
  197. c.cl_meta <- m :: c.cl_meta;
  198. | _ ->
  199. ()
  200. ) a.a_meta;
  201. a.a_impl <- Some c;
  202. c.cl_kind <- KAbstractImpl a
  203. | _ -> assert false);
  204. acc
  205. ) in
  206. decl :: acc
  207. in
  208. let tdecls = List.fold_left make_decl [] tdecls in
  209. let decls = List.rev !decls in
  210. m.m_types <- List.map fst decls;
  211. m, decls, List.rev tdecls
  212. let parse_file com file p =
  213. let ch = (try open_in_bin file with _ -> error ("Could not open " ^ file) p) in
  214. let t = Common.timer "parsing" in
  215. Lexer.init file true;
  216. incr stats.s_files_parsed;
  217. let data = (try Parser.parse com (Lexing.from_channel ch) with e -> close_in ch; t(); raise e) in
  218. close_in ch;
  219. t();
  220. Common.log com ("Parsed " ^ file);
  221. data
  222. let parse_hook = ref parse_file
  223. let type_module_hook = ref (fun _ _ _ -> None)
  224. let type_function_params_rec = ref (fun _ _ _ _ -> assert false)
  225. let return_partial_type = ref false
  226. let type_function_arg ctx t e opt p =
  227. if opt then
  228. let e = (match e with None -> Some (EConst (Ident "null"),p) | _ -> e) in
  229. ctx.t.tnull t, e
  230. else
  231. let t = match e with Some (EConst (Ident "null"),p) -> ctx.t.tnull t | _ -> t in
  232. t, e
  233. let type_var_field ctx t e stat p =
  234. if stat then ctx.curfun <- FunStatic else ctx.curfun <- FunMember;
  235. let e = type_expr ctx e (WithType t) in
  236. let e = (!cast_or_unify_ref) ctx t e p in
  237. match t with
  238. | TType ({ t_path = ([],"UInt") },[]) | TAbstract ({ a_path = ([],"UInt") },[]) when stat -> { e with etype = t }
  239. | _ -> e
  240. let apply_macro ctx mode path el p =
  241. let cpath, meth = (match List.rev (ExtString.String.nsplit path ".") with
  242. | meth :: name :: pack -> (List.rev pack,name), meth
  243. | _ -> error "Invalid macro path" p
  244. ) in
  245. ctx.g.do_macro ctx mode cpath meth el p
  246. (** since load_type_def and load_instance are used in PASS2, they should not access the structure of a type **)
  247. (*
  248. load a type or a subtype definition
  249. *)
  250. let rec load_type_def ctx p t =
  251. let no_pack = t.tpackage = [] in
  252. let tname = (match t.tsub with None -> t.tname | Some n -> n) in
  253. try
  254. if t.tsub <> None then raise Not_found;
  255. List.find (fun t2 ->
  256. let tp = t_path t2 in
  257. tp = (t.tpackage,tname) || (no_pack && snd tp = tname)
  258. ) (ctx.m.curmod.m_types @ ctx.m.module_types)
  259. with
  260. Not_found ->
  261. let next() =
  262. let t, m = (try
  263. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  264. with Error (Module_not_found _,p2) as e when p == p2 ->
  265. match t.tpackage with
  266. | "std" :: l ->
  267. let t = { t with tpackage = l } in
  268. t, ctx.g.do_load_module ctx (t.tpackage,t.tname) p
  269. | _ -> raise e
  270. ) in
  271. let tpath = (t.tpackage,tname) in
  272. try
  273. List.find (fun t -> not (t_infos t).mt_private && t_path t = tpath) m.m_types
  274. with
  275. Not_found -> raise (Error (Type_not_found (m.m_path,tname),p))
  276. in
  277. (* lookup in wildcard imported packages *)
  278. try
  279. if not no_pack then raise Exit;
  280. let rec loop = function
  281. | [] -> raise Exit
  282. | wp :: l ->
  283. try
  284. load_type_def ctx p { t with tpackage = wp }
  285. with
  286. | Error (Module_not_found _,p2)
  287. | Error (Type_not_found _,p2) when p == p2 -> loop l
  288. in
  289. loop ctx.m.wildcard_packages
  290. with Exit ->
  291. (* lookup in our own package - and its upper packages *)
  292. let rec loop = function
  293. | [] -> raise Exit
  294. | (_ :: lnext) as l ->
  295. try
  296. load_type_def ctx p { t with tpackage = List.rev l }
  297. with
  298. | Error (Module_not_found _,p2)
  299. | Error (Type_not_found _,p2) when p == p2 -> loop lnext
  300. in
  301. try
  302. if not no_pack then raise Exit;
  303. (match fst ctx.m.curmod.m_path with
  304. | [] -> raise Exit
  305. | x :: _ ->
  306. (* this can occur due to haxe remoting : a module can be
  307. already defined in the "js" package and is not allowed
  308. to access the js classes *)
  309. try
  310. (match PMap.find x ctx.com.package_rules with
  311. | Forbidden -> raise Exit
  312. | _ -> ())
  313. with Not_found -> ());
  314. loop (List.rev (fst ctx.m.curmod.m_path));
  315. with
  316. Exit -> next()
  317. let check_param_constraints ctx types t pl c p =
  318. match follow t with
  319. | TMono _ -> ()
  320. | _ ->
  321. let ctl = (match c.cl_kind with KTypeParameter l -> l | _ -> []) in
  322. List.iter (fun ti ->
  323. let ti = apply_params types pl ti in
  324. let ti = (match follow ti with
  325. | TInst ({ cl_kind = KGeneric } as c,pl) ->
  326. (* if we solve a generic contraint, let's substitute with the actual generic instance before unifying *)
  327. let _,_, f = ctx.g.do_build_instance ctx (TClassDecl c) p in
  328. f pl
  329. | _ -> ti
  330. ) in
  331. try
  332. unify_raise ctx t ti p
  333. with Error(Unify l,p) ->
  334. if not ctx.untyped then display_error ctx (error_msg (Unify (Constraint_failure (s_type_path c.cl_path) :: l))) p;
  335. ) ctl
  336. let requires_value_meta com co =
  337. Common.defined com Define.DocGen || (match co with
  338. | None -> false
  339. | Some c -> c.cl_extern || Meta.has Meta.Rtti c.cl_meta)
  340. let generate_value_meta com co cf args =
  341. if requires_value_meta com co then begin
  342. let values = List.fold_left (fun acc (name,_,_,eo) -> match eo with Some e -> (name,e) :: acc | _ -> acc) [] args in
  343. match values with
  344. | [] -> ()
  345. | _ -> cf.cf_meta <- ((Meta.Value,[EObjectDecl values,cf.cf_pos],cf.cf_pos) :: cf.cf_meta)
  346. end
  347. (* build an instance from a full type *)
  348. let rec load_instance ctx t p allow_no_params =
  349. try
  350. if t.tpackage <> [] || t.tsub <> None then raise Not_found;
  351. let pt = List.assoc t.tname ctx.type_params in
  352. if t.tparams <> [] then error ("Class type parameter " ^ t.tname ^ " can't have parameters") p;
  353. pt
  354. with Not_found ->
  355. let mt = load_type_def ctx p t in
  356. let is_generic,is_generic_build = match mt with
  357. | TClassDecl {cl_kind = KGeneric} -> true,false
  358. | TClassDecl {cl_kind = KGenericBuild _} -> false,true
  359. | _ -> false,false
  360. in
  361. let types , path , f = ctx.g.do_build_instance ctx mt p in
  362. let is_rest = is_generic_build && (match types with ["Rest",_] -> true | _ -> false) in
  363. if allow_no_params && t.tparams = [] && not is_rest then begin
  364. let pl = ref [] in
  365. pl := List.map (fun (name,t) ->
  366. match follow t with
  367. | TInst (c,_) ->
  368. let t = mk_mono() in
  369. if c.cl_kind <> KTypeParameter [] || is_generic then delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t (!pl) c p);
  370. t;
  371. | _ -> assert false
  372. ) types;
  373. f (!pl)
  374. end else if path = ([],"Dynamic") then
  375. match t.tparams with
  376. | [] -> t_dynamic
  377. | [TPType t] -> TDynamic (load_complex_type ctx p t)
  378. | _ -> error "Too many parameters for Dynamic" p
  379. else begin
  380. if not is_rest && List.length types <> List.length t.tparams then error ("Invalid number of type parameters for " ^ s_type_path path) p;
  381. let tparams = List.map (fun t ->
  382. match t with
  383. | TPExpr e ->
  384. let name = (match fst e with
  385. | EConst (String s) -> "S" ^ s
  386. | EConst (Int i) -> "I" ^ i
  387. | EConst (Float f) -> "F" ^ f
  388. | _ -> "Expr"
  389. ) in
  390. let c = mk_class null_module ([],name) p in
  391. c.cl_kind <- KExpr e;
  392. TInst (c,[])
  393. | TPType t -> load_complex_type ctx p t
  394. ) t.tparams in
  395. let rec loop tl1 tl2 is_rest = match tl1,tl2 with
  396. | t :: tl1,(name,t2) :: tl2 ->
  397. let isconst = (match t with TInst ({ cl_kind = KExpr _ },_) -> true | _ -> false) in
  398. if isconst <> (name = "Const") && t != t_dynamic && name <> "Rest" then error (if isconst then "Constant value unexpected here" else "Constant value excepted as type parameter") p;
  399. let is_rest = is_rest || name = "Rest" && is_generic_build in
  400. let t = match follow t2 with
  401. | TInst ({ cl_kind = KTypeParameter [] }, []) when not is_generic ->
  402. t
  403. | TInst (c,[]) ->
  404. let r = exc_protect ctx (fun r ->
  405. r := (fun() -> t);
  406. delay ctx PCheckConstraint (fun() -> check_param_constraints ctx types t tparams c p);
  407. t
  408. ) "constraint" in
  409. delay ctx PForce (fun () -> ignore(!r()));
  410. TLazy r
  411. | _ -> assert false
  412. in
  413. t :: loop tl1 tl2 is_rest
  414. | [],[] ->
  415. []
  416. | [],["Rest",_] when is_generic_build ->
  417. []
  418. | [],_ ->
  419. error ("Not enough type parameters for " ^ s_type_path path) p
  420. | t :: tl,[] ->
  421. if is_rest then
  422. t :: loop tl [] true
  423. else
  424. error ("Too many parameters for " ^ s_type_path path) p
  425. in
  426. let params = loop tparams types false in
  427. f params
  428. end
  429. (*
  430. build an instance from a complex type
  431. *)
  432. and load_complex_type ctx p t =
  433. match t with
  434. | CTParent t -> load_complex_type ctx p t
  435. | CTPath t -> load_instance ctx t p false
  436. | CTOptional _ -> error "Optional type not allowed here" p
  437. | CTExtend (tl,l) ->
  438. (match load_complex_type ctx p (CTAnonymous l) with
  439. | TAnon a as ta ->
  440. let is_redefined cf1 a2 =
  441. try
  442. let cf2 = PMap.find cf1.cf_name a2.a_fields in
  443. let st = s_type (print_context()) in
  444. if not (type_iseq cf1.cf_type cf2.cf_type) then begin
  445. display_error ctx ("Cannot redefine field " ^ cf1.cf_name ^ " with different type") p;
  446. display_error ctx ("First type was " ^ (st cf1.cf_type)) cf1.cf_pos;
  447. error ("Second type was " ^ (st cf2.cf_type)) cf2.cf_pos
  448. end else
  449. true
  450. with Not_found ->
  451. false
  452. in
  453. let mk_extension t =
  454. match follow t with
  455. | TInst ({cl_kind = KTypeParameter _},_) ->
  456. error "Cannot structurally extend type parameters" p
  457. | TInst (c,tl) ->
  458. ctx.com.warning "Structurally extending classes is deprecated and will be removed" p;
  459. let c2 = mk_class null_module (fst c.cl_path,"+" ^ snd c.cl_path) p in
  460. c2.cl_private <- true;
  461. PMap.iter (fun f _ ->
  462. try
  463. ignore(class_field c tl f);
  464. error ("Cannot redefine field " ^ f) p
  465. with
  466. Not_found -> ()
  467. ) a.a_fields;
  468. (* do NOT tag as extern - for protect *)
  469. c2.cl_kind <- KExtension (c,tl);
  470. c2.cl_super <- Some (c,tl);
  471. c2.cl_fields <- a.a_fields;
  472. TInst (c2,[])
  473. | TMono _ ->
  474. error "Loop found in cascading signatures definitions. Please change order/import" p
  475. | TAnon a2 ->
  476. PMap.iter (fun _ cf -> ignore(is_redefined cf a2)) a.a_fields;
  477. TAnon { a_fields = (PMap.foldi PMap.add a.a_fields a2.a_fields); a_status = ref (Extend [t]); }
  478. | _ -> error "Can only extend classes and structures" p
  479. in
  480. let loop t = match follow t with
  481. | TAnon a2 ->
  482. PMap.iter (fun f cf ->
  483. if not (is_redefined cf a) then
  484. a.a_fields <- PMap.add f cf a.a_fields
  485. ) a2.a_fields
  486. | _ ->
  487. error "Multiple structural extension is only allowed for structures" p
  488. in
  489. let il = List.map (fun t -> load_instance ctx t p false) tl in
  490. let tr = ref None in
  491. let t = TMono tr in
  492. let r = exc_protect ctx (fun r ->
  493. r := (fun _ -> t);
  494. tr := Some (match il with
  495. | [i] ->
  496. mk_extension i
  497. | _ ->
  498. List.iter loop il;
  499. a.a_status := Extend il;
  500. ta);
  501. t
  502. ) "constraint" in
  503. delay ctx PForce (fun () -> ignore(!r()));
  504. TLazy r
  505. | _ -> assert false)
  506. | CTAnonymous l ->
  507. let rec loop acc f =
  508. let n = f.cff_name in
  509. let p = f.cff_pos in
  510. if PMap.mem n acc then error ("Duplicate field declaration : " ^ n) p;
  511. let topt = function
  512. | None -> error ("Explicit type required for field " ^ n) p
  513. | Some t -> load_complex_type ctx p t
  514. in
  515. let no_expr = function
  516. | None -> ()
  517. | Some (_,p) -> error "Expression not allowed here" p
  518. in
  519. let pub = ref true in
  520. let dyn = ref false in
  521. let params = ref [] in
  522. List.iter (fun a ->
  523. match a with
  524. | APublic -> ()
  525. | APrivate -> pub := false;
  526. | ADynamic when (match f.cff_kind with FFun _ -> true | _ -> false) -> dyn := true
  527. | AStatic | AOverride | AInline | ADynamic | AMacro -> error ("Invalid access " ^ Ast.s_access a) p
  528. ) f.cff_access;
  529. let t , access = (match f.cff_kind with
  530. | FVar (Some (CTPath({tpackage=[];tname="Void"})), _) | FProp (_,_,Some (CTPath({tpackage=[];tname="Void"})),_) ->
  531. error "Fields of type Void are not allowed in structures" p
  532. | FVar (t, e) ->
  533. no_expr e;
  534. topt t, Var { v_read = AccNormal; v_write = AccNormal }
  535. | FFun fd ->
  536. params := (!type_function_params_rec) ctx fd f.cff_name p;
  537. no_expr fd.f_expr;
  538. let old = ctx.type_params in
  539. ctx.type_params <- !params @ old;
  540. let args = List.map (fun (name,o,t,e) -> no_expr e; name, o, topt t) fd.f_args in
  541. let t = TFun (args,topt fd.f_type), Method (if !dyn then MethDynamic else MethNormal) in
  542. ctx.type_params <- old;
  543. t
  544. | FProp (i1,i2,t,e) ->
  545. no_expr e;
  546. let access m get =
  547. match m with
  548. | "null" -> AccNo
  549. | "never" -> AccNever
  550. | "default" -> AccNormal
  551. | "dynamic" -> AccCall
  552. | "get" when get -> AccCall
  553. | "set" when not get -> AccCall
  554. | x when get && x = "get_" ^ n -> AccCall
  555. | x when not get && x = "set_" ^ n -> AccCall
  556. | _ ->
  557. error "Custom property access is no longer supported in Haxe 3" f.cff_pos;
  558. in
  559. let t = (match t with None -> error "Type required for structure property" p | Some t -> t) in
  560. load_complex_type ctx p t, Var { v_read = access i1 true; v_write = access i2 false }
  561. ) in
  562. let t = if Meta.has Meta.Optional f.cff_meta then ctx.t.tnull t else t in
  563. let cf = {
  564. cf_name = n;
  565. cf_type = t;
  566. cf_pos = p;
  567. cf_public = !pub;
  568. cf_kind = access;
  569. cf_params = !params;
  570. cf_expr = None;
  571. cf_doc = f.cff_doc;
  572. cf_meta = f.cff_meta;
  573. cf_overloads = [];
  574. } in
  575. init_meta_overloads ctx None cf;
  576. PMap.add n cf acc
  577. in
  578. mk_anon (List.fold_left loop PMap.empty l)
  579. | CTFunction (args,r) ->
  580. match args with
  581. | [CTPath { tpackage = []; tparams = []; tname = "Void" }] ->
  582. TFun ([],load_complex_type ctx p r)
  583. | _ ->
  584. TFun (List.map (fun t ->
  585. let t, opt = (match t with CTOptional t -> t, true | _ -> t,false) in
  586. "",opt,load_complex_type ctx p t
  587. ) args,load_complex_type ctx p r)
  588. and init_meta_overloads ctx co cf =
  589. let overloads = ref [] in
  590. let filter_meta m = match m with
  591. | ((Meta.Overload | Meta.Value),_,_) -> false
  592. | _ -> true
  593. in
  594. let cf_meta = List.filter filter_meta cf.cf_meta in
  595. cf.cf_meta <- List.filter (fun m ->
  596. match m with
  597. | (Meta.Overload,[(EFunction (fname,f),p)],_) ->
  598. if fname <> None then error "Function name must not be part of @:overload" p;
  599. (match f.f_expr with Some (EBlock [], _) -> () | _ -> error "Overload must only declare an empty method body {}" p);
  600. let old = ctx.type_params in
  601. (match cf.cf_params with
  602. | [] -> ()
  603. | l -> ctx.type_params <- List.filter (fun t -> not (List.mem t l)) ctx.type_params);
  604. let params = (!type_function_params_rec) ctx f cf.cf_name p in
  605. ctx.type_params <- params @ ctx.type_params;
  606. let topt = function None -> error "Explicit type required" p | Some t -> load_complex_type ctx p t in
  607. let args = List.map (fun (a,opt,t,_) -> a,opt,topt t) f.f_args in
  608. let cf = { cf with cf_type = TFun (args,topt f.f_type); cf_params = params; cf_meta = cf_meta} in
  609. generate_value_meta ctx.com co cf f.f_args;
  610. overloads := cf :: !overloads;
  611. ctx.type_params <- old;
  612. false
  613. | (Meta.Overload,[],_) when ctx.com.config.pf_overload ->
  614. let topt (n,_,t) = match t with | TMono t when !t = None -> error ("Explicit type required for overload functions\nFor function argument '" ^ n ^ "'") cf.cf_pos | _ -> () in
  615. (match follow cf.cf_type with
  616. | TFun (args,_) -> List.iter topt args
  617. | _ -> () (* could be a variable *));
  618. true
  619. | (Meta.Overload,[],p) ->
  620. error "This platform does not support this kind of overload declaration. Try @:overload(function()... {}) instead" p
  621. | (Meta.Overload,_,p) ->
  622. error "Invalid @:overload metadata format" p
  623. | _ ->
  624. true
  625. ) cf.cf_meta;
  626. cf.cf_overloads <- (List.rev !overloads)
  627. let hide_params ctx =
  628. let old_m = ctx.m in
  629. let old_type_params = ctx.type_params in
  630. let old_deps = ctx.g.std.m_extra.m_deps in
  631. ctx.m <- {
  632. curmod = ctx.g.std;
  633. module_types = [];
  634. module_using = [];
  635. module_globals = PMap.empty;
  636. wildcard_packages = [];
  637. };
  638. ctx.type_params <- [];
  639. (fun() ->
  640. ctx.m <- old_m;
  641. ctx.type_params <- old_type_params;
  642. (* restore dependencies that might be have been wronly inserted *)
  643. ctx.g.std.m_extra.m_deps <- old_deps;
  644. )
  645. (*
  646. load a type while ignoring the current imports or local types
  647. *)
  648. let load_core_type ctx name =
  649. let show = hide_params ctx in
  650. let t = load_instance ctx { tpackage = []; tname = name; tparams = []; tsub = None; } null_pos false in
  651. show();
  652. add_dependency ctx.m.curmod (match t with
  653. | TInst (c,_) -> c.cl_module
  654. | TType (t,_) -> t.t_module
  655. | TAbstract (a,_) -> a.a_module
  656. | TEnum (e,_) -> e.e_module
  657. | _ -> assert false);
  658. t
  659. let t_iterator ctx =
  660. let show = hide_params ctx in
  661. match load_type_def ctx null_pos { tpackage = []; tname = "Iterator"; tparams = []; tsub = None } with
  662. | TTypeDecl t ->
  663. show();
  664. add_dependency ctx.m.curmod t.t_module;
  665. if List.length t.t_params <> 1 then assert false;
  666. let pt = mk_mono() in
  667. apply_params t.t_params [pt] t.t_type, pt
  668. | _ ->
  669. assert false
  670. (*
  671. load either a type t or Null<Unknown> if not defined
  672. *)
  673. let load_type_opt ?(opt=false) ctx p t =
  674. let t = (match t with None -> mk_mono() | Some t -> load_complex_type ctx p t) in
  675. if opt then ctx.t.tnull t else t
  676. (* ---------------------------------------------------------------------- *)
  677. (* Structure check *)
  678. let valid_redefinition ctx f1 t1 f2 t2 =
  679. let valid t1 t2 =
  680. Type.unify t1 t2;
  681. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)]);
  682. in
  683. let t1, t2 = (match f1.cf_params, f2.cf_params with
  684. | [], [] -> t1, t2
  685. | l1, l2 when List.length l1 = List.length l2 ->
  686. let to_check = ref [] in
  687. let monos = List.map2 (fun (name,p1) (_,p2) ->
  688. (match follow p1, follow p2 with
  689. | TInst ({ cl_kind = KTypeParameter ct1 } as c1,pl1), TInst ({ cl_kind = KTypeParameter ct2 } as c2,pl2) ->
  690. (match ct1, ct2 with
  691. | [], [] -> ()
  692. | _, _ when List.length ct1 = List.length ct2 ->
  693. (* if same constraints, they are the same type *)
  694. let check monos =
  695. List.iter2 (fun t1 t2 ->
  696. try
  697. let t1 = apply_params l1 monos (apply_params c1.cl_params pl1 t1) in
  698. let t2 = apply_params l2 monos (apply_params c2.cl_params pl2 t2) in
  699. type_eq EqStrict t1 t2
  700. with Unify_error l ->
  701. raise (Unify_error (Unify_custom "Constraints differ" :: l))
  702. ) ct1 ct2
  703. in
  704. to_check := check :: !to_check;
  705. | _ ->
  706. raise (Unify_error [Unify_custom "Different number of constraints"]))
  707. | _ -> ());
  708. TInst (mk_class null_module ([],name) Ast.null_pos,[])
  709. ) l1 l2 in
  710. List.iter (fun f -> f monos) !to_check;
  711. apply_params l1 monos t1, apply_params l2 monos t2
  712. | _ ->
  713. (* ignore type params, will create other errors later *)
  714. t1, t2
  715. ) in
  716. match f1.cf_kind,f2.cf_kind with
  717. | Method m1, Method m2 when not (m1 = MethDynamic) && not (m2 = MethDynamic) ->
  718. begin match follow t1, follow t2 with
  719. | TFun (args1,r1) , TFun (args2,r2) -> (
  720. if not (List.length args1 = List.length args2) then raise (Unify_error [Unify_custom "Different number of function arguments"]);
  721. try
  722. List.iter2 (fun (n,o1,a1) (_,o2,a2) ->
  723. if o1 <> o2 then raise (Unify_error [Not_matching_optional n]);
  724. (try valid a2 a1 with Unify_error _ -> raise (Unify_error [Cannot_unify(a1,a2)]))
  725. ) args1 args2;
  726. valid r1 r2
  727. with Unify_error l ->
  728. raise (Unify_error (Cannot_unify (t1,t2) :: l)))
  729. | _ ->
  730. assert false
  731. end
  732. | _,(Var { v_write = AccNo | AccNever }) ->
  733. (* write variance *)
  734. valid t2 t1
  735. | _,(Var { v_read = AccNo | AccNever }) ->
  736. (* read variance *)
  737. valid t1 t2
  738. | _ , _ ->
  739. (* in case args differs, or if an interface var *)
  740. type_eq EqStrict t1 t2;
  741. if is_null t1 <> is_null t2 then raise (Unify_error [Cannot_unify (t1,t2)])
  742. let copy_meta meta_src meta_target sl =
  743. let meta = ref meta_target in
  744. List.iter (fun (m,e,p) ->
  745. if List.mem m sl then meta := (m,e,p) :: !meta
  746. ) meta_src;
  747. !meta
  748. let same_overload_args t1 t2 f1 f2 =
  749. if List.length f1.cf_params <> List.length f2.cf_params then
  750. false
  751. else
  752. let rec follow_skip_null t = match t with
  753. | TMono r ->
  754. (match !r with
  755. | Some t -> follow_skip_null t
  756. | _ -> t)
  757. | TLazy f ->
  758. follow_skip_null (!f())
  759. | TType ({ t_path = [],"Null" } as t, [p]) ->
  760. TType(t,[follow p])
  761. | TType (t,tl) ->
  762. follow_skip_null (apply_params t.t_params tl t.t_type)
  763. | _ -> t
  764. in
  765. let same_arg t1 t2 =
  766. let t1 = follow_skip_null t1 in
  767. let t2 = follow_skip_null t2 in
  768. match follow_skip_null t1, follow_skip_null t2 with
  769. | TType _, TType _ -> type_iseq t1 t2
  770. | TType _, _
  771. | _, TType _ -> false
  772. | _ -> type_iseq t1 t2
  773. in
  774. match follow (apply_params f1.cf_params (List.map (fun (_,t) -> t) f2.cf_params) t1), follow t2 with
  775. | TFun(a1,_), TFun(a2,_) ->
  776. (try
  777. List.for_all2 (fun (_,_,t1) (_,_,t2) ->
  778. same_arg t1 t2) a1 a2
  779. with | Invalid_argument("List.for_all2") ->
  780. false)
  781. | _ -> assert false
  782. (** retrieves all overloads from class c and field i, as (Type.t * tclass_field) list *)
  783. let rec get_overloads c i =
  784. let ret = try
  785. let f = PMap.find i c.cl_fields in
  786. (f.cf_type, f) :: (List.map (fun f -> f.cf_type, f) f.cf_overloads)
  787. with | Not_found -> []
  788. in
  789. let rsup = match c.cl_super with
  790. | None when c.cl_interface ->
  791. let ifaces = List.concat (List.map (fun (c,tl) ->
  792. List.map (fun (t,f) -> apply_params c.cl_params tl t, f) (get_overloads c i)
  793. ) c.cl_implements) in
  794. ret @ ifaces
  795. | None -> ret
  796. | Some (c,tl) ->
  797. ret @ ( List.map (fun (t,f) -> apply_params c.cl_params tl t, f) (get_overloads c i) )
  798. in
  799. ret @ (List.filter (fun (t,f) -> not (List.exists (fun (t2,f2) -> same_overload_args t t2 f f2) ret)) rsup)
  800. let check_overloads ctx c =
  801. (* check if field with same signature was declared more than once *)
  802. List.iter (fun f ->
  803. if Meta.has Meta.Overload f.cf_meta then
  804. List.iter (fun f2 ->
  805. try
  806. ignore (List.find (fun f3 -> f3 != f2 && same_overload_args f2.cf_type f3.cf_type f2 f3) (f :: f.cf_overloads));
  807. display_error ctx ("Another overloaded field of same signature was already declared : " ^ f2.cf_name) f2.cf_pos
  808. with | Not_found -> ()
  809. ) (f :: f.cf_overloads)) (c.cl_ordered_fields @ c.cl_ordered_statics)
  810. let check_overriding ctx c =
  811. match c.cl_super with
  812. | None ->
  813. (match c.cl_overrides with
  814. | [] -> ()
  815. | i :: _ ->
  816. display_error ctx ("Field " ^ i.cf_name ^ " is declared 'override' but doesn't override any field") i.cf_pos)
  817. | _ when c.cl_extern && Meta.has Meta.CsNative c.cl_meta -> () (* -net-lib specific: do not check overrides on extern CsNative classes *)
  818. | Some (csup,params) ->
  819. PMap.iter (fun i f ->
  820. let p = f.cf_pos in
  821. let check_field f get_super_field is_overload = try
  822. (if is_overload && not (Meta.has Meta.Overload f.cf_meta) then
  823. display_error ctx ("Missing @:overload declaration for field " ^ i) p);
  824. let t, f2 = get_super_field csup i in
  825. (* allow to define fields that are not defined for this platform version in superclass *)
  826. (match f2.cf_kind with
  827. | Var { v_read = AccRequire _ } -> raise Not_found;
  828. | _ -> ());
  829. if ctx.com.config.pf_overload && (Meta.has Meta.Overload f2.cf_meta && not (Meta.has Meta.Overload f.cf_meta)) then
  830. display_error ctx ("Field " ^ i ^ " should be declared with @:overload since it was already declared as @:overload in superclass") p
  831. else if not (List.memq f c.cl_overrides) then
  832. display_error ctx ("Field " ^ i ^ " should be declared with 'override' since it is inherited from superclass") p
  833. else if not f.cf_public && f2.cf_public then
  834. display_error ctx ("Field " ^ i ^ " has less visibility (public/private) than superclass one") p
  835. else (match f.cf_kind, f2.cf_kind with
  836. | _, Method MethInline ->
  837. display_error ctx ("Field " ^ i ^ " is inlined and cannot be overridden") p
  838. | a, b when a = b -> ()
  839. | Method MethInline, Method MethNormal ->
  840. () (* allow to redefine a method as inlined *)
  841. | _ ->
  842. display_error ctx ("Field " ^ i ^ " has different property access than in superclass") p);
  843. if has_meta Meta.Final f2.cf_meta then display_error ctx ("Cannot override @:final method " ^ i) p;
  844. try
  845. let t = apply_params csup.cl_params params t in
  846. valid_redefinition ctx f f.cf_type f2 t
  847. with
  848. Unify_error l ->
  849. display_error ctx ("Field " ^ i ^ " overloads parent class with different or incomplete type") p;
  850. display_error ctx (error_msg (Unify l)) p;
  851. with
  852. Not_found ->
  853. if List.memq f c.cl_overrides then
  854. let msg = if is_overload then
  855. ("Field " ^ i ^ " is declared 'override' but no compatible overload was found")
  856. else
  857. ("Field " ^ i ^ " is declared 'override' but doesn't override any field")
  858. in
  859. display_error ctx msg p
  860. in
  861. if ctx.com.config.pf_overload && Meta.has Meta.Overload f.cf_meta then begin
  862. let overloads = get_overloads csup i in
  863. List.iter (fun (t,f2) ->
  864. (* check if any super class fields are vars *)
  865. match f2.cf_kind with
  866. | Var _ ->
  867. display_error ctx ("A variable named '" ^ f2.cf_name ^ "' was already declared in a superclass") f.cf_pos
  868. | _ -> ()
  869. ) overloads;
  870. List.iter (fun f ->
  871. (* find the exact field being overridden *)
  872. check_field f (fun csup i ->
  873. List.find (fun (t,f2) ->
  874. same_overload_args f.cf_type (apply_params csup.cl_params params t) f f2
  875. ) overloads
  876. ) true
  877. ) (f :: f.cf_overloads)
  878. end else
  879. check_field f (fun csup i ->
  880. let _, t, f2 = raw_class_field (fun f -> f.cf_type) csup params i in
  881. t, f2) false
  882. ) c.cl_fields
  883. let class_field_no_interf c i =
  884. try
  885. let f = PMap.find i c.cl_fields in
  886. f.cf_type , f
  887. with Not_found ->
  888. match c.cl_super with
  889. | None ->
  890. raise Not_found
  891. | Some (c,tl) ->
  892. (* rec over class_field *)
  893. let _, t , f = raw_class_field (fun f -> f.cf_type) c tl i in
  894. apply_params c.cl_params tl t , f
  895. let rec check_interface ctx c intf params =
  896. let p = c.cl_pos in
  897. let rec check_field i f =
  898. (if ctx.com.config.pf_overload then
  899. List.iter (function
  900. | f2 when f != f2 ->
  901. check_field i f2
  902. | _ -> ()) f.cf_overloads);
  903. let is_overload = ref false in
  904. try
  905. let t2, f2 = class_field_no_interf c i in
  906. let t2, f2 =
  907. if ctx.com.config.pf_overload && (f2.cf_overloads <> [] || Meta.has Meta.Overload f2.cf_meta) then
  908. let overloads = get_overloads c i in
  909. is_overload := true;
  910. let t = (apply_params intf.cl_params params f.cf_type) in
  911. List.find (fun (t1,f1) -> same_overload_args t t1 f f1) overloads
  912. else
  913. t2, f2
  914. in
  915. ignore(follow f2.cf_type); (* force evaluation *)
  916. let p = (match f2.cf_expr with None -> p | Some e -> e.epos) in
  917. let mkind = function
  918. | MethNormal | MethInline -> 0
  919. | MethDynamic -> 1
  920. | MethMacro -> 2
  921. in
  922. if f.cf_public && not f2.cf_public && not (Meta.has Meta.CompilerGenerated f.cf_meta) then
  923. display_error ctx ("Field " ^ i ^ " should be public as requested by " ^ s_type_path intf.cl_path) p
  924. else if not (unify_kind f2.cf_kind f.cf_kind) || not (match f.cf_kind, f2.cf_kind with Var _ , Var _ -> true | Method m1, Method m2 -> mkind m1 = mkind m2 | _ -> false) then
  925. display_error ctx ("Field " ^ i ^ " has different property access than in " ^ s_type_path intf.cl_path ^ " (" ^ s_kind f2.cf_kind ^ " should be " ^ s_kind f.cf_kind ^ ")") p
  926. else try
  927. valid_redefinition ctx f2 t2 f (apply_params intf.cl_params params f.cf_type)
  928. with
  929. Unify_error l ->
  930. if not (Meta.has Meta.CsNative c.cl_meta && c.cl_extern) then begin
  931. display_error ctx ("Field " ^ i ^ " has different type than in " ^ s_type_path intf.cl_path) p;
  932. display_error ctx (error_msg (Unify l)) p;
  933. end
  934. with
  935. | Not_found when not c.cl_interface ->
  936. let msg = if !is_overload then
  937. let ctx = print_context() in
  938. let args = match follow f.cf_type with | TFun(args,_) -> String.concat ", " (List.map (fun (n,o,t) -> (if o then "?" else "") ^ n ^ " : " ^ (s_type ctx t)) args) | _ -> assert false in
  939. "No suitable overload for " ^ i ^ "( " ^ args ^ " ), as needed by " ^ s_type_path intf.cl_path ^ " was found"
  940. else
  941. ("Field " ^ i ^ " needed by " ^ s_type_path intf.cl_path ^ " is missing")
  942. in
  943. display_error ctx msg p
  944. | Not_found -> ()
  945. in
  946. PMap.iter check_field intf.cl_fields;
  947. List.iter (fun (i2,p2) ->
  948. check_interface ctx c i2 (List.map (apply_params intf.cl_params params) p2)
  949. ) intf.cl_implements
  950. let check_interfaces ctx c =
  951. match c.cl_path with
  952. | "Proxy" :: _ , _ -> ()
  953. | _ when c.cl_extern && Meta.has Meta.CsNative c.cl_meta -> ()
  954. | _ ->
  955. List.iter (fun (intf,params) -> check_interface ctx c intf params) c.cl_implements
  956. let rec return_flow ctx e =
  957. let error() = display_error ctx "A return is missing here" e.epos; raise Exit in
  958. let return_flow = return_flow ctx in
  959. match e.eexpr with
  960. | TReturn _ | TThrow _ -> ()
  961. | TParenthesis e | TMeta(_,e) ->
  962. return_flow e
  963. | TBlock el ->
  964. let rec loop = function
  965. | [] -> error()
  966. | [e] -> return_flow e
  967. | { eexpr = TReturn _ } :: _ | { eexpr = TThrow _ } :: _ -> ()
  968. | _ :: l -> loop l
  969. in
  970. loop el
  971. | TIf (_,e1,Some e2) ->
  972. return_flow e1;
  973. return_flow e2;
  974. | TSwitch (v,cases,Some e) ->
  975. List.iter (fun (_,e) -> return_flow e) cases;
  976. return_flow e
  977. | TSwitch ({eexpr = TMeta((Meta.Exhaustive,_,_),_)},cases,None) ->
  978. List.iter (fun (_,e) -> return_flow e) cases;
  979. | TTry (e,cases) ->
  980. return_flow e;
  981. List.iter (fun (_,e) -> return_flow e) cases;
  982. | TWhile({eexpr = (TConst (TBool true))},e,_) ->
  983. (* a special case for "inifite" while loops that have no break *)
  984. let rec loop e = match e.eexpr with
  985. (* ignore nested loops to not accidentally get one of its breaks *)
  986. | TWhile _ | TFor _ -> ()
  987. | TBreak -> error()
  988. | _ -> Type.iter loop e
  989. in
  990. loop e
  991. | _ ->
  992. error()
  993. (* ---------------------------------------------------------------------- *)
  994. (* PASS 1 & 2 : Module and Class Structure *)
  995. let is_generic_parameter ctx c =
  996. (* first check field parameters, then class parameters *)
  997. try
  998. ignore (List.assoc (snd c.cl_path) ctx.curfield.cf_params);
  999. Meta.has Meta.Generic ctx.curfield.cf_meta
  1000. with Not_found -> try
  1001. ignore(List.assoc (snd c.cl_path) ctx.type_params);
  1002. (match ctx.curclass.cl_kind with | KGeneric -> true | _ -> false);
  1003. with Not_found ->
  1004. false
  1005. let check_extends ctx c t p = match follow t with
  1006. | TInst ({ cl_path = [],"Array"; cl_extern = basic_extern },_)
  1007. | TInst ({ cl_path = [],"String"; cl_extern = basic_extern },_)
  1008. | TInst ({ cl_path = [],"Date"; cl_extern = basic_extern },_)
  1009. | TInst ({ cl_path = [],"Xml"; cl_extern = basic_extern },_) when not (c.cl_extern && basic_extern) ->
  1010. error "Cannot extend basic class" p;
  1011. | TInst (csup,params) ->
  1012. if is_parent c csup then error "Recursive class" p;
  1013. begin match csup.cl_kind with
  1014. | KTypeParameter _ when not (is_generic_parameter ctx csup) -> error "Cannot extend non-generic type parameters" p
  1015. | _ -> csup,params
  1016. end
  1017. | _ -> error "Should extend by using a class" p
  1018. let type_function_arg_value ctx t c =
  1019. match c with
  1020. | None -> None
  1021. | Some e ->
  1022. let p = pos e in
  1023. let e = ctx.g.do_optimize ctx (type_expr ctx e (WithType t)) in
  1024. unify ctx e.etype t p;
  1025. let rec loop e = match e.eexpr with
  1026. | TConst c -> Some c
  1027. | TCast(e,None) -> loop e
  1028. | _ -> display_error ctx "Parameter default value should be constant" p; None
  1029. in
  1030. loop e
  1031. let rec add_constructor ctx c force_constructor p =
  1032. match c.cl_constructor, c.cl_super with
  1033. | None, Some ({ cl_constructor = Some cfsup } as csup,cparams) when not c.cl_extern && not (Meta.has Meta.CompilerGenerated cfsup.cf_meta) ->
  1034. let cf = {
  1035. cfsup with
  1036. cf_pos = p;
  1037. cf_meta = [];
  1038. cf_doc = None;
  1039. cf_expr = None;
  1040. } in
  1041. let r = exc_protect ctx (fun r ->
  1042. let t = mk_mono() in
  1043. r := (fun() -> t);
  1044. let ctx = { ctx with
  1045. curfield = cf;
  1046. pass = PTypeField;
  1047. } in
  1048. ignore (follow cfsup.cf_type); (* make sure it's typed *)
  1049. (if ctx.com.config.pf_overload then List.iter (fun cf -> ignore (follow cf.cf_type)) cf.cf_overloads);
  1050. let map_arg (v,def) =
  1051. (*
  1052. let's optimize a bit the output by not always copying the default value
  1053. into the inherited constructor when it's not necessary for the platform
  1054. *)
  1055. match ctx.com.platform, def with
  1056. | _, Some _ when not ctx.com.config.pf_static -> v, (Some TNull)
  1057. | Flash, Some (TString _) -> v, (Some TNull)
  1058. | Cpp, Some (TString _) -> v, def
  1059. | Cpp, Some _ -> { v with v_type = ctx.t.tnull v.v_type }, (Some TNull)
  1060. | _ -> v, def
  1061. in
  1062. let args = (match cfsup.cf_expr with
  1063. | Some { eexpr = TFunction f } ->
  1064. List.map map_arg f.tf_args
  1065. | _ ->
  1066. let values = get_value_meta cfsup.cf_meta in
  1067. match follow cfsup.cf_type with
  1068. | TFun (args,_) ->
  1069. List.map (fun (n,o,t) ->
  1070. let def = try type_function_arg_value ctx t (Some (PMap.find n values)) with Not_found -> if o then Some TNull else None in
  1071. map_arg (alloc_var n (if o then ctx.t.tnull t else t),def)
  1072. ) args
  1073. | _ -> assert false
  1074. ) in
  1075. let p = c.cl_pos in
  1076. let vars = List.map (fun (v,def) -> alloc_var v.v_name (apply_params csup.cl_params cparams v.v_type), def) args in
  1077. let super_call = mk (TCall (mk (TConst TSuper) (TInst (csup,cparams)) p,List.map (fun (v,_) -> mk (TLocal v) v.v_type p) vars)) ctx.t.tvoid p in
  1078. let constr = mk (TFunction {
  1079. tf_args = vars;
  1080. tf_type = ctx.t.tvoid;
  1081. tf_expr = super_call;
  1082. }) (TFun (List.map (fun (v,c) -> v.v_name, c <> None, v.v_type) vars,ctx.t.tvoid)) p in
  1083. cf.cf_expr <- Some constr;
  1084. cf.cf_type <- t;
  1085. unify ctx t constr.etype p;
  1086. t
  1087. ) "add_constructor" in
  1088. cf.cf_type <- TLazy r;
  1089. c.cl_constructor <- Some cf;
  1090. delay ctx PForce (fun() -> ignore((!r)()));
  1091. | None,_ when force_constructor ->
  1092. let constr = mk (TFunction {
  1093. tf_args = [];
  1094. tf_type = ctx.t.tvoid;
  1095. tf_expr = mk (TBlock []) ctx.t.tvoid p;
  1096. }) (tfun [] ctx.t.tvoid) p in
  1097. let cf = mk_field "new" constr.etype p in
  1098. cf.cf_expr <- Some constr;
  1099. cf.cf_type <- constr.etype;
  1100. cf.cf_meta <- [Meta.CompilerGenerated,[],p];
  1101. cf.cf_kind <- Method MethNormal;
  1102. c.cl_constructor <- Some cf;
  1103. | _ ->
  1104. (* nothing to do *)
  1105. ()
  1106. let set_heritance ctx c herits p =
  1107. let ctx = { ctx with curclass = c; type_params = c.cl_params; } in
  1108. let old_meta = c.cl_meta in
  1109. let process_meta csup =
  1110. List.iter (fun m ->
  1111. match m with
  1112. | Meta.Final, _, _ -> if not (Meta.has Meta.Hack c.cl_meta || (match c.cl_kind with KTypeParameter _ -> true | _ -> false)) then error "Cannot extend a final class" p;
  1113. | Meta.AutoBuild, el, p -> c.cl_meta <- (Meta.Build,el,p) :: m :: c.cl_meta
  1114. | _ -> ()
  1115. ) csup.cl_meta
  1116. in
  1117. let cancel_build csup =
  1118. (* for macros reason, our super class is not yet built - see #2177 *)
  1119. (* let's reset our build and delay it until we are done *)
  1120. c.cl_meta <- old_meta;
  1121. c.cl_array_access <- None;
  1122. c.cl_dynamic <- None;
  1123. c.cl_implements <- [];
  1124. c.cl_super <- None;
  1125. raise Exit
  1126. in
  1127. let has_interf = ref false in
  1128. let rec loop = function
  1129. | HPrivate | HExtern | HInterface ->
  1130. ()
  1131. | HExtends t ->
  1132. if c.cl_super <> None then error "Cannot extend several classes" p;
  1133. let t = load_instance ctx t p false in
  1134. let csup,params = check_extends ctx c t p in
  1135. if not (csup.cl_build()) then cancel_build csup;
  1136. process_meta csup;
  1137. if c.cl_interface then begin
  1138. if not csup.cl_interface then error "Cannot extend by using a class" p;
  1139. c.cl_implements <- (csup,params) :: c.cl_implements;
  1140. if not !has_interf then begin
  1141. delay ctx PForce (fun() -> check_interfaces ctx c);
  1142. has_interf := true;
  1143. end
  1144. end else begin
  1145. if csup.cl_interface then error "Cannot extend by using an interface" p;
  1146. c.cl_super <- Some (csup,params)
  1147. end
  1148. | HImplements t ->
  1149. let t = load_instance ctx t p false in
  1150. (match follow t with
  1151. | TInst ({ cl_path = [],"ArrayAccess"; cl_extern = true; },[t]) ->
  1152. if c.cl_array_access <> None then error "Duplicate array access" p;
  1153. c.cl_array_access <- Some t
  1154. | TInst (intf,params) ->
  1155. if not (intf.cl_build()) then cancel_build intf;
  1156. if is_parent c intf then error "Recursive class" p;
  1157. if c.cl_interface then error "Interfaces cannot implement another interface (use extends instead)" p;
  1158. if not intf.cl_interface then error "You can only implement an interface" p;
  1159. process_meta intf;
  1160. c.cl_implements <- (intf, params) :: c.cl_implements;
  1161. if not !has_interf && not (Meta.has (Meta.Custom "$do_not_check_interf") c.cl_meta) then begin
  1162. delay ctx PForce (fun() -> check_interfaces ctx c);
  1163. has_interf := true;
  1164. end
  1165. | TDynamic t ->
  1166. if c.cl_dynamic <> None then error "Cannot have several dynamics" p;
  1167. c.cl_dynamic <- Some t
  1168. | _ -> error "Should implement by using an interface" p)
  1169. in
  1170. (*
  1171. resolve imports before calling build_inheritance, since it requires full paths.
  1172. that means that typedefs are not working, but that's a fair limitation
  1173. *)
  1174. let rec resolve_imports t =
  1175. match t.tpackage with
  1176. | _ :: _ -> t
  1177. | [] ->
  1178. try
  1179. let find = List.find (fun lt -> snd (t_path lt) = t.tname) in
  1180. let lt = try find ctx.m.curmod.m_types with Not_found -> find ctx.m.module_types in
  1181. { t with tpackage = fst (t_path lt) }
  1182. with
  1183. Not_found -> t
  1184. in
  1185. let herits = List.map (function
  1186. | HExtends t -> HExtends (resolve_imports t)
  1187. | HImplements t -> HImplements (resolve_imports t)
  1188. | h -> h
  1189. ) herits in
  1190. List.iter loop (List.filter (ctx.g.do_inherit ctx c p) herits)
  1191. let rec type_type_params ?(enum_constructor=false) ctx path get_params p tp =
  1192. let n = tp.tp_name in
  1193. let c = mk_class ctx.m.curmod (fst path @ [snd path],n) p in
  1194. c.cl_params <- List.map (type_type_params ctx c.cl_path get_params p) tp.tp_params;
  1195. c.cl_kind <- KTypeParameter [];
  1196. if enum_constructor then c.cl_meta <- (Meta.EnumConstructorParam,[],c.cl_pos) :: c.cl_meta;
  1197. let t = TInst (c,List.map snd c.cl_params) in
  1198. match tp.tp_constraints with
  1199. | [] ->
  1200. n, t
  1201. | _ ->
  1202. let r = exc_protect ctx (fun r ->
  1203. r := (fun _ -> t);
  1204. let ctx = { ctx with type_params = ctx.type_params @ get_params() } in
  1205. let constr = List.map (load_complex_type ctx p) tp.tp_constraints in
  1206. (* check against direct recursion *)
  1207. let rec loop t =
  1208. match follow t with
  1209. | TInst (c2,_) when c == c2 -> error "Recursive constraint parameter is not allowed" p
  1210. | TInst ({ cl_kind = KTypeParameter cl },_) ->
  1211. List.iter loop cl
  1212. | _ ->
  1213. ()
  1214. in
  1215. List.iter loop constr;
  1216. c.cl_kind <- KTypeParameter constr;
  1217. t
  1218. ) "constraint" in
  1219. delay ctx PForce (fun () -> ignore(!r()));
  1220. n, TLazy r
  1221. let type_function_params ctx fd fname p =
  1222. let params = ref [] in
  1223. params := List.map (fun tp ->
  1224. type_type_params ctx ([],fname) (fun() -> !params) p tp
  1225. ) fd.f_params;
  1226. !params
  1227. let find_enclosing com e =
  1228. let display_pos = ref (!Parser.resume_display) in
  1229. let mk_null p = (EDisplay(((EConst(Ident "null")),p),false),p) in
  1230. let encloses_display_pos p =
  1231. if p.pmin <= !display_pos.pmin && p.pmax >= !display_pos.pmax then begin
  1232. let p = !display_pos in
  1233. display_pos := { pfile = ""; pmin = -2; pmax = -2 };
  1234. Some p
  1235. end else
  1236. None
  1237. in
  1238. let rec loop e = match fst e with
  1239. | EBlock el ->
  1240. let p = pos e in
  1241. (* We want to find the innermost block which contains the display position. *)
  1242. let el = List.map loop el in
  1243. let el = match encloses_display_pos p with
  1244. | None ->
  1245. el
  1246. | Some p2 ->
  1247. let b,el = List.fold_left (fun (b,el) e ->
  1248. let p = pos e in
  1249. if b || p.pmax <= p2.pmin then begin
  1250. (b,e :: el)
  1251. end else begin
  1252. let e_d = (EDisplay(mk_null p,false)),p in
  1253. (true,e :: e_d :: el)
  1254. end
  1255. ) (false,[]) el in
  1256. let el = if b then
  1257. el
  1258. else begin
  1259. mk_null p :: el
  1260. end in
  1261. List.rev el
  1262. in
  1263. (EBlock el),(pos e)
  1264. | _ ->
  1265. Ast.map_expr loop e
  1266. in
  1267. loop e
  1268. let find_before_pos com e =
  1269. let display_pos = ref (!Parser.resume_display) in
  1270. let is_annotated p =
  1271. if p.pmax = !display_pos.pmin - 1 then begin
  1272. display_pos := { pfile = ""; pmin = -2; pmax = -2 };
  1273. true
  1274. end else
  1275. false
  1276. in
  1277. let rec loop e =
  1278. if is_annotated (pos e) then
  1279. (EDisplay(e,false),(pos e))
  1280. else
  1281. e
  1282. in
  1283. let rec map e =
  1284. loop (Ast.map_expr map e)
  1285. in
  1286. map e
  1287. let type_function ctx args ret fmode f do_display p =
  1288. let locals = save_locals ctx in
  1289. let fargs = List.map (fun (n,c,t) ->
  1290. if n.[0] = '$' then error "Function argument names starting with a dollar are not allowed" p;
  1291. let c = type_function_arg_value ctx t c in
  1292. let v,c = add_local ctx n t, c in
  1293. if n = "this" then v.v_meta <- (Meta.This,[],p) :: v.v_meta;
  1294. v,c
  1295. ) args in
  1296. let old_ret = ctx.ret in
  1297. let old_fun = ctx.curfun in
  1298. let old_opened = ctx.opened in
  1299. ctx.curfun <- fmode;
  1300. ctx.ret <- ret;
  1301. ctx.opened <- [];
  1302. let e = match f.f_expr with None -> error "Function body required" p | Some e -> e in
  1303. let e = if not do_display then
  1304. type_expr ctx e NoValue
  1305. else begin
  1306. let e = match ctx.com.display with
  1307. | DMToplevel -> find_enclosing ctx.com e
  1308. | DMPosition | DMUsage | DMType -> find_before_pos ctx.com e
  1309. | _ -> e
  1310. in
  1311. try
  1312. if Common.defined ctx.com Define.NoCOpt then raise Exit;
  1313. type_expr ctx (Optimizer.optimize_completion_expr e) NoValue
  1314. with
  1315. | Parser.TypePath (_,None,_) | Exit ->
  1316. type_expr ctx e NoValue
  1317. | DisplayTypes [t] when (match follow t with TMono _ -> true | _ -> false) ->
  1318. type_expr ctx (if ctx.com.display = DMToplevel then find_enclosing ctx.com e else e) NoValue
  1319. end in
  1320. let e = match e.eexpr with
  1321. | TMeta((Meta.MergeBlock,_,_), ({eexpr = TBlock el} as e1)) -> e1
  1322. | _ -> e
  1323. in
  1324. let rec loop e =
  1325. match e.eexpr with
  1326. | TReturn (Some e) -> (match follow e.etype with TAbstract({a_path = [],"Void"},[]) -> () | _ -> raise Exit)
  1327. | TFunction _ -> ()
  1328. | _ -> Type.iter loop e
  1329. in
  1330. let have_ret = (try loop e; false with Exit -> true) in
  1331. if have_ret then
  1332. (try return_flow ctx e with Exit -> ())
  1333. else (try type_eq EqStrict ret ctx.t.tvoid with Unify_error _ ->
  1334. match e.eexpr with
  1335. (* accept final throw (issue #1923) *)
  1336. | TThrow _ -> ()
  1337. | TBlock el when (match List.rev el with ({eexpr = TThrow _} :: _) -> true | _ -> false) -> ()
  1338. | _ -> display_error ctx ("Missing return " ^ (s_type (print_context()) ret)) p);
  1339. let rec loop e =
  1340. match e.eexpr with
  1341. | TCall ({ eexpr = TConst TSuper },_) -> raise Exit
  1342. | TFunction _ -> ()
  1343. | _ -> Type.iter loop e
  1344. in
  1345. let has_super_constr() =
  1346. match ctx.curclass.cl_super with
  1347. | None ->
  1348. None
  1349. | Some (csup,tl) ->
  1350. try
  1351. let _,cf = get_constructor (fun f->f.cf_type) csup in
  1352. Some (Meta.has Meta.CompilerGenerated cf.cf_meta,TInst(csup,tl))
  1353. with Not_found ->
  1354. None
  1355. in
  1356. let e = if fmode <> FunConstructor then
  1357. e
  1358. else match has_super_constr() with
  1359. | Some (was_forced,t_super) ->
  1360. (try
  1361. loop e;
  1362. if was_forced then
  1363. let e_super = mk (TConst TSuper) t_super e.epos in
  1364. let e_super_call = mk (TCall(e_super,[])) ctx.t.tvoid e.epos in
  1365. concat e_super_call e
  1366. else begin
  1367. display_error ctx "Missing super constructor call" p;
  1368. e
  1369. end
  1370. with
  1371. Exit -> e);
  1372. | None ->
  1373. e
  1374. in
  1375. locals();
  1376. let e = match ctx.curfun, ctx.vthis with
  1377. | (FunMember|FunConstructor), Some v ->
  1378. let ev = mk (TVar (v,Some (mk (TConst TThis) ctx.tthis p))) ctx.t.tvoid p in
  1379. (match e.eexpr with
  1380. | TBlock l -> { e with eexpr = TBlock (ev::l) }
  1381. | _ -> mk (TBlock [ev;e]) e.etype p)
  1382. | _ -> e
  1383. in
  1384. List.iter (fun r -> r := Closed) ctx.opened;
  1385. ctx.ret <- old_ret;
  1386. ctx.curfun <- old_fun;
  1387. ctx.opened <- old_opened;
  1388. e , fargs
  1389. let load_core_class ctx c =
  1390. let ctx2 = (match ctx.g.core_api with
  1391. | None ->
  1392. let com2 = Common.clone ctx.com in
  1393. com2.defines <- PMap.empty;
  1394. Common.define com2 Define.CoreApi;
  1395. Common.define com2 Define.Sys;
  1396. if ctx.in_macro then Common.define com2 Define.Macro;
  1397. com2.class_path <- ctx.com.std_path;
  1398. let ctx2 = ctx.g.do_create com2 in
  1399. ctx.g.core_api <- Some ctx2;
  1400. ctx2
  1401. | Some c ->
  1402. c
  1403. ) in
  1404. let tpath = match c.cl_kind with
  1405. | KAbstractImpl a -> { tpackage = fst a.a_path; tname = snd a.a_path; tparams = []; tsub = None; }
  1406. | _ -> { tpackage = fst c.cl_path; tname = snd c.cl_path; tparams = []; tsub = None; }
  1407. in
  1408. let t = load_instance ctx2 tpath c.cl_pos true in
  1409. flush_pass ctx2 PFinal "core_final";
  1410. match t with
  1411. | TInst (ccore,_) | TAbstract({a_impl = Some ccore}, _) ->
  1412. ccore
  1413. | _ ->
  1414. assert false
  1415. let init_core_api ctx c =
  1416. let ccore = load_core_class ctx c in
  1417. begin try
  1418. List.iter2 (fun (n1,t1) (n2,t2) -> match follow t1, follow t2 with
  1419. | TInst({cl_kind = KTypeParameter l1},_),TInst({cl_kind = KTypeParameter l2},_) ->
  1420. begin try
  1421. List.iter2 (fun t1 t2 -> type_eq EqCoreType t2 t1) l1 l2
  1422. with
  1423. | Invalid_argument _ ->
  1424. error "Type parameters must have the same number of constraints as core type" c.cl_pos
  1425. | Unify_error l ->
  1426. display_error ctx ("Type parameter " ^ n2 ^ " has different constraint than in core type") c.cl_pos;
  1427. display_error ctx (error_msg (Unify l)) c.cl_pos
  1428. end
  1429. | t1,t2 ->
  1430. Printf.printf "%s %s" (s_type (print_context()) t1) (s_type (print_context()) t2);
  1431. assert false
  1432. ) ccore.cl_params c.cl_params;
  1433. with Invalid_argument _ ->
  1434. error "Class must have the same number of type parameters as core type" c.cl_pos
  1435. end;
  1436. (match c.cl_doc with
  1437. | None -> c.cl_doc <- ccore.cl_doc
  1438. | Some _ -> ());
  1439. let compare_fields f f2 =
  1440. let p = (match f2.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  1441. (try
  1442. type_eq EqCoreType (apply_params ccore.cl_params (List.map snd c.cl_params) f.cf_type) f2.cf_type
  1443. with Unify_error l ->
  1444. display_error ctx ("Field " ^ f.cf_name ^ " has different type than in core type") p;
  1445. display_error ctx (error_msg (Unify l)) p);
  1446. if f2.cf_public <> f.cf_public then error ("Field " ^ f.cf_name ^ " has different visibility than core type") p;
  1447. (match f2.cf_doc with
  1448. | None -> f2.cf_doc <- f.cf_doc
  1449. | Some _ -> ());
  1450. if f2.cf_kind <> f.cf_kind then begin
  1451. match f2.cf_kind, f.cf_kind with
  1452. | Method MethInline, Method MethNormal -> () (* allow to add 'inline' *)
  1453. | Method MethNormal, Method MethInline -> () (* allow to disable 'inline' *)
  1454. | _ ->
  1455. error ("Field " ^ f.cf_name ^ " has different property access than core type") p;
  1456. end;
  1457. (match follow f.cf_type, follow f2.cf_type with
  1458. | TFun (pl1,_), TFun (pl2,_) ->
  1459. if List.length pl1 != List.length pl2 then error "Argument count mismatch" p;
  1460. List.iter2 (fun (n1,_,_) (n2,_,_) ->
  1461. if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
  1462. ) pl1 pl2;
  1463. | _ -> ());
  1464. in
  1465. let check_fields fcore fl =
  1466. PMap.iter (fun i f ->
  1467. if not f.cf_public then () else
  1468. let f2 = try PMap.find f.cf_name fl with Not_found -> error ("Missing field " ^ i ^ " required by core type") c.cl_pos in
  1469. compare_fields f f2;
  1470. ) fcore;
  1471. PMap.iter (fun i f ->
  1472. let p = (match f.cf_expr with None -> c.cl_pos | Some e -> e.epos) in
  1473. if f.cf_public && not (Meta.has Meta.Hack f.cf_meta) && not (PMap.mem f.cf_name fcore) && not (List.memq f c.cl_overrides) then error ("Public field " ^ i ^ " is not part of core type") p;
  1474. ) fl;
  1475. in
  1476. check_fields ccore.cl_fields c.cl_fields;
  1477. check_fields ccore.cl_statics c.cl_statics;
  1478. (match ccore.cl_constructor, c.cl_constructor with
  1479. | None, None -> ()
  1480. | Some { cf_public = false }, _ -> ()
  1481. | Some f, Some f2 -> compare_fields f f2
  1482. | None, Some { cf_public = false } -> ()
  1483. | _ -> error "Constructor differs from core type" c.cl_pos)
  1484. let check_global_metadata ctx f_add mpath tpath so =
  1485. let sl1 = if mpath = tpath then
  1486. (fst tpath) @ [snd tpath]
  1487. else
  1488. (fst mpath) @ [snd mpath;snd tpath]
  1489. in
  1490. let sl1,field_mode = match so with None -> sl1,false | Some s -> sl1 @ [s],true in
  1491. List.iter (fun (sl2,m,(recursive,to_types,to_fields)) ->
  1492. let rec loop sl1 sl2 = match sl1,sl2 with
  1493. | [],[] ->
  1494. true
  1495. (* always recurse into types of package paths *)
  1496. | (s1 :: s11 :: _),[s2] when is_lower_ident s2 && not (is_lower_ident s11)->
  1497. s1 = s2
  1498. | [_],[""] ->
  1499. true
  1500. | _,([] | [""]) ->
  1501. recursive
  1502. | [],_ ->
  1503. false
  1504. | (s1 :: sl1),(s2 :: sl2) ->
  1505. s1 = s2 && loop sl1 sl2
  1506. in
  1507. let add = ((field_mode && to_fields) || (not field_mode && to_types)) && (loop sl1 sl2) in
  1508. if add then f_add m
  1509. ) ctx.g.global_metadata
  1510. let patch_class ctx c fields =
  1511. let path = match c.cl_kind with
  1512. | KAbstractImpl a -> a.a_path
  1513. | _ -> c.cl_path
  1514. in
  1515. let h = (try Some (Hashtbl.find ctx.g.type_patches path) with Not_found -> None) in
  1516. match h with
  1517. | None -> fields
  1518. | Some (h,hcl) ->
  1519. c.cl_meta <- c.cl_meta @ hcl.tp_meta;
  1520. let rec loop acc = function
  1521. | [] -> acc
  1522. | f :: l ->
  1523. (* patch arguments types *)
  1524. (match f.cff_kind with
  1525. | FFun ff ->
  1526. let param ((n,opt,t,e) as p) =
  1527. try
  1528. let t2 = (try Hashtbl.find h (("$" ^ f.cff_name ^ "__" ^ n),false) with Not_found -> Hashtbl.find h (("$" ^ n),false)) in
  1529. n, opt, t2.tp_type, e
  1530. with Not_found ->
  1531. p
  1532. in
  1533. f.cff_kind <- FFun { ff with f_args = List.map param ff.f_args }
  1534. | _ -> ());
  1535. (* other patches *)
  1536. match (try Some (Hashtbl.find h (f.cff_name,List.mem AStatic f.cff_access)) with Not_found -> None) with
  1537. | None -> loop (f :: acc) l
  1538. | Some { tp_remove = true } -> loop acc l
  1539. | Some p ->
  1540. f.cff_meta <- f.cff_meta @ p.tp_meta;
  1541. (match p.tp_type with
  1542. | None -> ()
  1543. | Some t ->
  1544. f.cff_kind <- match f.cff_kind with
  1545. | FVar (_,e) -> FVar (Some t,e)
  1546. | FProp (get,set,_,eo) -> FProp (get,set,Some t,eo)
  1547. | FFun f -> FFun { f with f_type = Some t });
  1548. loop (f :: acc) l
  1549. in
  1550. List.rev (loop [] fields)
  1551. let rec string_list_of_expr_path_raise (e,p) =
  1552. match e with
  1553. | EConst (Ident i) -> [i]
  1554. | EField (e,f) -> f :: string_list_of_expr_path_raise e
  1555. | _ -> raise Exit
  1556. let string_list_of_expr_path (e,p) =
  1557. try string_list_of_expr_path_raise (e,p)
  1558. with Exit -> error "Invalid path" p
  1559. let build_enum_abstract ctx c a fields p =
  1560. List.iter (fun field ->
  1561. match field.cff_kind with
  1562. | FVar(ct,eo) when not (List.mem AStatic field.cff_access) ->
  1563. field.cff_access <- [AStatic;APublic;AInline];
  1564. field.cff_meta <- (Meta.Enum,[],field.cff_pos) :: (Meta.Impl,[],field.cff_pos) :: field.cff_meta;
  1565. let e = match eo with
  1566. | None -> error "Value required" field.cff_pos
  1567. | Some e -> (ECast(e,None),field.cff_pos)
  1568. in
  1569. field.cff_kind <- FVar(ct,Some e)
  1570. | _ ->
  1571. ()
  1572. ) fields;
  1573. EVars ["",Some (CTAnonymous fields),None],p
  1574. let build_module_def ctx mt meta fvars context_init fbuild =
  1575. let rec loop = function
  1576. | (Meta.Build,args,p) :: l ->
  1577. let epath, el = (match args with
  1578. | [ECall (epath,el),p] -> epath, el
  1579. | _ -> error "Invalid build parameters" p
  1580. ) in
  1581. let s = try String.concat "." (List.rev (string_list_of_expr_path epath)) with Error (_,p) -> error "Build call parameter must be a class path" p in
  1582. if ctx.in_macro then error "You cannot use @:build inside a macro : make sure that your enum is not used in macro" p;
  1583. let old = ctx.g.get_build_infos in
  1584. ctx.g.get_build_infos <- (fun() -> Some (mt, List.map snd (t_infos mt).mt_params, fvars()));
  1585. context_init();
  1586. let r = try apply_macro ctx MBuild s el p with e -> ctx.g.get_build_infos <- old; raise e in
  1587. ctx.g.get_build_infos <- old;
  1588. (match r with
  1589. | None -> error "Build failure" p
  1590. | Some e -> fbuild e; loop l)
  1591. | (Meta.Enum,_,p) :: l ->
  1592. begin match mt with
  1593. | TClassDecl ({cl_kind = KAbstractImpl a} as c) ->
  1594. context_init();
  1595. let e = build_enum_abstract ctx c a (fvars()) p in
  1596. fbuild e;
  1597. loop l
  1598. | _ ->
  1599. loop l
  1600. end
  1601. | _ :: l -> loop l
  1602. | [] -> ()
  1603. in
  1604. (* let errors go through to prevent resume if build fails *)
  1605. loop meta
  1606. let init_class ctx c p context_init herits fields =
  1607. let ctx = {
  1608. ctx with
  1609. curclass = c;
  1610. type_params = c.cl_params;
  1611. pass = PBuildClass;
  1612. tthis = (match c.cl_kind with
  1613. | KAbstractImpl a ->
  1614. (match a.a_this with
  1615. | TMono r when !r = None -> TAbstract (a,List.map snd c.cl_params)
  1616. | t -> t)
  1617. | _ -> TInst (c,List.map snd c.cl_params));
  1618. on_error = (fun ctx msg ep ->
  1619. ctx.com.error msg ep;
  1620. (* macros expressions might reference other code, let's recall which class we are actually compiling *)
  1621. if !locate_macro_error && (ep.pfile <> c.cl_pos.pfile || ep.pmax < c.cl_pos.pmin || ep.pmin > c.cl_pos.pmax) then ctx.com.error "Defined in this class" c.cl_pos
  1622. );
  1623. } in
  1624. locate_macro_error := true;
  1625. incr stats.s_classes_built;
  1626. let fields = patch_class ctx c fields in
  1627. let fields = ref fields in
  1628. let get_fields() = !fields in
  1629. build_module_def ctx (TClassDecl c) c.cl_meta get_fields context_init (fun (e,p) ->
  1630. match e with
  1631. | EVars [_,Some (CTAnonymous f),None] ->
  1632. List.iter (fun f ->
  1633. if List.mem AMacro f.cff_access then
  1634. (match ctx.g.macros with
  1635. | Some (_,mctx) when Hashtbl.mem mctx.g.types_module c.cl_path ->
  1636. (* assume that if we had already a macro with the same name, it has not been changed during the @:build operation *)
  1637. if not (List.exists (fun f2 -> f2.cff_name = f.cff_name && List.mem AMacro f2.cff_access) (!fields)) then
  1638. error "Class build macro cannot return a macro function when the class has already been compiled into the macro context" p
  1639. | _ -> ())
  1640. ) f;
  1641. fields := f
  1642. | _ -> error "Class build macro must return a single variable with anonymous fields" p
  1643. );
  1644. let fields = !fields in
  1645. let core_api = Meta.has Meta.CoreApi c.cl_meta in
  1646. let is_class_macro = Meta.has Meta.Macro c.cl_meta in
  1647. if is_class_macro then display_error ctx "Macro classes are no longer allowed in haxe 3" p;
  1648. let fields, herits = if is_class_macro && not ctx.in_macro then begin
  1649. c.cl_extern <- true;
  1650. List.filter (fun f -> List.mem AStatic f.cff_access) fields, []
  1651. end else fields, herits in
  1652. if core_api && ctx.com.display = DMNone then delay ctx PForce (fun() -> init_core_api ctx c);
  1653. let rec extends_public c =
  1654. Meta.has Meta.PublicFields c.cl_meta ||
  1655. match c.cl_super with
  1656. | None -> false
  1657. | Some (c,_) -> extends_public c
  1658. in
  1659. let extends_public = extends_public c in
  1660. let is_public access parent =
  1661. if List.mem APrivate access then
  1662. false
  1663. else if List.mem APublic access then
  1664. true
  1665. else match parent with
  1666. | Some { cf_public = p } -> p
  1667. | _ -> c.cl_extern || c.cl_interface || extends_public
  1668. in
  1669. let rec get_parent c name =
  1670. match c.cl_super with
  1671. | None -> None
  1672. | Some (csup,_) ->
  1673. try
  1674. Some (PMap.find name csup.cl_fields)
  1675. with
  1676. Not_found -> get_parent csup name
  1677. in
  1678. let type_opt ctx p t =
  1679. match t with
  1680. | None when c.cl_extern || c.cl_interface ->
  1681. display_error ctx "Type required for extern classes and interfaces" p;
  1682. t_dynamic
  1683. | None when core_api ->
  1684. display_error ctx "Type required for core api classes" p;
  1685. t_dynamic
  1686. | _ ->
  1687. load_type_opt ctx p t
  1688. in
  1689. let rec has_field f = function
  1690. | None -> false
  1691. | Some (c,_) ->
  1692. PMap.exists f c.cl_fields || has_field f c.cl_super || List.exists (fun i -> has_field f (Some i)) c.cl_implements
  1693. in
  1694. let rec get_declared f = function
  1695. | None -> None
  1696. | Some (c,a) when PMap.exists f c.cl_fields ->
  1697. Some (c,a)
  1698. | Some (c,_) ->
  1699. let ret = get_declared f c.cl_super in
  1700. match ret with
  1701. | Some r -> Some r
  1702. | None ->
  1703. let rec loop ifaces = match ifaces with
  1704. | [] -> None
  1705. | i :: ifaces -> match get_declared f (Some i) with
  1706. | Some r -> Some r
  1707. | None -> loop ifaces
  1708. in
  1709. loop c.cl_implements
  1710. in
  1711. (match c.cl_super with None -> () | Some _ -> delay ctx PForce (fun() -> check_overriding ctx c));
  1712. if ctx.com.config.pf_overload then delay ctx PForce (fun() -> check_overloads ctx c);
  1713. (* ----------------------- COMPLETION ----------------------------- *)
  1714. let display_file = match ctx.com.display with
  1715. | DMNone -> false
  1716. | DMResolve s ->
  1717. let mt = load_type_def ctx p {tname = s; tpackage = []; tsub = None; tparams = []} in
  1718. let p = (t_infos mt).mt_pos in
  1719. raise (DisplayPosition [p]);
  1720. | _ ->
  1721. Common.unique_full_path p.pfile = (!Parser.resume_display).pfile
  1722. in
  1723. let cp = !Parser.resume_display in
  1724. let delayed_expr = ref [] in
  1725. let rec is_full_type t =
  1726. match t with
  1727. | TFun (args,ret) -> is_full_type ret && List.for_all (fun (_,_,t) -> is_full_type t) args
  1728. | TMono r -> (match !r with None -> false | Some t -> is_full_type t)
  1729. | TAbstract _ | TInst _ | TEnum _ | TLazy _ | TDynamic _ | TAnon _ | TType _ -> true
  1730. in
  1731. let bind_type ctx cf r p macro =
  1732. if ctx.com.display <> DMNone then begin
  1733. let cp = !Parser.resume_display in
  1734. if display_file && (cp.pmin = 0 || (p.pmin <= cp.pmin && p.pmax >= cp.pmax)) then begin
  1735. if macro && not ctx.in_macro then
  1736. (* force macro system loading of this class in order to get completion *)
  1737. delay ctx PTypeField (fun() -> ignore(ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [] p))
  1738. else begin
  1739. cf.cf_type <- TLazy r;
  1740. delayed_expr := (ctx,Some r) :: !delayed_expr;
  1741. end
  1742. end else begin
  1743. if not (is_full_type cf.cf_type) then begin
  1744. delayed_expr := (ctx, None) :: !delayed_expr;
  1745. cf.cf_type <- TLazy r;
  1746. end;
  1747. end
  1748. end else if macro && not ctx.in_macro then
  1749. ()
  1750. else begin
  1751. cf.cf_type <- TLazy r;
  1752. delayed_expr := (ctx,Some r) :: !delayed_expr;
  1753. end
  1754. in
  1755. let force_constructor = ref false in
  1756. let bind_var ctx cf e stat inline =
  1757. let p = cf.cf_pos in
  1758. if not stat then begin match get_declared cf.cf_name c.cl_super with
  1759. | None -> ()
  1760. | Some (csup,_) ->
  1761. (* this can happen on -net-lib generated classes if a combination of explicit interfaces and variables with the same name happens *)
  1762. if not (csup.cl_interface && Meta.has Meta.CsNative c.cl_meta) then
  1763. error ("Redefinition of variable " ^ cf.cf_name ^ " in subclass is not allowed. Previously declared at " ^ (Ast.s_type_path csup.cl_path) ) p
  1764. end;
  1765. let t = cf.cf_type in
  1766. match e with
  1767. | None -> ()
  1768. | Some e ->
  1769. if requires_value_meta ctx.com (Some c) then cf.cf_meta <- ((Meta.Value,[e],cf.cf_pos) :: cf.cf_meta);
  1770. let check_cast e =
  1771. (* insert cast to keep explicit field type (issue #1901) *)
  1772. if type_iseq e.etype cf.cf_type then
  1773. e
  1774. else begin match e.eexpr,follow cf.cf_type with
  1775. | TConst (TInt i),TAbstract({a_path=[],"Float"},_) ->
  1776. (* turn int constant to float constant if expected type is float *)
  1777. {e with eexpr = TConst (TFloat (Int32.to_string i))}
  1778. | _ ->
  1779. mk_cast e cf.cf_type e.epos
  1780. end
  1781. in
  1782. let r = exc_protect ctx (fun r ->
  1783. (* type constant init fields (issue #1956) *)
  1784. if not !return_partial_type || (match fst e with EConst _ -> true | _ -> false) then begin
  1785. r := (fun() -> t);
  1786. context_init();
  1787. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
  1788. let e = type_var_field ctx t e stat p in
  1789. let require_constant_expression e msg = match Optimizer.make_constant_expression ctx e with
  1790. | Some e -> e
  1791. | None -> display_error ctx msg p; e
  1792. in
  1793. let e = (match cf.cf_kind with
  1794. | Var v when c.cl_extern || Meta.has Meta.Extern cf.cf_meta ->
  1795. if not stat then begin
  1796. display_error ctx "Extern non-static variables may not be initialized" p;
  1797. e
  1798. end else if v.v_read <> AccInline then begin
  1799. display_error ctx "Extern non-inline variables may not be initialized" p;
  1800. e
  1801. end else require_constant_expression e "Extern variable initialization must be a constant value"
  1802. | Var v when is_extern_field cf ->
  1803. (* disallow initialization of non-physical fields (issue #1958) *)
  1804. display_error ctx "This field cannot be initialized because it is not a real variable" p; e
  1805. | Var v when not stat ->
  1806. let e = match Optimizer.make_constant_expression ctx e with
  1807. | Some e -> e
  1808. | None ->
  1809. let rec has_this e = match e.eexpr with
  1810. | TConst TThis ->
  1811. display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
  1812. | TLocal v when (match ctx.vthis with Some v2 -> v == v2 | None -> false) ->
  1813. display_error ctx "Cannot access this or other member field in variable initialization" e.epos;
  1814. | _ ->
  1815. Type.iter has_this e
  1816. in
  1817. has_this e;
  1818. e
  1819. in
  1820. e
  1821. | Var v when v.v_read = AccInline ->
  1822. let e = require_constant_expression e "Inline variable initialization must be a constant value" in
  1823. begin match c.cl_kind with
  1824. | KAbstractImpl a when Meta.has Meta.Enum cf.cf_meta && Meta.has Meta.Enum a.a_meta ->
  1825. unify ctx t (TAbstract(a,(List.map (fun _ -> mk_mono()) a.a_params))) p;
  1826. begin match e.eexpr with
  1827. | TCast(e1,None) -> unify ctx e1.etype a.a_this e1.epos
  1828. | _ -> assert false
  1829. end
  1830. | _ ->
  1831. ()
  1832. end;
  1833. e
  1834. | _ ->
  1835. e
  1836. ) in
  1837. let e = check_cast e in
  1838. cf.cf_expr <- Some e;
  1839. cf.cf_type <- t;
  1840. end;
  1841. t
  1842. ) "bind_var" in
  1843. if not stat then force_constructor := true;
  1844. bind_type ctx cf r (snd e) false
  1845. in
  1846. (* ----------------------- FIELD INIT ----------------------------- *)
  1847. let loop_cf f =
  1848. let name = f.cff_name in
  1849. check_global_metadata ctx (fun m -> f.cff_meta <- m :: f.cff_meta) c.cl_module.m_path c.cl_path (Some name);
  1850. let p = f.cff_pos in
  1851. if name.[0] = '$' && ctx.com.display = DMNone then error "Field names starting with a dollar are not allowed" p;
  1852. let stat = List.mem AStatic f.cff_access in
  1853. let extern = Meta.has Meta.Extern f.cff_meta || c.cl_extern in
  1854. let is_abstract,allow_inline =
  1855. match c.cl_kind, f.cff_kind with
  1856. | KAbstractImpl _, _ -> true,true
  1857. |_, FFun _ -> false,ctx.g.doinline || extern
  1858. | _ -> false,true
  1859. in
  1860. let inline = List.mem AInline f.cff_access && allow_inline in
  1861. let override = List.mem AOverride f.cff_access in
  1862. let is_macro = Meta.has Meta.Macro f.cff_meta in
  1863. if is_macro then ctx.com.warning "@:macro should now be 'macro' accessor" p;
  1864. let is_macro = is_macro || List.mem AMacro f.cff_access in
  1865. List.iter (fun acc ->
  1866. match (acc, f.cff_kind) with
  1867. | APublic, _ | APrivate, _ | AStatic, _ -> ()
  1868. | ADynamic, FFun _ | AOverride, FFun _ | AMacro, FFun _ | AInline, FFun _ | AInline, FVar _ -> ()
  1869. | _, FVar _ -> error ("Invalid accessor '" ^ Ast.s_access acc ^ "' for variable " ^ name) p
  1870. | _, FProp _ -> error ("Invalid accessor '" ^ Ast.s_access acc ^ "' for property " ^ name) p
  1871. ) f.cff_access;
  1872. if override then (match c.cl_super with None -> error ("Invalid override on field '" ^ f.cff_name ^ "': class has no super class") p | _ -> ());
  1873. (* build the per-field context *)
  1874. let ctx = {
  1875. ctx with
  1876. pass = PBuildClass; (* will be set later to PTypeExpr *)
  1877. } in
  1878. match f.cff_kind with
  1879. | FVar (t,e) ->
  1880. if not stat && is_abstract then error (f.cff_name ^ ": Cannot declare member variable in abstract") p;
  1881. if inline && not stat then error (f.cff_name ^ ": Inline variable must be static") p;
  1882. if inline && e = None then error (f.cff_name ^ ": Inline variable must be initialized") p;
  1883. let t = (match t with
  1884. | None when not stat && e = None ->
  1885. error ("Type required for member variable " ^ name) p;
  1886. | None ->
  1887. mk_mono()
  1888. | Some t ->
  1889. let old = ctx.type_params in
  1890. if stat then ctx.type_params <- [];
  1891. let t = load_complex_type ctx p t in
  1892. if stat then ctx.type_params <- old;
  1893. t
  1894. ) in
  1895. let cf = {
  1896. cf_name = name;
  1897. cf_doc = f.cff_doc;
  1898. cf_meta = f.cff_meta;
  1899. cf_type = t;
  1900. cf_pos = f.cff_pos;
  1901. cf_kind = Var (if inline then { v_read = AccInline ; v_write = AccNever } else { v_read = AccNormal; v_write = AccNormal });
  1902. cf_expr = None;
  1903. cf_public = is_public f.cff_access None;
  1904. cf_params = [];
  1905. cf_overloads = [];
  1906. } in
  1907. ctx.curfield <- cf;
  1908. bind_var ctx cf e stat inline;
  1909. f, false, cf, true
  1910. | FFun fd ->
  1911. let params = type_function_params ctx fd f.cff_name p in
  1912. if inline && c.cl_interface then error (f.cff_name ^ ": You can't declare inline methods in interfaces") p;
  1913. if Meta.has Meta.Generic f.cff_meta then begin
  1914. if params = [] then error (f.cff_name ^ ": Generic functions must have type parameters") p;
  1915. end;
  1916. let is_macro = is_macro || (is_class_macro && stat) in
  1917. let f, stat, fd = if not is_macro || stat then
  1918. f, stat, fd
  1919. else if ctx.in_macro then
  1920. (* non-static macros methods are turned into static when we are running the macro *)
  1921. { f with cff_access = AStatic :: f.cff_access }, true, fd
  1922. else
  1923. (* remove display of first argument which will contain the "this" expression *)
  1924. f, stat, { fd with f_args = match fd.f_args with [] -> [] | _ :: l -> l }
  1925. in
  1926. let fd = if not is_macro then
  1927. fd
  1928. else begin
  1929. if ctx.in_macro then begin
  1930. (* a class with a macro cannot be extern in macro context (issue #2015) *)
  1931. c.cl_extern <- false;
  1932. let texpr = CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tparams = []; tsub = None } in
  1933. (* ExprOf type parameter might contain platform-specific type, let's replace it by Expr *)
  1934. let no_expr_of = function
  1935. | CTPath { tpackage = ["haxe";"macro"]; tname = "Expr"; tsub = Some ("ExprOf"); tparams = [TPType _] }
  1936. | CTPath { tpackage = []; tname = ("ExprOf"); tsub = None; tparams = [TPType _] } -> Some texpr
  1937. | t -> Some t
  1938. in
  1939. {
  1940. f_params = fd.f_params;
  1941. f_type = (match fd.f_type with None -> Some texpr | Some t -> no_expr_of t);
  1942. f_args = List.map (fun (a,o,t,e) -> a,o,(match t with None -> Some texpr | Some t -> no_expr_of t),e) fd.f_args;
  1943. f_expr = fd.f_expr;
  1944. }
  1945. end else
  1946. let tdyn = Some (CTPath { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None }) in
  1947. let to_dyn = function
  1948. | { tpackage = ["haxe";"macro"]; tname = "Expr"; tsub = Some ("ExprOf"); tparams = [TPType t] } -> Some t
  1949. | { tpackage = []; tname = ("ExprOf"); tsub = None; tparams = [TPType t] } -> Some t
  1950. | { tpackage = ["haxe"]; tname = ("PosInfos"); tsub = None; tparams = [] } -> error "haxe.PosInfos is not allowed on macro functions, use Context.currentPos() instead" p
  1951. | _ -> tdyn
  1952. in
  1953. {
  1954. f_params = fd.f_params;
  1955. f_type = (match fd.f_type with Some (CTPath t) -> to_dyn t | _ -> tdyn);
  1956. f_args = List.map (fun (a,o,t,_) -> a,o,(match t with Some (CTPath t) -> to_dyn t | _ -> tdyn),None) fd.f_args;
  1957. f_expr = None;
  1958. }
  1959. end in
  1960. let parent = (if not stat then get_parent c name else None) in
  1961. let dynamic = List.mem ADynamic f.cff_access || (match parent with Some { cf_kind = Method MethDynamic } -> true | _ -> false) in
  1962. if inline && dynamic then error (f.cff_name ^ ": You can't have both 'inline' and 'dynamic'") p;
  1963. ctx.type_params <- (match c.cl_kind with
  1964. | KAbstractImpl a when Meta.has Meta.Impl f.cff_meta ->
  1965. params @ a.a_params
  1966. | _ ->
  1967. if stat then params else params @ ctx.type_params);
  1968. let constr = (name = "new") in
  1969. let ret = if constr then ctx.t.tvoid else type_opt ctx p fd.f_type in
  1970. let rec loop args = match args with
  1971. | (name,opt,t,ct) :: args ->
  1972. let t, ct = type_function_arg ctx (type_opt ctx p t) ct opt p in
  1973. delay ctx PTypeField (fun() -> match follow t with
  1974. | TAbstract({a_path = ["haxe"],"Rest"},_) ->
  1975. if not c.cl_extern then error "Rest argument are only supported for extern methods" p;
  1976. if opt then error "Rest argument cannot be optional" p;
  1977. if ct <> None then error "Rest argument cannot have default value" p;
  1978. if args <> [] then error "Rest should only be used for the last function argument" p;
  1979. | _ ->
  1980. ()
  1981. );
  1982. (name, ct, t) :: (loop args)
  1983. | [] ->
  1984. []
  1985. in
  1986. let args = loop fd.f_args in
  1987. let t = TFun (fun_args args,ret) in
  1988. if c.cl_interface && not stat && fd.f_expr <> None then error (f.cff_name ^ ": An interface method cannot have a body") p;
  1989. if constr then begin
  1990. if c.cl_interface then error "An interface cannot have a constructor" p;
  1991. if stat then error "A constructor must not be static" p;
  1992. match fd.f_type with
  1993. | None | Some (CTPath { tpackage = []; tname = "Void" }) -> ()
  1994. | _ -> error "A class constructor can't have a return value" p
  1995. end;
  1996. let cf = {
  1997. cf_name = name;
  1998. cf_doc = f.cff_doc;
  1999. cf_meta = f.cff_meta;
  2000. cf_type = t;
  2001. cf_pos = f.cff_pos;
  2002. cf_kind = Method (if is_macro then MethMacro else if inline then MethInline else if dynamic then MethDynamic else MethNormal);
  2003. cf_expr = None;
  2004. cf_public = is_public f.cff_access parent;
  2005. cf_params = params;
  2006. cf_overloads = [];
  2007. } in
  2008. generate_value_meta ctx.com (Some c) cf fd.f_args;
  2009. let do_bind = ref (((not c.cl_extern || inline) && not c.cl_interface) || cf.cf_name = "__init__") in
  2010. let do_add = ref true in
  2011. (match c.cl_kind with
  2012. | KAbstractImpl a ->
  2013. let m = mk_mono() in
  2014. let ta = TAbstract(a, List.map (fun _ -> mk_mono()) a.a_params) in
  2015. let tthis = if Meta.has Meta.Impl f.cff_meta || Meta.has Meta.To f.cff_meta then monomorphs a.a_params a.a_this else a.a_this in
  2016. let check_bind () =
  2017. if fd.f_expr = None then begin
  2018. if inline then error (f.cff_name ^ ": Inline functions must have an expression") f.cff_pos;
  2019. begin match fd.f_type with
  2020. | None -> error (f.cff_name ^ ": Functions without expressions must have an explicit return type") f.cff_pos
  2021. | Some _ -> ()
  2022. end;
  2023. cf.cf_meta <- (Meta.NoExpr,[],cf.cf_pos) :: cf.cf_meta;
  2024. do_add := false;
  2025. do_bind := false;
  2026. end
  2027. in
  2028. let rec loop ml = match ml with
  2029. | (Meta.From,_,_) :: _ ->
  2030. if is_macro then error (f.cff_name ^ ": Macro cast functions are not supported") p;
  2031. let r = fun () ->
  2032. (* the return type of a from-function must be the abstract, not the underlying type *)
  2033. (try type_eq EqStrict ret ta with Unify_error l -> error (error_msg (Unify l)) p);
  2034. match t with
  2035. | TFun([_,_,t],_) -> t
  2036. | _ -> error (f.cff_name ^ ": @:from cast functions must accept exactly one argument") p
  2037. in
  2038. a.a_from_field <- (TLazy (ref r),cf) :: a.a_from_field;
  2039. | (Meta.To,_,_) :: _ ->
  2040. if is_macro then error (f.cff_name ^ ": Macro cast functions are not supported") p;
  2041. if not (Meta.has Meta.Impl cf.cf_meta) then cf.cf_meta <- (Meta.Impl,[],cf.cf_pos) :: cf.cf_meta;
  2042. let resolve_m args =
  2043. (try unify_raise ctx t (tfun (tthis :: args) m) f.cff_pos with Error (Unify l,p) -> error (error_msg (Unify l)) p);
  2044. match follow m with
  2045. | TMono _ when (match cf.cf_type with TFun(_,r) -> r == t_dynamic | _ -> false) -> t_dynamic
  2046. | m -> m
  2047. in
  2048. let r = exc_protect ctx (fun r ->
  2049. let args = if Meta.has Meta.MultiType a.a_meta then begin
  2050. let ctor = try
  2051. PMap.find "_new" c.cl_statics
  2052. with Not_found ->
  2053. error "Constructor of multi-type abstract must be defined before the individual @:to-functions are" cf.cf_pos
  2054. in
  2055. (* delay ctx PFinal (fun () -> unify ctx m tthis f.cff_pos); *)
  2056. let args = match follow (monomorphs a.a_params ctor.cf_type) with
  2057. | TFun(args,_) -> List.map (fun (_,_,t) -> t) args
  2058. | _ -> assert false
  2059. in
  2060. args
  2061. end else
  2062. []
  2063. in
  2064. let t = resolve_m args in
  2065. r := (fun() -> t);
  2066. t
  2067. ) "@:to" in
  2068. delay ctx PForce (fun() -> ignore ((!r)()));
  2069. a.a_to_field <- (TLazy r, cf) :: a.a_to_field
  2070. | (Meta.ArrayAccess,_,_) :: _ ->
  2071. if is_macro then error (f.cff_name ^ ": Macro array-access functions are not supported") p;
  2072. a.a_array <- cf :: a.a_array;
  2073. if Meta.has Meta.CoreType a.a_meta then check_bind();
  2074. | (Meta.Op,[EBinop(op,_,_),_],_) :: _ ->
  2075. if is_macro then error (f.cff_name ^ ": Macro operator functions are not supported") p;
  2076. let targ = if Meta.has Meta.Impl f.cff_meta then tthis else ta in
  2077. let left_eq,right_eq = match follow t with
  2078. | TFun([(_,_,t1);(_,_,t2)],_) ->
  2079. type_iseq targ t1,type_iseq targ t2
  2080. | _ ->
  2081. if Meta.has Meta.Impl cf.cf_meta then
  2082. error (f.cff_name ^ ": Member @:op functions must accept exactly one argument") cf.cf_pos
  2083. else
  2084. error (f.cff_name ^ ": Static @:op functions must accept exactly two arguments") cf.cf_pos
  2085. in
  2086. if not (left_eq || right_eq) then error (f.cff_name ^ ": The left or right argument type must be " ^ (s_type (print_context()) targ)) f.cff_pos;
  2087. if right_eq && Meta.has Meta.Commutative f.cff_meta then error (f.cff_name ^ ": @:commutative is only allowed if the right argument is not " ^ (s_type (print_context()) targ)) f.cff_pos;
  2088. a.a_ops <- (op,cf) :: a.a_ops;
  2089. check_bind();
  2090. | (Meta.Op,[EUnop(op,flag,_),_],_) :: _ ->
  2091. if is_macro then error (f.cff_name ^ ": Macro operator functions are not supported") p;
  2092. let targ = if Meta.has Meta.Impl f.cff_meta then tthis else ta in
  2093. (try type_eq EqStrict t (tfun [targ] (mk_mono())) with Unify_error l -> raise (Error ((Unify l),f.cff_pos)));
  2094. a.a_unops <- (op,flag,cf) :: a.a_unops;
  2095. check_bind();
  2096. | (Meta.Impl,_,_) :: ml when f.cff_name <> "_new" && not is_macro ->
  2097. begin match follow t with
  2098. | TFun((_,_,t1) :: _, _) when type_iseq tthis t1 ->
  2099. ()
  2100. | _ ->
  2101. display_error ctx ("First argument of implementation function must be " ^ (s_type (print_context()) tthis)) f.cff_pos
  2102. end;
  2103. loop ml
  2104. (* | (Meta.Resolve,_,_) :: _ ->
  2105. let targ = if Meta.has Meta.Impl f.cff_meta then tthis else ta in
  2106. begin match follow t with
  2107. | TFun([(_,_,t1);(_,_,t2)],_) ->
  2108. if not is_macro then begin
  2109. if not (type_iseq targ t1) then error ("First argument type must be " ^ (s_type (print_context()) targ)) f.cff_pos;
  2110. if not (type_iseq ctx.t.tstring t2) then error ("Second argument type must be String") f.cff_pos
  2111. end
  2112. | _ ->
  2113. error ("Field type of resolve must be " ^ (s_type (print_context()) targ) ^ " -> String -> T") f.cff_pos
  2114. end *)
  2115. | _ :: ml ->
  2116. loop ml
  2117. | [] ->
  2118. ()
  2119. in
  2120. loop f.cff_meta;
  2121. if f.cff_name = "_new" && Meta.has Meta.MultiType a.a_meta then do_bind := false;
  2122. | _ ->
  2123. ());
  2124. init_meta_overloads ctx (Some c) cf;
  2125. ctx.curfield <- cf;
  2126. let r = exc_protect ctx (fun r ->
  2127. if not !return_partial_type then begin
  2128. r := (fun() -> t);
  2129. context_init();
  2130. incr stats.s_methods_typed;
  2131. if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ name);
  2132. let fmode = (match c.cl_kind with
  2133. | KAbstractImpl _ ->
  2134. (match args with
  2135. | ("this",_,_) :: _ -> FunMemberAbstract
  2136. | _ when name = "_new" -> FunMemberAbstract
  2137. | _ -> FunStatic)
  2138. | _ ->
  2139. if constr then FunConstructor else if stat then FunStatic else FunMember
  2140. ) in
  2141. let display_field = display_file && (f.cff_pos.pmin <= cp.pmin && f.cff_pos.pmax >= cp.pmax) in
  2142. let e , fargs = type_function ctx args ret fmode fd display_field p in
  2143. let f = {
  2144. tf_args = fargs;
  2145. tf_type = ret;
  2146. tf_expr = e;
  2147. } in
  2148. if stat && name = "__init__" then
  2149. (match e.eexpr with
  2150. | TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
  2151. | _ -> c.cl_init <- Some e);
  2152. cf.cf_expr <- Some (mk (TFunction f) t p);
  2153. cf.cf_type <- t;
  2154. end;
  2155. t
  2156. ) "type_fun" in
  2157. if !do_bind then bind_type ctx cf r (match fd.f_expr with Some e -> snd e | None -> f.cff_pos) is_macro;
  2158. f, constr, cf, !do_add
  2159. | FProp (get,set,t,eo) ->
  2160. (match c.cl_kind with
  2161. | KAbstractImpl a when Meta.has Meta.Impl f.cff_meta ->
  2162. ctx.type_params <- a.a_params;
  2163. | _ -> ());
  2164. let ret = (match t, eo with
  2165. | None, None -> error (f.cff_name ^ ": Property must either define a type or a default value") p;
  2166. | None, _ -> mk_mono()
  2167. | Some t, _ -> load_complex_type ctx p t
  2168. ) in
  2169. let t_get,t_set = match c.cl_kind with
  2170. | KAbstractImpl a when Meta.has Meta.Impl f.cff_meta ->
  2171. if Meta.has Meta.IsVar f.cff_meta then error (f.cff_name ^ ": Abstract properties cannot be real variables") f.cff_pos;
  2172. let ta = apply_params a.a_params (List.map snd a.a_params) a.a_this in
  2173. tfun [ta] ret, tfun [ta;ret] ret
  2174. | _ -> tfun [] ret, TFun(["value",false,ret],ret)
  2175. in
  2176. let check_method m t req_name =
  2177. if ctx.com.display <> DMNone then () else
  2178. try
  2179. let overloads =
  2180. (* on pf_overload platforms, the getter/setter may have been defined as an overloaded function; get all overloads *)
  2181. if ctx.com.config.pf_overload then
  2182. if stat then
  2183. let f = PMap.find m c.cl_statics in
  2184. (f.cf_type, f) :: (List.map (fun f -> f.cf_type, f) f.cf_overloads)
  2185. else
  2186. get_overloads c m
  2187. else
  2188. [ if stat then
  2189. let f = PMap.find m c.cl_statics in
  2190. f.cf_type, f
  2191. else match class_field c (List.map snd c.cl_params) m with
  2192. | _, t,f -> t,f ]
  2193. in
  2194. (* choose the correct overload if and only if there is more than one overload found *)
  2195. let rec get_overload overl = match overl with
  2196. | [tf] -> tf
  2197. | (t2,f2) :: overl ->
  2198. if type_iseq t t2 then
  2199. (t2,f2)
  2200. else
  2201. get_overload overl
  2202. | [] ->
  2203. if c.cl_interface then
  2204. raise Not_found
  2205. else
  2206. raise (Error (Custom
  2207. (Printf.sprintf "No overloaded method named %s was compatible with the property %s with expected type %s" m name (s_type (print_context()) t)
  2208. ), p))
  2209. in
  2210. let t2, f2 = get_overload overloads in
  2211. (* accessors must be public on As3 (issue #1872) *)
  2212. if Common.defined ctx.com Define.As3 then f2.cf_meta <- (Meta.Public,[],p) :: f2.cf_meta;
  2213. (match f2.cf_kind with
  2214. | Method MethMacro ->
  2215. display_error ctx (f2.cf_name ^ ": Macro methods cannot be used as property accessor") p;
  2216. display_error ctx (f2.cf_name ^ ": Accessor method is here") f2.cf_pos;
  2217. | _ -> ());
  2218. unify_raise ctx t2 t f2.cf_pos;
  2219. if (Meta.has Meta.Impl f.cff_meta && not (Meta.has Meta.Impl f2.cf_meta)) || (Meta.has Meta.Impl f2.cf_meta && not (Meta.has Meta.Impl f.cff_meta)) then
  2220. display_error ctx "Mixing abstract implementation and static properties/accessors is not allowed" f2.cf_pos;
  2221. (match req_name with None -> () | Some n -> display_error ctx ("Please use " ^ n ^ " to name your property access method") f2.cf_pos);
  2222. with
  2223. | Error (Unify l,p) -> raise (Error (Stack (Custom ("In method " ^ m ^ " required by property " ^ name),Unify l),p))
  2224. | Not_found ->
  2225. if req_name <> None then display_error ctx (f.cff_name ^ ": Custom property accessor is no longer supported, please use get/set") p else
  2226. if c.cl_interface then begin
  2227. let cf = mk_field m t p in
  2228. cf.cf_meta <- [Meta.CompilerGenerated,[],p];
  2229. cf.cf_kind <- Method MethNormal;
  2230. c.cl_fields <- PMap.add cf.cf_name cf c.cl_fields;
  2231. c.cl_ordered_fields <- cf :: c.cl_ordered_fields;
  2232. end else if not c.cl_extern then begin
  2233. try
  2234. let _, _, f2 = (if not stat then let f = PMap.find m c.cl_statics in None, f.cf_type, f else class_field c (List.map snd c.cl_params) m) in
  2235. display_error ctx (Printf.sprintf "Method %s is no valid accessor for %s because it is %sstatic" m name (if stat then "not " else "")) f2.cf_pos
  2236. with Not_found ->
  2237. display_error ctx ("Method " ^ m ^ " required by property " ^ name ^ " is missing") p
  2238. end
  2239. in
  2240. let get = (match get with
  2241. | "null" -> AccNo
  2242. | "dynamic" -> AccCall
  2243. | "never" -> AccNever
  2244. | "default" -> AccNormal
  2245. | _ ->
  2246. let get = if get = "get" then "get_" ^ name else get in
  2247. delay ctx PTypeField (fun() -> check_method get t_get (if get <> "get" && get <> "get_" ^ name then Some ("get_" ^ name) else None));
  2248. AccCall
  2249. ) in
  2250. let set = (match set with
  2251. | "null" ->
  2252. (* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
  2253. if c.cl_extern && (match c.cl_path with "flash" :: _ , _ -> true | _ -> false) && ctx.com.platform = Flash then
  2254. AccNever
  2255. else
  2256. AccNo
  2257. | "never" -> AccNever
  2258. | "dynamic" -> AccCall
  2259. | "default" -> AccNormal
  2260. | _ ->
  2261. let set = if set = "set" then "set_" ^ name else set in
  2262. delay ctx PTypeField (fun() -> check_method set t_set (if set <> "set" && set <> "set_" ^ name then Some ("set_" ^ name) else None));
  2263. AccCall
  2264. ) in
  2265. if set = AccNormal && (match get with AccCall -> true | _ -> false) then error (f.cff_name ^ ": Unsupported property combination") p;
  2266. let cf = {
  2267. cf_name = name;
  2268. cf_doc = f.cff_doc;
  2269. cf_meta = f.cff_meta;
  2270. cf_pos = f.cff_pos;
  2271. cf_kind = Var { v_read = get; v_write = set };
  2272. cf_expr = None;
  2273. cf_type = ret;
  2274. cf_public = is_public f.cff_access None;
  2275. cf_params = [];
  2276. cf_overloads = [];
  2277. } in
  2278. ctx.curfield <- cf;
  2279. bind_var ctx cf eo stat inline;
  2280. f, false, cf, true
  2281. in
  2282. let rec check_require = function
  2283. | [] -> None
  2284. | (Meta.Require,conds,_) :: l ->
  2285. let rec loop = function
  2286. | [] -> check_require l
  2287. | e :: l ->
  2288. let sc = match fst e with
  2289. | EConst (Ident s) -> s
  2290. | EBinop ((OpEq|OpNotEq|OpGt|OpGte|OpLt|OpLte) as op,(EConst (Ident s),_),(EConst ((Int _ | Float _ | String _) as c),_)) -> s ^ s_binop op ^ s_constant c
  2291. | _ -> ""
  2292. in
  2293. if not (Parser.is_true (Parser.eval ctx.com e)) then
  2294. Some (sc,(match List.rev l with (EConst (String msg),_) :: _ -> Some msg | _ -> None))
  2295. else
  2296. loop l
  2297. in
  2298. loop conds
  2299. | _ :: l ->
  2300. check_require l
  2301. in
  2302. let cl_req = check_require c.cl_meta in
  2303. List.iter (fun f ->
  2304. let p = f.cff_pos in
  2305. try
  2306. let fd , constr, f, do_add = loop_cf f in
  2307. let is_static = List.mem AStatic fd.cff_access in
  2308. if (is_static || constr) && c.cl_interface && f.cf_name <> "__init__" then error "You can't declare static fields in interfaces" p;
  2309. begin try
  2310. let _,args,_ = Meta.get Meta.IfFeature f.cf_meta in
  2311. List.iter (fun e -> match fst e with
  2312. | EConst(String s) ->
  2313. ctx.m.curmod.m_extra.m_if_feature <- (s,(c,f,is_static)) :: ctx.m.curmod.m_extra.m_if_feature;
  2314. | _ ->
  2315. error "String expected" (pos e)
  2316. ) args
  2317. with Not_found -> () end;
  2318. let req = check_require fd.cff_meta in
  2319. let req = (match req with None -> if is_static || constr then cl_req else None | _ -> req) in
  2320. (match req with
  2321. | None -> ()
  2322. | Some r -> f.cf_kind <- Var { v_read = AccRequire (fst r, snd r); v_write = AccRequire (fst r, snd r) });
  2323. if constr then begin
  2324. match c.cl_constructor with
  2325. | None ->
  2326. c.cl_constructor <- Some f
  2327. | Some ctor when ctx.com.config.pf_overload ->
  2328. if Meta.has Meta.Overload f.cf_meta && Meta.has Meta.Overload ctor.cf_meta then
  2329. ctor.cf_overloads <- f :: ctor.cf_overloads
  2330. else
  2331. display_error ctx ("If using overloaded constructors, all constructors must be declared with @:overload") (if Meta.has Meta.Overload f.cf_meta then ctor.cf_pos else f.cf_pos)
  2332. | Some ctor ->
  2333. display_error ctx "Duplicate constructor" p
  2334. end else if not is_static || f.cf_name <> "__init__" then begin
  2335. let dup = if is_static then PMap.exists f.cf_name c.cl_fields || has_field f.cf_name c.cl_super else PMap.exists f.cf_name c.cl_statics in
  2336. if dup then error ("Same field name can't be use for both static and instance : " ^ f.cf_name) p;
  2337. if List.mem AOverride fd.cff_access then c.cl_overrides <- f :: c.cl_overrides;
  2338. let is_var f = match f.cf_kind with | Var _ -> true | _ -> false in
  2339. if PMap.mem f.cf_name (if is_static then c.cl_statics else c.cl_fields) then
  2340. if ctx.com.config.pf_overload && Meta.has Meta.Overload f.cf_meta && not (is_var f) then
  2341. let mainf = PMap.find f.cf_name (if is_static then c.cl_statics else c.cl_fields) in
  2342. if is_var mainf then display_error ctx "Cannot declare a variable with same name as a method" mainf.cf_pos;
  2343. (if not (Meta.has Meta.Overload mainf.cf_meta) then display_error ctx ("Overloaded methods must have @:overload metadata") mainf.cf_pos);
  2344. mainf.cf_overloads <- f :: mainf.cf_overloads
  2345. else
  2346. display_error ctx ("Duplicate class field declaration : " ^ f.cf_name) p
  2347. else
  2348. if not do_add then
  2349. ()
  2350. else if is_static then begin
  2351. c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
  2352. c.cl_ordered_statics <- f :: c.cl_ordered_statics;
  2353. end else begin
  2354. c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
  2355. c.cl_ordered_fields <- f :: c.cl_ordered_fields;
  2356. end;
  2357. end
  2358. with Error (Custom str,p2) when p = p2 ->
  2359. display_error ctx str p
  2360. ) fields;
  2361. (match c.cl_kind with
  2362. | KAbstractImpl a ->
  2363. a.a_to_field <- List.rev a.a_to_field;
  2364. a.a_from_field <- List.rev a.a_from_field;
  2365. a.a_ops <- List.rev a.a_ops;
  2366. a.a_unops <- List.rev a.a_unops;
  2367. a.a_array <- List.rev a.a_array;
  2368. | _ -> ());
  2369. c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
  2370. c.cl_ordered_fields <- List.rev c.cl_ordered_fields;
  2371. (*
  2372. make sure a default contructor with same access as super one will be added to the class structure at some point.
  2373. *)
  2374. (* add_constructor does not deal with overloads correctly *)
  2375. if not ctx.com.config.pf_overload then add_constructor ctx c !force_constructor p;
  2376. (* check overloaded constructors *)
  2377. (if ctx.com.config.pf_overload then match c.cl_constructor with
  2378. | Some ctor ->
  2379. List.iter (fun f ->
  2380. try
  2381. (* TODO: consider making a broader check, and treat some types, like TAnon and type parameters as Dynamic *)
  2382. ignore(List.find (fun f2 -> f != f2 && same_overload_args f.cf_type f2.cf_type f f2) (ctor :: ctor.cf_overloads));
  2383. display_error ctx ("Another overloaded field of same signature was already declared : " ^ f.cf_name) f.cf_pos;
  2384. with Not_found -> ()
  2385. ) (ctor :: ctor.cf_overloads)
  2386. | _ -> ());
  2387. (* push delays in reverse order so they will be run in correct order *)
  2388. List.iter (fun (ctx,r) ->
  2389. init_class_done ctx;
  2390. (match r with
  2391. | None -> ()
  2392. | Some r -> delay ctx PTypeField (fun() -> ignore((!r)())))
  2393. ) !delayed_expr
  2394. let resolve_typedef t =
  2395. match t with
  2396. | TClassDecl _ | TEnumDecl _ | TAbstractDecl _ -> t
  2397. | TTypeDecl td ->
  2398. match follow td.t_type with
  2399. | TEnum (e,_) -> TEnumDecl e
  2400. | TInst (c,_) -> TClassDecl c
  2401. | TAbstract (a,_) -> TAbstractDecl a
  2402. | _ -> t
  2403. let add_module ctx m p =
  2404. let decl_type t =
  2405. let t = t_infos t in
  2406. try
  2407. let m2 = Hashtbl.find ctx.g.types_module t.mt_path in
  2408. if m.m_path <> m2 && String.lowercase (s_type_path m2) = String.lowercase (s_type_path m.m_path) then error ("Module " ^ s_type_path m2 ^ " is loaded with a different case than " ^ s_type_path m.m_path) p;
  2409. error ("Type name " ^ s_type_path t.mt_path ^ " is redefined from module " ^ s_type_path m2) p
  2410. with
  2411. Not_found ->
  2412. Hashtbl.add ctx.g.types_module t.mt_path m.m_path
  2413. in
  2414. List.iter decl_type m.m_types;
  2415. Hashtbl.add ctx.g.modules m.m_path m
  2416. (*
  2417. In this pass, we can access load and access other modules types, but we cannot follow them or access their structure
  2418. since they have not been setup. We also build a context_init list that will be evaluated the first time we evaluate
  2419. an expression into the context
  2420. *)
  2421. let rec init_module_type ctx context_init do_init (decl,p) =
  2422. let get_type name =
  2423. try List.find (fun t -> snd (t_infos t).mt_path = name) ctx.m.curmod.m_types with Not_found -> assert false
  2424. in
  2425. match decl with
  2426. | EImport (path,mode) ->
  2427. let rec loop acc = function
  2428. | x :: l when is_lower_ident (fst x) -> loop (x::acc) l
  2429. | rest -> List.rev acc, rest
  2430. in
  2431. let pack, rest = loop [] path in
  2432. (match rest with
  2433. | [] ->
  2434. (match mode with
  2435. | IAll ->
  2436. ctx.m.wildcard_packages <- List.map fst pack :: ctx.m.wildcard_packages
  2437. | _ ->
  2438. (match List.rev path with
  2439. | [] -> assert false
  2440. | (_,p) :: _ -> error "Module name must start with an uppercase letter" p))
  2441. | (tname,p2) :: rest ->
  2442. let p1 = (match pack with [] -> p2 | (_,p1) :: _ -> p1) in
  2443. let p_type = punion p1 p2 in
  2444. let md = ctx.g.do_load_module ctx (List.map fst pack,tname) p_type in
  2445. let types = md.m_types in
  2446. let no_private t = not (t_infos t).mt_private in
  2447. let chk_private t p = if (t_infos t).mt_private then error "You can't import a private type" p in
  2448. let has_name name t = snd (t_infos t).mt_path = name in
  2449. let get_type tname =
  2450. let t = (try List.find (has_name tname) types with Not_found -> error (string_error tname (List.map (fun mt -> snd (t_infos mt).mt_path) types) ("Module " ^ s_type_path md.m_path ^ " does not define type " ^ tname)) p_type) in
  2451. chk_private t p_type;
  2452. t
  2453. in
  2454. let rebind t name =
  2455. if not (name.[0] >= 'A' && name.[0] <= 'Z') then
  2456. error "Type aliases must start with an uppercase letter" p;
  2457. let _, _, f = ctx.g.do_build_instance ctx t p_type in
  2458. (* create a temp private typedef, does not register it in module *)
  2459. TTypeDecl {
  2460. t_path = (fst md.m_path @ ["_" ^ snd md.m_path],name);
  2461. t_module = md;
  2462. t_pos = p;
  2463. t_private = true;
  2464. t_doc = None;
  2465. t_meta = [];
  2466. t_params = (t_infos t).mt_params;
  2467. t_type = f (List.map snd (t_infos t).mt_params);
  2468. }
  2469. in
  2470. let add_static_init t name s =
  2471. let name = (match name with None -> s | Some n -> n) in
  2472. match resolve_typedef t with
  2473. | TClassDecl c ->
  2474. ignore(c.cl_build());
  2475. ignore(PMap.find s c.cl_statics);
  2476. ctx.m.module_globals <- PMap.add name (TClassDecl c,s) ctx.m.module_globals
  2477. | TEnumDecl e ->
  2478. ignore(PMap.find s e.e_constrs);
  2479. ctx.m.module_globals <- PMap.add name (TEnumDecl e,s) ctx.m.module_globals
  2480. | _ ->
  2481. raise Not_found
  2482. in
  2483. (match mode with
  2484. | INormal | IAsName _ ->
  2485. let name = (match mode with IAsName n -> Some n | _ -> None) in
  2486. (match rest with
  2487. | [] ->
  2488. (match name with
  2489. | None ->
  2490. ctx.m.module_types <- List.filter no_private types @ ctx.m.module_types
  2491. | Some newname ->
  2492. ctx.m.module_types <- rebind (get_type tname) newname :: ctx.m.module_types);
  2493. | [tsub,p2] ->
  2494. let p = punion p1 p2 in
  2495. (try
  2496. let tsub = List.find (has_name tsub) types in
  2497. chk_private tsub p;
  2498. ctx.m.module_types <- (match name with None -> tsub | Some n -> rebind tsub n) :: ctx.m.module_types
  2499. with Not_found ->
  2500. (* this might be a static property, wait later to check *)
  2501. let tmain = get_type tname in
  2502. context_init := (fun() ->
  2503. try
  2504. add_static_init tmain name tsub
  2505. with Not_found ->
  2506. error (s_type_path (t_infos tmain).mt_path ^ " has no field or subtype " ^ tsub) p
  2507. ) :: !context_init)
  2508. | (tsub,p2) :: (fname,p3) :: rest ->
  2509. (match rest with
  2510. | [] -> ()
  2511. | (n,p) :: _ -> error ("Unexpected " ^ n) p);
  2512. let tsub = get_type tsub in
  2513. context_init := (fun() ->
  2514. try
  2515. add_static_init tsub name fname
  2516. with Not_found ->
  2517. error (s_type_path (t_infos tsub).mt_path ^ " has no field " ^ fname) (punion p p3)
  2518. ) :: !context_init;
  2519. )
  2520. | IAll ->
  2521. let t = (match rest with
  2522. | [] -> get_type tname
  2523. | [tsub,_] -> get_type tsub
  2524. | _ :: (n,p) :: _ -> error ("Unexpected " ^ n) p
  2525. ) in
  2526. context_init := (fun() ->
  2527. match resolve_typedef t with
  2528. | TClassDecl c
  2529. | TAbstractDecl {a_impl = Some c} ->
  2530. ignore(c.cl_build());
  2531. PMap.iter (fun _ cf -> if not (has_meta Meta.NoImportGlobal cf.cf_meta) then ctx.m.module_globals <- PMap.add cf.cf_name (TClassDecl c,cf.cf_name) ctx.m.module_globals) c.cl_statics
  2532. | TEnumDecl e ->
  2533. PMap.iter (fun _ c -> if not (has_meta Meta.NoImportGlobal c.ef_meta) then ctx.m.module_globals <- PMap.add c.ef_name (TEnumDecl e,c.ef_name) ctx.m.module_globals) e.e_constrs
  2534. | _ ->
  2535. error "No statics to import from this type" p
  2536. ) :: !context_init
  2537. ))
  2538. | EUsing t ->
  2539. (* do the import first *)
  2540. let types = (match t.tsub with
  2541. | None ->
  2542. let md = ctx.g.do_load_module ctx (t.tpackage,t.tname) p in
  2543. let types = List.filter (fun t -> not (t_infos t).mt_private) md.m_types in
  2544. ctx.m.module_types <- types @ ctx.m.module_types;
  2545. types
  2546. | Some _ ->
  2547. let t = load_type_def ctx p t in
  2548. ctx.m.module_types <- t :: ctx.m.module_types;
  2549. [t]
  2550. ) in
  2551. (* delay the using since we need to resolve typedefs *)
  2552. let filter_classes types =
  2553. let rec loop acc types = match types with
  2554. | td :: l ->
  2555. (match resolve_typedef td with
  2556. | TClassDecl c | TAbstractDecl({a_impl = Some c}) ->
  2557. loop (c :: acc) l
  2558. | td ->
  2559. loop acc l)
  2560. | [] ->
  2561. acc
  2562. in
  2563. loop [] types
  2564. in
  2565. context_init := (fun() -> ctx.m.module_using <- filter_classes types @ ctx.m.module_using) :: !context_init
  2566. | EClass d ->
  2567. let c = (match get_type d.d_name with TClassDecl c -> c | _ -> assert false) in
  2568. check_global_metadata ctx (fun m -> c.cl_meta <- m :: c.cl_meta) c.cl_module.m_path c.cl_path None;
  2569. let herits = d.d_flags in
  2570. if Meta.has Meta.Generic c.cl_meta && c.cl_params <> [] then c.cl_kind <- KGeneric;
  2571. if Meta.has Meta.GenericBuild c.cl_meta then c.cl_kind <- KGenericBuild d.d_data;
  2572. if c.cl_path = (["haxe";"macro"],"MacroType") then c.cl_kind <- KMacroType;
  2573. c.cl_extern <- List.mem HExtern herits;
  2574. c.cl_interface <- List.mem HInterface herits;
  2575. let rec build() =
  2576. c.cl_build <- (fun()-> false);
  2577. try
  2578. set_heritance ctx c herits p;
  2579. init_class ctx c p do_init d.d_flags d.d_data;
  2580. c.cl_build <- (fun()-> true);
  2581. List.iter (fun (_,t) -> ignore(follow t)) c.cl_params;
  2582. true;
  2583. with Exit ->
  2584. c.cl_build <- make_pass ctx build;
  2585. delay ctx PTypeField (fun() -> ignore(c.cl_build())); (* delay after PBuildClass, not very good but better than forgotten *)
  2586. false
  2587. | exn ->
  2588. c.cl_build <- (fun()-> true);
  2589. raise exn
  2590. in
  2591. ctx.pass <- PBuildClass;
  2592. ctx.curclass <- c;
  2593. c.cl_build <- make_pass ctx build;
  2594. ctx.pass <- PBuildModule;
  2595. ctx.curclass <- null_class;
  2596. delay ctx PBuildClass (fun() -> ignore(c.cl_build()));
  2597. | EEnum d ->
  2598. let e = (match get_type d.d_name with TEnumDecl e -> e | _ -> assert false) in
  2599. let ctx = { ctx with type_params = e.e_params } in
  2600. let h = (try Some (Hashtbl.find ctx.g.type_patches e.e_path) with Not_found -> None) in
  2601. check_global_metadata ctx (fun m -> e.e_meta <- m :: e.e_meta) e.e_module.m_path e.e_path None;
  2602. (match h with
  2603. | None -> ()
  2604. | Some (h,hcl) ->
  2605. Hashtbl.iter (fun _ _ -> error "Field type patch not supported for enums" e.e_pos) h;
  2606. e.e_meta <- e.e_meta @ hcl.tp_meta);
  2607. let constructs = ref d.d_data in
  2608. let get_constructs() =
  2609. List.map (fun c ->
  2610. {
  2611. cff_name = c.ec_name;
  2612. cff_doc = c.ec_doc;
  2613. cff_meta = c.ec_meta;
  2614. cff_pos = c.ec_pos;
  2615. cff_access = [];
  2616. cff_kind = (match c.ec_args, c.ec_params with
  2617. | [], [] -> FVar (c.ec_type,None)
  2618. | _ -> FFun { f_params = c.ec_params; f_type = c.ec_type; f_expr = None; f_args = List.map (fun (n,o,t) -> n,o,Some t,None) c.ec_args });
  2619. }
  2620. ) (!constructs)
  2621. in
  2622. let init () = List.iter (fun f -> f()) !context_init in
  2623. build_module_def ctx (TEnumDecl e) e.e_meta get_constructs init (fun (e,p) ->
  2624. match e with
  2625. | EVars [_,Some (CTAnonymous fields),None] ->
  2626. constructs := List.map (fun f ->
  2627. let args, params, t = (match f.cff_kind with
  2628. | FVar (t,None) -> [], [], t
  2629. | FFun { f_params = pl; f_type = t; f_expr = (None|Some (EBlock [],_)); f_args = al } ->
  2630. let al = List.map (fun (n,o,t,_) -> match t with None -> error "Missing function parameter type" f.cff_pos | Some t -> n,o,t) al in
  2631. al, pl, t
  2632. | _ ->
  2633. error "Invalid enum constructor in @:build result" p
  2634. ) in
  2635. {
  2636. ec_name = f.cff_name;
  2637. ec_doc = f.cff_doc;
  2638. ec_meta = f.cff_meta;
  2639. ec_pos = f.cff_pos;
  2640. ec_args = args;
  2641. ec_params = params;
  2642. ec_type = t;
  2643. }
  2644. ) fields
  2645. | _ -> error "Enum build macro must return a single variable with anonymous object fields" p
  2646. );
  2647. let et = TEnum (e,List.map snd e.e_params) in
  2648. let names = ref [] in
  2649. let index = ref 0 in
  2650. let is_flat = ref true in
  2651. let fields = ref PMap.empty in
  2652. List.iter (fun c ->
  2653. let p = c.ec_pos in
  2654. let params = ref [] in
  2655. params := List.map (fun tp -> type_type_params ~enum_constructor:true ctx ([],c.ec_name) (fun() -> !params) c.ec_pos tp) c.ec_params;
  2656. let params = !params in
  2657. let ctx = { ctx with type_params = params @ ctx.type_params } in
  2658. let rt = (match c.ec_type with
  2659. | None -> et
  2660. | Some t ->
  2661. let t = load_complex_type ctx p t in
  2662. (match follow t with
  2663. | TEnum (te,_) when te == e ->
  2664. ()
  2665. | _ ->
  2666. error "Explicit enum type must be of the same enum type" p);
  2667. t
  2668. ) in
  2669. let t = (match c.ec_args with
  2670. | [] -> rt
  2671. | l ->
  2672. is_flat := false;
  2673. let pnames = ref PMap.empty in
  2674. TFun (List.map (fun (s,opt,t) ->
  2675. (match t with CTPath({tpackage=[];tname="Void"}) -> error "Arguments of type Void are not allowed in enum constructors" c.ec_pos | _ -> ());
  2676. if PMap.mem s (!pnames) then error ("Duplicate parameter '" ^ s ^ "' in enum constructor " ^ c.ec_name) p;
  2677. pnames := PMap.add s () (!pnames);
  2678. s, opt, load_type_opt ~opt ctx p (Some t)
  2679. ) l, rt)
  2680. ) in
  2681. if PMap.mem c.ec_name e.e_constrs then error ("Duplicate constructor " ^ c.ec_name) p;
  2682. let f = {
  2683. ef_name = c.ec_name;
  2684. ef_type = t;
  2685. ef_pos = p;
  2686. ef_doc = c.ec_doc;
  2687. ef_index = !index;
  2688. ef_params = params;
  2689. ef_meta = c.ec_meta;
  2690. } in
  2691. let cf = {
  2692. cf_name = f.ef_name;
  2693. cf_public = true;
  2694. cf_type = f.ef_type;
  2695. cf_kind = (match follow f.ef_type with
  2696. | TFun _ -> Method MethNormal
  2697. | _ -> Var { v_read = AccNormal; v_write = AccNo }
  2698. );
  2699. cf_pos = e.e_pos;
  2700. cf_doc = None;
  2701. cf_meta = no_meta;
  2702. cf_expr = None;
  2703. cf_params = f.ef_params;
  2704. cf_overloads = [];
  2705. } in
  2706. e.e_constrs <- PMap.add f.ef_name f e.e_constrs;
  2707. fields := PMap.add cf.cf_name cf !fields;
  2708. incr index;
  2709. names := c.ec_name :: !names;
  2710. ) (!constructs);
  2711. e.e_names <- List.rev !names;
  2712. e.e_extern <- e.e_extern;
  2713. e.e_type.t_params <- e.e_params;
  2714. e.e_type.t_type <- TAnon {
  2715. a_fields = !fields;
  2716. a_status = ref (EnumStatics e);
  2717. };
  2718. if !is_flat then e.e_meta <- (Meta.FlatEnum,[],e.e_pos) :: e.e_meta;
  2719. | ETypedef d ->
  2720. let t = (match get_type d.d_name with TTypeDecl t -> t | _ -> assert false) in
  2721. check_global_metadata ctx (fun m -> t.t_meta <- m :: t.t_meta) t.t_module.m_path t.t_path None;
  2722. let ctx = { ctx with type_params = t.t_params } in
  2723. let tt = load_complex_type ctx p d.d_data in
  2724. (*
  2725. we exceptionnaly allow follow here because we don't care the type we get as long as it's not our own
  2726. *)
  2727. (match d.d_data with
  2728. | CTExtend _ -> ()
  2729. | _ ->
  2730. if t.t_type == follow tt then error "Recursive typedef is not allowed" p);
  2731. (match t.t_type with
  2732. | TMono r ->
  2733. (match !r with
  2734. | None -> r := Some tt;
  2735. | Some _ -> assert false);
  2736. | _ -> assert false);
  2737. | EAbstract d ->
  2738. let a = (match get_type d.d_name with TAbstractDecl a -> a | _ -> assert false) in
  2739. check_global_metadata ctx (fun m -> a.a_meta <- m :: a.a_meta) a.a_module.m_path a.a_path None;
  2740. let ctx = { ctx with type_params = a.a_params } in
  2741. let is_type = ref false in
  2742. let load_type t from =
  2743. let t = load_complex_type ctx p t in
  2744. let t = if not (Meta.has Meta.CoreType a.a_meta) then begin
  2745. if !is_type then begin
  2746. let r = exc_protect ctx (fun r ->
  2747. r := (fun() -> t);
  2748. let at = monomorphs a.a_params a.a_this in
  2749. (try (if from then Type.unify t at else Type.unify at t) with Unify_error _ -> error "You can only declare from/to with compatible types" p);
  2750. t
  2751. ) "constraint" in
  2752. delay ctx PForce (fun () -> ignore(!r()));
  2753. TLazy r
  2754. end else
  2755. error "Missing underlying type declaration or @:coreType declaration" p;
  2756. end else begin
  2757. if Meta.has Meta.Callable a.a_meta then
  2758. error "@:coreType abstracts cannot be @:callable" p;
  2759. t
  2760. end in
  2761. t
  2762. in
  2763. List.iter (function
  2764. | AFromType t -> a.a_from <- (load_type t true) :: a.a_from
  2765. | AToType t -> a.a_to <- (load_type t false) :: a.a_to
  2766. | AIsType t ->
  2767. if a.a_impl = None then error "Abstracts with underlying type must have an implementation" a.a_pos;
  2768. if Meta.has Meta.CoreType a.a_meta then error "@:coreType abstracts cannot have an underlying type" p;
  2769. let at = load_complex_type ctx p t in
  2770. (match at with TAbstract(a2,_) when a == a2 -> error "Abstract underlying type cannot be recursive" a.a_pos | _ -> ());
  2771. a.a_this <- at;
  2772. is_type := true;
  2773. | APrivAbstract -> ()
  2774. ) d.d_flags;
  2775. if not !is_type then begin
  2776. if Meta.has Meta.CoreType a.a_meta then
  2777. a.a_this <- TAbstract(a,List.map snd a.a_params)
  2778. else
  2779. error "Abstract is missing underlying type declaration" a.a_pos
  2780. end
  2781. let type_module ctx m file ?(is_extern=false) tdecls p =
  2782. let m, decls, tdecls = make_module ctx m file tdecls p in
  2783. if is_extern then m.m_extra.m_kind <- MExtern;
  2784. add_module ctx m p;
  2785. (* define the per-module context for the next pass *)
  2786. let ctx = {
  2787. com = ctx.com;
  2788. g = ctx.g;
  2789. t = ctx.t;
  2790. m = {
  2791. curmod = m;
  2792. module_types = ctx.g.std.m_types;
  2793. module_using = [];
  2794. module_globals = PMap.empty;
  2795. wildcard_packages = [];
  2796. };
  2797. meta = [];
  2798. this_stack = [];
  2799. with_type_stack = [];
  2800. call_argument_stack = [];
  2801. pass = PBuildModule;
  2802. on_error = (fun ctx msg p -> ctx.com.error msg p);
  2803. macro_depth = ctx.macro_depth;
  2804. curclass = null_class;
  2805. curfield = null_field;
  2806. tthis = ctx.tthis;
  2807. ret = ctx.ret;
  2808. locals = PMap.empty;
  2809. type_params = [];
  2810. curfun = FunStatic;
  2811. untyped = false;
  2812. in_super_call = false;
  2813. in_macro = ctx.in_macro;
  2814. in_display = false;
  2815. in_loop = false;
  2816. opened = [];
  2817. vthis = None;
  2818. } in
  2819. if ctx.g.std != null_module then begin
  2820. add_dependency m ctx.g.std;
  2821. (* this will ensure both String and (indirectly) Array which are basic types which might be referenced *)
  2822. ignore(load_core_type ctx "String");
  2823. end;
  2824. (* here is an additional PASS 1 phase, which define the type parameters for all module types.
  2825. Constraints are handled lazily (no other type is loaded) because they might be recursive anyway *)
  2826. List.iter (fun d ->
  2827. match d with
  2828. | (TClassDecl c, (EClass d, p)) ->
  2829. c.cl_params <- List.map (type_type_params ctx c.cl_path (fun() -> c.cl_params) p) d.d_params;
  2830. | (TEnumDecl e, (EEnum d, p)) ->
  2831. e.e_params <- List.map (type_type_params ctx e.e_path (fun() -> e.e_params) p) d.d_params;
  2832. | (TTypeDecl t, (ETypedef d, p)) ->
  2833. t.t_params <- List.map (type_type_params ctx t.t_path (fun() -> t.t_params) p) d.d_params;
  2834. | (TAbstractDecl a, (EAbstract d, p)) ->
  2835. a.a_params <- List.map (type_type_params ctx a.a_path (fun() -> a.a_params) p) d.d_params;
  2836. | _ ->
  2837. assert false
  2838. ) decls;
  2839. (* setup module types *)
  2840. let context_init = ref [] in
  2841. let do_init() =
  2842. match !context_init with
  2843. | [] -> ()
  2844. | l -> context_init := []; List.iter (fun f -> f()) (List.rev l)
  2845. in
  2846. List.iter (init_module_type ctx context_init do_init) tdecls;
  2847. m
  2848. let resolve_module_file com m remap p =
  2849. let forbid = ref false in
  2850. let file = (match m with
  2851. | [] , name -> name
  2852. | x :: l , name ->
  2853. let x = (try
  2854. match PMap.find x com.package_rules with
  2855. | Forbidden -> forbid := true; x
  2856. | Directory d -> d
  2857. | Remap d -> remap := d :: l; d
  2858. with Not_found -> x
  2859. ) in
  2860. String.concat "/" (x :: l) ^ "/" ^ name
  2861. ) ^ ".hx" in
  2862. let file = Common.find_file com file in
  2863. let file = (match String.lowercase (snd m) with
  2864. | "con" | "aux" | "prn" | "nul" | "com1" | "com2" | "com3" | "lpt1" | "lpt2" | "lpt3" when Sys.os_type = "Win32" ->
  2865. (* these names are reserved by the OS - old DOS legacy, such files cannot be easily created but are reported as visible *)
  2866. if (try (Unix.stat file).Unix.st_size with _ -> 0) > 0 then file else raise Not_found
  2867. | _ -> file
  2868. ) in
  2869. (* if we try to load a std.xxxx class and resolve a real std file, the package name is not valid, ignore *)
  2870. (match fst m with
  2871. | "std" :: _ ->
  2872. let file = Common.unique_full_path file in
  2873. if List.exists (fun path -> ExtString.String.starts_with file (try Common.unique_full_path path with _ -> path)) com.std_path then raise Not_found;
  2874. | _ -> ());
  2875. if !forbid then begin
  2876. let _, decls = (!parse_hook) com file p in
  2877. let rec loop decls = match decls with
  2878. | ((EImport _,_) | (EUsing _,_)) :: decls -> loop decls
  2879. | (EClass d,_) :: _ -> d.d_meta
  2880. | (EEnum d,_) :: _ -> d.d_meta
  2881. | (EAbstract d,_) :: _ -> d.d_meta
  2882. | (ETypedef d,_) :: _ -> d.d_meta
  2883. | [] -> []
  2884. in
  2885. let meta = loop decls in
  2886. if not (Meta.has Meta.NoPackageRestrict meta) then begin
  2887. let x = (match fst m with [] -> assert false | x :: _ -> x) in
  2888. raise (Forbid_package ((x,m,p),[],if Common.defined com Define.Macro then "macro" else platform_name com.platform));
  2889. end;
  2890. end;
  2891. file
  2892. let parse_module ctx m p =
  2893. let remap = ref (fst m) in
  2894. let file = resolve_module_file ctx.com m remap p in
  2895. let pack, decls = (!parse_hook) ctx.com file p in
  2896. if pack <> !remap then begin
  2897. let spack m = if m = [] then "<empty>" else String.concat "." m in
  2898. if p == Ast.null_pos then
  2899. display_error ctx ("Invalid commandline class : " ^ s_type_path m ^ " should be " ^ s_type_path (pack,snd m)) p
  2900. else
  2901. display_error ctx ("Invalid package : " ^ spack (fst m) ^ " should be " ^ spack pack) p
  2902. end;
  2903. file, if !remap <> fst m then
  2904. (* build typedefs to redirect to real package *)
  2905. List.rev (List.fold_left (fun acc (t,p) ->
  2906. let build f d =
  2907. let priv = List.mem f d.d_flags in
  2908. (ETypedef {
  2909. d_name = d.d_name;
  2910. d_doc = None;
  2911. d_meta = [];
  2912. d_params = d.d_params;
  2913. d_flags = if priv then [EPrivate] else [];
  2914. d_data = CTPath (if priv then { tpackage = []; tname = "Dynamic"; tparams = []; tsub = None; } else
  2915. {
  2916. tpackage = !remap;
  2917. tname = d.d_name;
  2918. tparams = List.map (fun tp ->
  2919. TPType (CTPath { tpackage = []; tname = tp.tp_name; tparams = []; tsub = None; })
  2920. ) d.d_params;
  2921. tsub = None;
  2922. });
  2923. },p) :: acc
  2924. in
  2925. match t with
  2926. | EClass d -> build HPrivate d
  2927. | EEnum d -> build EPrivate d
  2928. | ETypedef d -> build EPrivate d
  2929. | EAbstract d -> build APrivAbstract d
  2930. | EImport _ | EUsing _ -> acc
  2931. ) [(EImport (List.map (fun s -> s,null_pos) (!remap @ [snd m]),INormal),null_pos)] decls)
  2932. else
  2933. decls
  2934. let load_module ctx m p =
  2935. let m2 = (try
  2936. Hashtbl.find ctx.g.modules m
  2937. with
  2938. Not_found ->
  2939. match !type_module_hook ctx m p with
  2940. | Some m -> m
  2941. | None ->
  2942. let is_extern = ref false in
  2943. let file, decls = (try
  2944. parse_module ctx m p
  2945. with Not_found ->
  2946. let rec loop = function
  2947. | [] ->
  2948. raise (Error (Module_not_found m,p))
  2949. | load :: l ->
  2950. match load m p with
  2951. | None -> loop l
  2952. | Some (file,(_,a)) -> file, a
  2953. in
  2954. is_extern := true;
  2955. loop ctx.com.load_extern_type
  2956. ) in
  2957. let is_extern = !is_extern in
  2958. try
  2959. type_module ctx m file ~is_extern decls p
  2960. with Forbid_package (inf,pl,pf) when p <> Ast.null_pos ->
  2961. raise (Forbid_package (inf,p::pl,pf))
  2962. ) in
  2963. add_dependency ctx.m.curmod m2;
  2964. if ctx.pass = PTypeField then flush_pass ctx PBuildClass "load_module";
  2965. m2
  2966. ;;
  2967. type_function_params_rec := type_function_params