typeload.ml 125 KB

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