typecheck.ml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. open Printf
  2. open Ast
  3. open Util
  4. open Stringify
  5. (*
  6. * Do a number of checks:
  7. * - A void function must not return a value.
  8. * - A non-void function must return a value of the correct type.
  9. * - Array indices must be of type integer.
  10. * - The number of array indices must match the number of array dimensions.
  11. * - The type on the right-hand side of an assignment must match the type on
  12. * the left-hand side.
  13. * - The number of arguments used for a function call must match the number of
  14. * parameters for that function.
  15. * - The types of the function arguments must match the types of parameters.
  16. * - The operands of a unary or binary operation must have valid types.
  17. * - The predicate expression of an if, while, or do-while statement must be
  18. * a boolean.
  19. * - Only values having a basic type can be type cast.
  20. *)
  21. let spec = function
  22. | ArrayDec (ctype, dims)
  23. | ArrayDef (ctype, dims) -> ArraySpec (ctype, list_size dims)
  24. | ctype -> ctype
  25. let array_width = function
  26. | ArrayDec (_, dims)
  27. | ArrayDef (_, dims) -> list_size dims
  28. | _ -> raise InvalidNode
  29. let check_type ?(msg="") expected = function
  30. | Type (node, got) when (spec got) <> (spec expected) ->
  31. let msg = match msg with
  32. | "" -> sprintf "type mismatch: expected type %s, got %s"
  33. (type2str expected) (type2str got)
  34. (*(type2str (spec expected)) (type2str (spec got))*)
  35. | _ -> msg
  36. in
  37. raise (NodeError (node, msg))
  38. | Type _ -> ()
  39. | _ -> raise InvalidNode
  40. let op_types = function
  41. | Not | And | Or -> [Bool]
  42. | Mod -> [Int]
  43. | Neg | Sub | Div | Lt | Le | Gt | Ge -> [Int; Float]
  44. | Add | Mul | Eq | Ne -> [Bool; Int; Float]
  45. let op_result_type operand_type = function
  46. | Not | And | Or | Eq | Ne | Lt | Le | Gt | Ge -> Bool
  47. | Neg | Add | Sub | Mul | Div | Mod -> operand_type
  48. (* Check if the given operator can be applied to the given type *)
  49. let check_type_op allowed_types desc = function
  50. | Type (node, ctype) when not (List.mem ctype allowed_types) ->
  51. let msg = sprintf
  52. "%s cannot be applied to type %s, only to %s"
  53. desc (type2str ctype) (types2str allowed_types)
  54. in
  55. raise (NodeError (node, msg))
  56. | Type _ -> ()
  57. | _ -> raise InvalidNode
  58. let check_dims_match dims dec_type errnode =
  59. match (list_size dims, array_width dec_type) with
  60. | (got, expected) when got != expected ->
  61. let msg = sprintf
  62. "dimension mismatch: expected %d indices, got %d" expected got
  63. in
  64. raise (NodeError (errnode, msg))
  65. | _ -> ()
  66. let rec typecheck node = match node with
  67. | FunUse (FunCall (_, args, _), FunDef (_, ftype, name, params, _, _), _) ->
  68. (match (list_size args, list_size params) with
  69. | (nargs, nparams) when nargs != nparams ->
  70. let msg = sprintf
  71. "function \"%s\" expects %d arguments, got %d"
  72. name nparams nargs
  73. in
  74. raise (NodeError (node, msg))
  75. | _ ->
  76. let check_arg_type arg param =
  77. check_type (ctypeof param) (typecheck arg);
  78. in
  79. List.iter2 check_arg_type args params;
  80. Type (node, ftype)
  81. )
  82. | Arg (Type (_, vtype)) -> Type (node, vtype)
  83. | Arg value -> typecheck (Arg (typecheck value))
  84. | Monop (op, (Type (_, vtype) as value), _) ->
  85. let desc = sprintf "unary operator \"%s\"" (op2str op) in
  86. check_type_op (op_types op) desc value;
  87. Type (node, op_result_type vtype op)
  88. | Monop (op, value, loc) ->
  89. typecheck (Monop (op, typecheck value, loc))
  90. | Binop (op, (Type (_, ltype) as left), right, loc) ->
  91. let desc = sprintf "binary operator \"%s\"" (op2str op) in
  92. check_type_op (op_types op) desc left;
  93. check_type ltype right;
  94. Type (node, op_result_type ltype op)
  95. | Binop (op, left, right, loc) ->
  96. typecheck (Binop (op, typecheck left, typecheck right, loc))
  97. | Cond (Type (cond, condtype), Type (texpr, ttype), fexpr, loc) ->
  98. check_type ttype fexpr;
  99. Type (node, ttype)
  100. | VarLet (Assign (_, None, (Type _ as value), _), dec_type, depth) ->
  101. check_type dec_type value;
  102. node
  103. | VarLet (Assign (_, Some dims, (Type _ as value), _) as assign, dec_type, depth) ->
  104. (* Number of assigned indices must match array definition *)
  105. check_dims_match dims dec_type assign;
  106. (* Array indices must be ints *)
  107. List.iter (check_type Int) dims;
  108. (* Assigned value must match array base type *)
  109. check_type (base_type dec_type) value;
  110. node
  111. | VarLet (assign, dec_type, depth) ->
  112. typecheck (VarLet (typecheck assign, dec_type, depth))
  113. | TypeCast (ctype, (Type _ as value), loc) ->
  114. check_type_op [Bool; Int; Float] "typecast" value;
  115. Type (node, ctype)
  116. | TypeCast (ctype, value, loc) ->
  117. typecheck (TypeCast (ctype, typecheck value, loc))
  118. | VarUse (Deref (_, dims, _) as deref, dec_type, depth) ->
  119. let dims = List.map typecheck dims in
  120. List.iter (check_type Int) dims;
  121. check_dims_match dims dec_type deref;
  122. let ctype = base_type dec_type in
  123. typecheck (VarUse (Type (deref, ctype), ctype, depth))
  124. | Allocate (name, dims, dec, loc) ->
  125. let dims = List.map typecheck dims in
  126. List.iter (check_type Int) dims;
  127. Allocate (name, dims, dec, loc)
  128. | Return (Type _, _) -> node
  129. | Return (value, loc) -> typecheck (Return (typecheck value, loc))
  130. | FunDef (export, ret_type, name, params, body, loc) ->
  131. let params = transform_all typecheck params in
  132. let body = typecheck body in
  133. let rec find_return = function
  134. | [] -> None
  135. | [Return (Type (_, rtype), _) as ret] -> Some (ret, rtype)
  136. | hd :: tl -> find_return tl
  137. in (
  138. match (ret_type, find_return (block_body body)) with
  139. | (Void, Some (ret, _)) ->
  140. raise (NodeError (ret, "void function should not have a return value"))
  141. | ((Bool | Int | Float), None) ->
  142. let msg = sprintf
  143. "expected return value of type %s for function \"%s\""
  144. (type2str ret_type) name
  145. in
  146. raise (NodeError (node, msg))
  147. | ((Bool | Int | Float), Some (ret, t)) when t != ret_type ->
  148. let msg = sprintf
  149. "function \"%s\" has return type %s, got %s"
  150. name (type2str ret_type) (type2str t)
  151. in
  152. raise (NodeError (ret, msg))
  153. | _ ->
  154. FunDef (export, ret_type, name, params, body, loc)
  155. )
  156. (* Conditions in if-statements and loop must be type bool *)
  157. | If (Type _ as cond, _, _)
  158. | IfElse (Type _ as cond, _, _, _)
  159. | While (Type _ as cond, _, _)
  160. | DoWhile (Type _ as cond, _, _) ->
  161. check_type Bool cond (*~msg:"condition should have type bool"*);
  162. node
  163. | If (cond, body, loc) ->
  164. typecheck (If (typecheck cond, typecheck body, loc))
  165. | IfElse (cond, tbody, fbody, loc) ->
  166. typecheck (IfElse (typecheck cond, typecheck tbody, typecheck fbody, loc))
  167. | While (cond, body, loc) ->
  168. typecheck (While (typecheck cond, typecheck body, loc))
  169. | DoWhile (cond, body, loc) ->
  170. typecheck (DoWhile (typecheck cond, typecheck body, loc))
  171. | BoolConst (value, _) -> Type (node, Bool)
  172. | IntConst (value, _) -> Type (node, Int)
  173. | FloatConst (value, _) -> Type (node, Float)
  174. | VarUse (_, ctype, _) -> Type (node, ctype)
  175. | _ -> transform_children typecheck node
  176. let rec phase input =
  177. prerr_endline "- Type checking";
  178. match input with
  179. | Ast (node, args) ->
  180. Ast (typecheck node, args)
  181. | _ -> raise (InvalidInput "typecheck")