typeload.ml 123 KB

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