typecore.ml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. (*
  2. * Haxe Compiler
  3. * Copyright (c)2005-2008 Nicolas Cannasse
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *)
  19. open Common
  20. open Type
  21. type type_patch = {
  22. mutable tp_type : Ast.complex_type option;
  23. mutable tp_remove : bool;
  24. mutable tp_meta : Ast.metadata;
  25. }
  26. type current_fun =
  27. | FMember
  28. | FStatic
  29. | FConstructor
  30. | FMemberLocal
  31. type macro_mode =
  32. | MExpr
  33. | MBuild
  34. | MMacroType
  35. type delayed_functions = {
  36. mutable df_normal : (unit -> unit) list;
  37. mutable df_late : (unit -> unit) list;
  38. }
  39. type typer_globals = {
  40. types_module : (path, path) Hashtbl.t;
  41. modules : (path , module_def) Hashtbl.t;
  42. mutable delayed : delayed_functions;
  43. doinline : bool;
  44. mutable core_api : typer option;
  45. mutable macros : ((unit -> unit) * typer) option;
  46. mutable std : module_def;
  47. mutable hook_generate : (unit -> unit) list;
  48. type_patches : (path, (string * bool, type_patch) Hashtbl.t * type_patch) Hashtbl.t;
  49. mutable get_build_infos : unit -> (module_type * Ast.class_field list) option;
  50. (* api *)
  51. do_inherit : typer -> Type.tclass -> Ast.pos -> Ast.class_flag -> bool;
  52. do_create : Common.context -> typer;
  53. do_macro : typer -> macro_mode -> path -> string -> Ast.expr list -> Ast.pos -> Ast.expr option;
  54. do_load_module : typer -> path -> pos -> module_def;
  55. do_optimize : typer -> texpr -> texpr;
  56. do_build_instance : typer -> module_type -> pos -> ((string * t) list * path * (t list -> t));
  57. }
  58. and typer = {
  59. (* shared *)
  60. com : context;
  61. mutable t : basic_types;
  62. g : typer_globals;
  63. mutable in_macro : bool;
  64. mutable macro_depth : int;
  65. (* per-module *)
  66. current : module_def;
  67. mutable local_types : module_type list;
  68. mutable local_using : module_type list;
  69. (* per-class *)
  70. mutable curclass : tclass;
  71. mutable tthis : t;
  72. mutable type_params : (string * t) list;
  73. (* per-function *)
  74. mutable curmethod : string;
  75. mutable untyped : bool;
  76. mutable in_super_call : bool;
  77. mutable in_loop : bool;
  78. mutable in_display : bool;
  79. mutable curfun : current_fun;
  80. mutable ret : t;
  81. mutable locals : (string, tvar) PMap.t;
  82. mutable opened : anon_status ref list;
  83. mutable param_type : t option;
  84. mutable vthis : tvar option;
  85. }
  86. type error_msg =
  87. | Module_not_found of path
  88. | Type_not_found of path * string
  89. | Unify of unify_error list
  90. | Custom of string
  91. | Unknown_ident of string
  92. | Stack of error_msg * error_msg
  93. | Forbid_package of string * path
  94. exception Fatal_error
  95. exception Error of error_msg * pos
  96. let type_expr_ref : (typer -> Ast.expr -> bool -> texpr) ref = ref (fun _ _ _ -> assert false)
  97. let unify_min_ref : (typer -> texpr list -> t) ref = ref (fun _ _ -> assert false)
  98. let type_expr_with_type_ref : (typer -> Ast.expr -> t option -> bool -> texpr) ref = ref (fun _ _ _ -> assert false)
  99. let unify_error_msg ctx = function
  100. | Cannot_unify (t1,t2) ->
  101. s_type ctx t1 ^ " should be " ^ s_type ctx t2
  102. | Invalid_field_type s ->
  103. "Invalid type for field " ^ s ^ " :"
  104. | Has_no_field (t,n) ->
  105. s_type ctx t ^ " has no field " ^ n
  106. | Has_no_runtime_field (t,n) ->
  107. s_type ctx t ^ "." ^ n ^ " is not accessible at runtime"
  108. | Has_extra_field (t,n) ->
  109. s_type ctx t ^ " has extra field " ^ n
  110. | Invalid_kind (f,a,b) ->
  111. (match a, b with
  112. | Var va, Var vb ->
  113. let name, stra, strb = if va.v_read = vb.v_read then
  114. "setter", s_access va.v_write, s_access vb.v_write
  115. else if va.v_write = vb.v_write then
  116. "getter", s_access va.v_read, s_access vb.v_read
  117. else
  118. "access", "(" ^ s_access va.v_read ^ "," ^ s_access va.v_write ^ ")", "(" ^ s_access vb.v_read ^ "," ^ s_access vb.v_write ^ ")"
  119. in
  120. "Inconsistent " ^ name ^ " for field " ^ f ^ " : " ^ stra ^ " should be " ^ strb
  121. | _ ->
  122. "Field " ^ f ^ " is " ^ s_kind a ^ " but should be " ^ s_kind b)
  123. | Invalid_visibility n ->
  124. "The field " ^ n ^ " is not public"
  125. | Not_matching_optional n ->
  126. "Optional attribute of parameter " ^ n ^ " differs"
  127. | Cant_force_optional ->
  128. "Optional parameters can't be forced"
  129. | Invariant_parameter _ ->
  130. "Type parameters are invariant"
  131. | Constraint_failure name ->
  132. "Constraint check failure for " ^ name
  133. | Missing_overload (cf, t) ->
  134. cf.cf_name ^ " has no overload for " ^ s_type ctx t
  135. | Unify_custom msg ->
  136. msg
  137. let rec error_msg = function
  138. | Module_not_found m -> "Class not found : " ^ Ast.s_type_path m
  139. | Type_not_found (m,t) -> "Module " ^ Ast.s_type_path m ^ " does not define type " ^ t
  140. | Unify l ->
  141. let ctx = print_context() in
  142. String.concat "\n" (List.map (unify_error_msg ctx) l)
  143. | Unknown_ident s -> "Unknown identifier : " ^ s
  144. | Custom s -> s
  145. | Stack (m1,m2) -> error_msg m1 ^ "\n" ^ error_msg m2
  146. | Forbid_package (p,m) ->
  147. "You can't access the " ^ p ^ " package with current compilation flags (for " ^ Ast.s_type_path m ^ ")"
  148. let display_error ctx msg p = ctx.com.error msg p
  149. let error msg p = raise (Error (Custom msg,p))
  150. let type_expr ctx e need_val = (!type_expr_ref) ctx e need_val
  151. let unify_min ctx el = (!unify_min_ref) ctx el
  152. let type_expr_with_type ctx e t do_raise = (!type_expr_with_type_ref) ctx e t do_raise
  153. let unify ctx t1 t2 p =
  154. try
  155. Type.unify t1 t2
  156. with
  157. Unify_error l ->
  158. if not ctx.untyped then display_error ctx (error_msg (Unify l)) p
  159. let unify_raise ctx t1 t2 p =
  160. try
  161. Type.unify t1 t2
  162. with
  163. Unify_error l ->
  164. (* no untyped check *)
  165. raise (Error (Unify l,p))
  166. let exc_protect ctx f =
  167. let rec r = ref (fun() ->
  168. try
  169. f r
  170. with
  171. | Error (m,p) ->
  172. display_error ctx (error_msg m) p;
  173. raise Fatal_error
  174. ) in
  175. r
  176. let save_locals ctx =
  177. let locals = ctx.locals in
  178. (fun() -> ctx.locals <- locals)
  179. let add_local ctx n t =
  180. let v = alloc_var n t in
  181. ctx.locals <- PMap.add n v ctx.locals;
  182. v
  183. let gen_local_prefix = "`"
  184. let gen_local ctx t =
  185. (* ensure that our generated local does not mask an existing one *)
  186. let rec loop n =
  187. let nv = (if n = 0 then gen_local_prefix else gen_local_prefix ^ string_of_int n) in
  188. if PMap.mem nv ctx.locals then
  189. loop (n+1)
  190. else
  191. nv
  192. in
  193. add_local ctx (loop 0) t
  194. let not_opened = ref Closed
  195. let mk_anon fl = TAnon { a_fields = fl; a_status = not_opened; }
  196. let delay ctx f =
  197. ctx.g.delayed.df_normal <- f :: ctx.g.delayed.df_normal
  198. let delay_late ctx f =
  199. ctx.g.delayed.df_late <- f :: ctx.g.delayed.df_late
  200. let mk_field name t p = {
  201. cf_name = name;
  202. cf_type = t;
  203. cf_pos = p;
  204. cf_doc = None;
  205. cf_meta = no_meta;
  206. cf_public = true;
  207. cf_kind = Var { v_read = AccNormal; v_write = AccNormal };
  208. cf_expr = None;
  209. cf_params = [];
  210. cf_overloads = [];
  211. }
  212. let fake_modules = Hashtbl.create 0
  213. let create_fake_module ctx file =
  214. let file = Common.unique_full_path file in
  215. let mdep = (try Hashtbl.find fake_modules file with Not_found ->
  216. let mdep = {
  217. m_id = alloc_mid();
  218. m_path = (["$DEP"],file);
  219. m_types = [];
  220. m_extra = module_extra file (Common.get_signature ctx.com) (file_time file) MFake;
  221. } in
  222. Hashtbl.add fake_modules file mdep;
  223. mdep
  224. ) in
  225. Hashtbl.replace ctx.g.modules mdep.m_path mdep;
  226. mdep