typerEntry.ml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. open Globals
  2. open Common
  3. open Type
  4. open Typecore
  5. open Typer
  6. open Resolution
  7. open Error
  8. let create com macros =
  9. let rec ctx = {
  10. com = com;
  11. t = com.basic;
  12. g = {
  13. core_api = None;
  14. macros = macros;
  15. module_check_policies = [];
  16. delayed = Array.init TyperPass.all_typer_passes_length (fun _ -> { tasks = []});
  17. delayed_min_index = 0;
  18. debug_delayed = [];
  19. doinline = com.display.dms_inline && not (Common.defined com Define.NoInline);
  20. retain_meta = Common.defined com Define.RetainUntypedMeta;
  21. std_types = null_module;
  22. global_using = [];
  23. complete = false;
  24. type_hints = [];
  25. load_only_cached_modules = false;
  26. return_partial_type = false;
  27. build_count = 0;
  28. t_dynamic_def = t_dynamic;
  29. do_macro = MacroContext.type_macro;
  30. do_load_macro = MacroContext.load_macro';
  31. do_load_module = TypeloadModule.load_module;
  32. do_load_type_def = Typeload.load_type_def;
  33. get_build_info = InstanceBuilder.get_build_info;
  34. do_format_string = format_string;
  35. do_load_core_class = Typeload.load_core_class;
  36. delayed_display = None;
  37. root_typer = ctx;
  38. };
  39. m = {
  40. curmod = null_module;
  41. import_resolution = new resolution_list ["import";"typer"];
  42. own_resolution = None;
  43. enum_with_type = None;
  44. module_using = [];
  45. import_statements = [];
  46. is_display_file = false;
  47. };
  48. c = {
  49. curclass = null_class;
  50. tthis = t_dynamic;
  51. get_build_infos = (fun() -> None);
  52. };
  53. f = TyperManager.create_ctx_f null_field;
  54. e = TyperManager.create_ctx_e FunStatic false;
  55. pass = PBuildModule;
  56. allow_inline = true;
  57. allow_transform = true;
  58. type_params = [];
  59. memory_marker = Typecore.memory_marker;
  60. } in
  61. ctx.g.std_types <- (try
  62. TypeloadModule.load_module ctx ([],"StdTypes") null_pos
  63. with
  64. Error { err_message = Module_not_found ([],"StdTypes") } ->
  65. Error.raise_std_not_found ()
  66. );
  67. (* We always want core types to be available so we add them as default imports (issue #1904 and #3131). *)
  68. List.iter (fun mt ->
  69. ctx.m.import_resolution#add (module_type_resolution mt None null_pos))
  70. (List.rev ctx.g.std_types.m_types);
  71. List.iter (fun t ->
  72. match t with
  73. | TAbstractDecl a ->
  74. (match snd a.a_path with
  75. | "Void" ->
  76. let t = TAbstract (a,[]) in
  77. Type.unify t ctx.t.tvoid;
  78. ctx.t.tvoid <- t;
  79. | "Float" ->
  80. let t = (TAbstract (a,[])) in
  81. Type.unify t ctx.t.tfloat;
  82. ctx.t.tfloat <- t
  83. | "Int" ->
  84. let t = (TAbstract (a,[])) in
  85. Type.unify t ctx.t.tint;
  86. ctx.t.tint <- t
  87. | "Bool" ->
  88. let t = (TAbstract (a,[])) in
  89. Type.unify t ctx.t.tbool;
  90. ctx.t.tbool <- t
  91. | "Dynamic" ->
  92. ctx.g.t_dynamic_def <- TAbstract(a,extract_param_types a.a_params);
  93. | "Null" ->
  94. let mk_null t =
  95. try
  96. if not (is_null ~no_lazy:true t || is_explicit_null t) then TAbstract (a,[t]) else t
  97. with Exit ->
  98. (* don't force lazy evaluation *)
  99. let r = ref (lazy_available t_dynamic) in
  100. r := lazy_wait (fun() ->
  101. let t = (if not (is_null t) then TAbstract (a,[t]) else t) in
  102. r := lazy_available t;
  103. t
  104. );
  105. TLazy r
  106. in
  107. ctx.t.tnull <- mk_null;
  108. | _ -> ())
  109. | TTypeDecl td ->
  110. begin match snd td.t_path with
  111. | "Iterator" ->
  112. ctx.t.titerator <- (fun t -> TType(td,[t]))
  113. | _ ->
  114. ()
  115. end
  116. | TEnumDecl _ | TClassDecl _ ->
  117. ()
  118. ) ctx.g.std_types.m_types;
  119. let m = TypeloadModule.load_module ctx ([],"String") null_pos in
  120. List.iter (fun mt -> match mt with
  121. | TClassDecl ({cl_path = ([],"String")} as c) ->
  122. let t = (TInst (c,[])) in
  123. Type.unify t ctx.t.tstring;
  124. ctx.t.tstring <- t
  125. | _ -> ()
  126. ) m.m_types;
  127. let m = TypeloadModule.load_module ctx ([],"Std") null_pos in
  128. List.iter (fun mt -> match mt with
  129. | TClassDecl ({cl_path = ([],"Std")} as c) -> ctx.com.std <- c;
  130. | _ -> ()
  131. ) m.m_types;
  132. let m = TypeloadModule.load_module ctx ([],"Any") null_pos in
  133. List.iter (fun mt -> match mt with
  134. | TAbstractDecl a ->
  135. (match snd a.a_path with
  136. | "Any" ->
  137. let t = TAbstract (a,[]) in
  138. Type.unify t ctx.t.tany;
  139. ctx.t.tany <- t;
  140. | _ -> ())
  141. | _ -> ()
  142. ) m.m_types;
  143. let m = TypeloadModule.load_module ctx ([],"Array") null_pos in
  144. (try
  145. List.iter (fun t -> (
  146. match t with
  147. | TClassDecl ({cl_path = ([],"Array")} as c) ->
  148. ctx.t.tarray <- (fun t -> TInst (c,[t]));
  149. raise Exit
  150. | _ -> ()
  151. )) m.m_types;
  152. die "" __LOC__
  153. with Exit -> ());
  154. let m = TypeloadModule.load_module ctx (["haxe"],"EnumTools") null_pos in
  155. (match m.m_types with
  156. | [TClassDecl c1;TClassDecl c2] -> ctx.g.global_using <- (c1,c1.cl_pos) :: (c2,c2.cl_pos) :: ctx.g.global_using
  157. | [TClassDecl c1] ->
  158. let m = TypeloadModule.load_module ctx (["haxe"],"EnumWithType.valueTools") null_pos in
  159. (match m.m_types with
  160. | [TClassDecl c2 ] -> ctx.g.global_using <- (c1,c1.cl_pos) :: (c2,c2.cl_pos) :: ctx.g.global_using
  161. | _ -> die "" __LOC__);
  162. | _ -> die "" __LOC__);
  163. ignore(TypeloadModule.load_module ctx (["haxe"],"Exception") null_pos);
  164. ctx.g.complete <- true;
  165. ctx
  166. ;;
  167. create_context_ref := create;
  168. Inline.maybe_reapply_overload_call_ref := CallUnification.maybe_reapply_overload_call;