globals.ml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. type pos = {
  2. pfile : string;
  3. pmin : int;
  4. pmax : int;
  5. }
  6. type path = string list * string
  7. type located =
  8. | Message of string * pos
  9. | Stack of located list
  10. module IntMap = Ptmap
  11. module StringMap = Map.Make(struct type t = string let compare = String.compare end)
  12. module Int32Map = Map.Make(struct type t = Int32.t let compare = Int32.compare end)
  13. type platform =
  14. | Cross
  15. | Js
  16. | Lua
  17. | Neko
  18. | Flash
  19. | Php
  20. | Cpp
  21. | Cs
  22. | Java
  23. | Python
  24. | Hl
  25. | Eval
  26. let version = 4304
  27. let version_major = version / 1000
  28. let version_minor = (version mod 1000) / 100
  29. let version_revision = (version mod 100)
  30. let version_pre = None
  31. let null_pos = { pfile = "?"; pmin = -1; pmax = -1 }
  32. let located msg p = Message (msg,p)
  33. let located_stack stack = Stack stack
  34. let rec extract_located = function
  35. | Message (msg,p) -> [(msg, p)]
  36. | Stack stack -> List.fold_left (fun acc s -> acc @ (extract_located s)) [] stack
  37. let rec relocate msg p = match msg with
  38. | Message (msg,_) -> Message (msg,p)
  39. | Stack [] -> Stack []
  40. | Stack (hd :: tl) -> Stack ((relocate hd p) :: tl)
  41. let rec extract_located_pos = function
  42. | Message (_,p) -> p
  43. | Stack [] -> null_pos
  44. | Stack (hd :: _) -> extract_located_pos hd
  45. let macro_platform = ref Neko
  46. let return_partial_type = ref false
  47. let is_windows = Sys.os_type = "Win32" || Sys.os_type = "Cygwin"
  48. let platforms = [
  49. Js;
  50. Lua;
  51. Neko;
  52. Flash;
  53. Php;
  54. Cpp;
  55. Cs;
  56. Java;
  57. Python;
  58. Hl;
  59. Eval;
  60. ]
  61. (** Expected to match `haxe.display.Display.Platform`. *)
  62. let platform_name = function
  63. | Cross -> "cross"
  64. | Js -> "js"
  65. | Lua -> "lua"
  66. | Neko -> "neko"
  67. | Flash -> "flash"
  68. | Php -> "php"
  69. | Cpp -> "cpp"
  70. | Cs -> "cs"
  71. | Java -> "java"
  72. | Python -> "python"
  73. | Hl -> "hl"
  74. | Eval -> "eval"
  75. let parse_platform = function
  76. | "cross" -> Cross
  77. | "js" -> Js
  78. | "lua" -> Lua
  79. | "neko" -> Neko
  80. | "flash" -> Flash
  81. | "php" -> Php
  82. | "cpp" -> Cpp
  83. | "cs" -> Cs
  84. | "java" -> Java
  85. | "python" -> Python
  86. | "hl" -> Hl
  87. | "eval" -> Eval
  88. | p -> raise (failwith ("invalid platform " ^ p))
  89. let platform_list_help = function
  90. | [] -> ""
  91. | [p] -> " (" ^ platform_name p ^ " only)"
  92. | pl -> " (for " ^ String.concat "," (List.map platform_name pl) ^ ")"
  93. let mk_zero_range_pos p = { p with pmax = p.pmin }
  94. let s_type_path (p,s) = match p with [] -> s | _ -> String.concat "." p ^ "." ^ s
  95. let starts_with s c =
  96. String.length s > 0 && s.[0] = c
  97. let get_error_pos_ref : ((string -> int -> string) -> pos -> string) ref = ref (fun printer p ->
  98. Printf.sprintf "%s: characters %d-%d" p.pfile p.pmin p.pmax
  99. )
  100. let s_version =
  101. let pre = Option.map_default (fun pre -> "-" ^ pre) "" version_pre in
  102. Printf.sprintf "%d.%d.%d%s" version_major version_minor version_revision pre
  103. let s_version_full =
  104. match Version.version_extra with
  105. | Some (_,build) -> s_version ^ "+" ^ build
  106. | _ -> s_version
  107. let patch_string_pos p s = { p with pmin = p.pmax - String.length s }
  108. (**
  109. Terminates compiler process and prints user-friendly instructions about filing an issue.
  110. Usage: `die message __LOC__`, where `__LOC__` is a built-in ocaml constant
  111. *)
  112. let die ?p msg ml_loc =
  113. let msg =
  114. let str_pos, expr_msg =
  115. match p with
  116. | None -> "", ""
  117. | Some p -> ((!get_error_pos_ref (Printf.sprintf "%s:%d:") p) ^ " "), "the expression example and "
  118. in
  119. str_pos ^ "Compiler failure" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n"
  120. ^ str_pos ^ "Please submit an issue at https://github.com/HaxeFoundation/haxe/issues/new\n"
  121. ^ str_pos ^ "Attach " ^ expr_msg ^ "the following information:"
  122. in
  123. let backtrace = Printexc.raw_backtrace_to_string (Printexc.get_callstack 21) in
  124. let backtrace =
  125. try snd (ExtString.String.split backtrace "\n")
  126. with ExtString.Invalid_string -> backtrace
  127. in
  128. let ver = s_version_full
  129. and os_type = if Sys.unix then "unix" else "windows" in
  130. Printf.eprintf "%s\nHaxe: %s; OS type: %s;\n%s\n%s" msg ver os_type ml_loc backtrace;
  131. assert false
  132. module MessageSeverity = struct
  133. type t =
  134. | Error
  135. | Warning
  136. | Information
  137. | Hint
  138. let to_int = function
  139. | Error -> 1
  140. | Warning -> 2
  141. | Information -> 3
  142. | Hint -> 4
  143. end
  144. module MessageKind = struct
  145. type t =
  146. | DKUnusedImport
  147. | DKUnresolvedIdentifier
  148. | DKCompilerMessage
  149. | DKRemovableCode
  150. | DKParserError
  151. | DKDeprecationWarning
  152. | DKInactiveBlock
  153. | DKMissingFields
  154. let to_int = function
  155. | DKUnusedImport -> 0
  156. | DKUnresolvedIdentifier -> 1
  157. | DKCompilerMessage -> 2
  158. | DKRemovableCode -> 3
  159. | DKParserError -> 4
  160. | DKDeprecationWarning -> 5
  161. | DKInactiveBlock -> 6
  162. | DKMissingFields -> 7
  163. end
  164. type compiler_message = {
  165. cm_message : string;
  166. cm_pos : pos;
  167. cm_depth : int;
  168. cm_kind : MessageKind.t;
  169. cm_severity : MessageSeverity.t;
  170. }
  171. let make_compiler_message msg p depth kind sev = {
  172. cm_message = msg;
  173. cm_pos = p;
  174. cm_depth = depth;
  175. cm_kind = kind;
  176. cm_severity = sev;
  177. }
  178. let i32_31 = Int32.of_int 31