typecheck.ml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. (*
  2. * Do a number of checks:
  3. * - A void function must not return a value.
  4. * - A non-void function must return a value of the correct type.
  5. * - Array indices must be of type integer.
  6. * - The number of array indices must match the number of array dimensions.
  7. * - The type on the right-hand side of an assignment must match the type on
  8. * the left-hand side.
  9. * - The number of arguments used for a function call must match the number of
  10. * parameters for that function.
  11. * - The types of the function arguments must match the types of parameters.
  12. * - The operands of a unary or binary operation must have valid types.
  13. * - The predicate expression of an if, while, or do-while statement must be
  14. * a boolean.
  15. * - Only values of a basic type can be type cast.
  16. *)
  17. open Printf
  18. open Types
  19. open Util
  20. open Stringify
  21. (* Stringify a list of types for use in error messages.
  22. * ctype list -> string *)
  23. let rec types2str = function
  24. | [] -> ""
  25. | [ctype] -> type2str ctype
  26. | ctype :: tail -> type2str ctype ^ " or " ^ (types2str tail)
  27. let array_depth = function
  28. | ArrayDims (_, dims) -> List.length dims
  29. | _ -> raise InvalidNode
  30. let spec = function
  31. | ArrayDims (ctype, dims) -> (ctype, List.length dims)
  32. | ctype -> (ctype, 0)
  33. let type2str_error = function
  34. | ArrayDims (ctype, dims) ->
  35. type2str ctype ^ "[" ^ repeat "," (List.length dims - 1) ^ "]"
  36. | ctype ->
  37. type2str ctype
  38. let check_type ?(msg="") expected node =
  39. let got = typeof node in
  40. if (spec got) <> (spec expected) then begin
  41. let msg = match msg with
  42. | "" -> sprintf "type mismatch: expected type %s, got %s"
  43. (type2str_error expected) (type2str_error got)
  44. | _ -> msg
  45. in
  46. raise (NodeError (node, msg))
  47. end
  48. let op_types = function
  49. | Not | And | Or -> [Bool]
  50. | Mod -> [Int]
  51. | Neg | Sub | Div | Lt | Le | Gt | Ge -> [Int; Float]
  52. | Add | Mul | Eq | Ne -> [Bool; Int; Float]
  53. let op_result_type opnd_type = function
  54. | Not | And | Or | Eq | Ne | Lt | Le | Gt | Ge -> Bool
  55. | Neg | Add | Sub | Mul | Div | Mod -> opnd_type
  56. (* Check if the given operator can be applied to the given type *)
  57. let check_type_op allowed_types desc node =
  58. let got = typeof node in
  59. if not (List.mem got allowed_types) then (
  60. let msg = sprintf
  61. "%s cannot be applied to type %s, only to %s"
  62. desc (type2str got) (types2str allowed_types)
  63. in
  64. raise (NodeError (node, msg))
  65. ); ()
  66. let check_dims_match dims dec_type errnode =
  67. match (List.length dims, array_depth dec_type) with
  68. | (got, expected) when got != expected ->
  69. let msg = sprintf
  70. "dimension mismatch: expected %d indices, got %d" expected got
  71. in
  72. raise (NodeError (errnode, msg))
  73. | _ -> ()
  74. let rec typecheck node =
  75. let check_trav ctype node =
  76. let node = typecheck node in
  77. check_type ctype node;
  78. node
  79. in
  80. match node with
  81. | FunUse ((FunDec (ret_type, name, params, _) as dec), args, ann)
  82. | FunUse ((FunDef (_, ret_type, name, params, _, _) as dec), args, ann) ->
  83. begin
  84. match (List.length args, List.length params) with
  85. | (nargs, nparams) when nargs != nparams ->
  86. let msg = sprintf
  87. "function \"%s\" expects %d arguments, got %d"
  88. name nparams nargs
  89. in
  90. raise (NodeError (node, msg))
  91. | _ ->
  92. let args = List.map typecheck args in
  93. let check_arg_type arg param =
  94. check_type (typeof param) arg;
  95. in
  96. List.iter2 check_arg_type args params;
  97. FunUse (dec, args, Type ret_type :: ann)
  98. end
  99. (* Operators match operand types and get a new type based on the operator *)
  100. | Monop (op, opnd, ann) ->
  101. let opnd = typecheck opnd in
  102. let desc = sprintf "unary operator \"%s\"" (op2str op) in
  103. check_type_op (op_types op) desc opnd;
  104. Monop (op, opnd, Type (op_result_type (typeof opnd) op) :: ann)
  105. | Binop (op, left, right, ann) ->
  106. let left = typecheck left in
  107. let right = typecheck right in
  108. let desc = sprintf "binary operator \"%s\"" (op2str op) in
  109. check_type_op (op_types op) desc left;
  110. check_type (typeof left) right;
  111. (* Check for division by zero *)
  112. begin
  113. match (op, right) with
  114. | (Div, Const (IntVal 0, _)) -> node_warning right "division by zero"
  115. | _ -> ()
  116. end;
  117. Binop (op, left, right, Type (op_result_type (typeof left) op) :: ann)
  118. (* Conditions must be bool, and right-hand type must match left-hand type *)
  119. | Cond (cond, texpr, fexpr, ann) ->
  120. let cond = check_trav Bool cond in
  121. let texpr = typecheck texpr in
  122. let fexpr = check_trav (typeof texpr) fexpr in
  123. Cond (cond, texpr, fexpr, Type (typeof texpr) :: ann)
  124. (* Only basic types can be typecasted *)
  125. | TypeCast (ctype, value, ann) ->
  126. let value = typecheck value in
  127. check_type_op [Bool; Int; Float] "typecast" value;
  128. TypeCast (ctype, value, Type ctype :: ann)
  129. (* Array allocation dimensions must have type int *)
  130. | Allocate (dec, dims, ann) ->
  131. Allocate (dec, List.map (check_trav Int) dims, ann)
  132. (* Array dimensions are always integers *)
  133. | Dim (name, ann) ->
  134. Dim (name, Type Int :: ann)
  135. (* Functions and parameters must be traversed to give types to Dim nodes *)
  136. (*
  137. | FunDec (ret_type, name, params, ann) ->
  138. FunDec (ret_type, name, List.map typecheck params, ann)
  139. | Param (ArrayDims (ctype, dims), name, ann) ->
  140. Param (ArrayDims (ctype, List.map typecheck dims), name, ann)
  141. *)
  142. (* Void functions may have no return statement, other functions must have a
  143. * return statement of valid type *)
  144. | FunDef (export, ret_type, name, params, body, ann) ->
  145. let params = List.map typecheck params in
  146. let body = typecheck body in
  147. let rec find_return = function
  148. | [] -> None
  149. | [Return (value, _) as ret] -> Some (ret, typeof value)
  150. | hd :: tl -> find_return tl
  151. in
  152. begin
  153. match (ret_type, find_return (block_body body)) with
  154. | (Void, Some (ret, _)) ->
  155. raise (NodeError (ret, "void function should not have a return value"))
  156. | ((Bool | Int | Float), None) ->
  157. let msg = sprintf
  158. "expected return value of type %s for function \"%s\""
  159. (type2str ret_type) name
  160. in
  161. raise (NodeError (node, msg))
  162. | ((Bool | Int | Float), Some (ret, t)) when t != ret_type ->
  163. let msg = sprintf
  164. "function \"%s\" has return type %s, got %s"
  165. name (type2str ret_type) (type2str t)
  166. in
  167. raise (NodeError (ret, msg))
  168. | _ -> FunDef (export, ret_type, name, params, body, ann)
  169. end
  170. (* Conditions in must have type bool *)
  171. | If (cond, body, ann) ->
  172. If (check_trav Bool cond, typecheck body, ann)
  173. | IfElse (cond, tbody, fbody, ann) ->
  174. IfElse (check_trav Bool cond, typecheck tbody, typecheck fbody, ann)
  175. | While (cond, body, ann) ->
  176. While (check_trav Bool cond, typecheck body, ann)
  177. | DoWhile (cond, body, ann) ->
  178. DoWhile (check_trav Bool cond, typecheck body, ann)
  179. (* Constants *)
  180. | Const (BoolVal value, ann) ->
  181. Const (BoolVal value, Type Bool :: ann)
  182. | Const (IntVal value, ann) ->
  183. (* Do a bound check on integers (use Int32 because default ints in ocaml
  184. * are 31- or 63-bit *)
  185. let cmpval = Nativeint.of_int value in
  186. let min = Nativeint.of_int32 Int32.min_int in
  187. let max = Nativeint.of_int32 Int32.max_int in
  188. if cmpval < min || cmpval > max then (
  189. raise (NodeError (node, "integer value out of range (signed 32-bit)"))
  190. );
  191. Const (IntVal value, Type Int :: ann)
  192. | Const (FloatVal value, ann) ->
  193. Const (FloatVal value, Type Float :: ann)
  194. (* Extern arrays variables are transformed to imported functions, so the
  195. * pointer cannot be passed *)
  196. | VarUse (GlobalDec (ArrayDims _, _, _), None, _) ->
  197. raise (NodeError (node, "imported array pointers may only be \
  198. dereferenced, not used directly"))
  199. (* Variables inherit the type of their declaration *)
  200. | VarUse (dec, None, ann) ->
  201. VarUse (dec, None, Type (typeof dec) :: ann)
  202. | VarUse (dec, Some dims, ann) ->
  203. let dims = List.map typecheck dims in
  204. List.iter (check_type Int) dims;
  205. check_dims_match dims (typeof dec) node;
  206. VarUse (dec, Some dims, Type (basetypeof dec) :: ann)
  207. (* Array pointers cannot be re-assigned, because array dimension reduction
  208. * makes assumptions about dimensions of an array *)
  209. | VarLet (dec, None, _, _) when is_array dec ->
  210. raise (NodeError (node, "cannot assign value to array pointer after \
  211. initialisation"))
  212. (* Assigned values must match variable declaration *)
  213. | VarLet (dec, None, value, ann) ->
  214. VarLet (dec, None, check_trav (typeof dec) value, ann)
  215. | VarLet (dec, Some dims, value, ann) ->
  216. (* Number of assigned indices must match array definition *)
  217. check_dims_match dims (typeof dec) node;
  218. (* Array indices must be ints *)
  219. let dims = List.map typecheck dims in
  220. List.iter (check_type Int) dims;
  221. (* Assigned value must match array base type *)
  222. let value = typecheck value in
  223. check_type (basetypeof dec) value;
  224. VarLet (dec, Some dims, value, ann)
  225. (* ArrayConst initialisations are transformed during desugaring, so any
  226. * occurrences that are left are illegal *)
  227. | ArrayConst _ ->
  228. raise (NodeError (node, "array constants can only be used in array \
  229. initialisation"))
  230. | _ -> traverse_unit typecheck node
  231. let phase = function
  232. | Ast node -> Ast (typecheck node)
  233. | _ -> raise (InvalidInput "typecheck")