typeload.ml 123 KB

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