typecheck.ml 7.8 KB

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