typeload.ml 125 KB

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