typecheck.ml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "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 =
  52. sprintf "%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 =
  62. sprintf "dimension mismatch: expected %d indices, got %d"
  63. expected got
  64. in
  65. raise (NodeError (errnode, msg))
  66. | _ -> ()
  67. let rec typecheck node = match node with
  68. | FunUse (FunCall (_, args, _), FunDef (_, ftype, name, params, _, _), _) ->
  69. (match (list_size args, list_size params) with
  70. | (nargs, nparams) when nargs != nparams ->
  71. let msg =
  72. sprintf "function \"%s\" expects %d arguments, got %d"
  73. name nparams nargs
  74. in
  75. raise (NodeError (node, msg))
  76. | _ ->
  77. let check_arg_type arg param =
  78. check_type (ctypeof param) (typecheck arg);
  79. in
  80. List.iter2 check_arg_type args params;
  81. Type (node, ftype)
  82. )
  83. | Arg (Type (_, vtype)) -> Type (node, vtype)
  84. | Arg value -> typecheck (Arg (typecheck value))
  85. | Monop (op, (Type (_, vtype) as value), _) ->
  86. let desc = sprintf "unary operator \"%s\"" (op2str op) in
  87. check_type_op (op_types op) desc value;
  88. Type (node, op_result_type vtype op)
  89. | Monop (op, value, loc) ->
  90. typecheck (Monop (op, typecheck value, loc))
  91. | Binop (op, (Type (_, ltype) as left), right, loc) ->
  92. let desc = sprintf "binary operator \"%s\"" (op2str op) in
  93. check_type_op (op_types op) desc left;
  94. check_type ltype right;
  95. Type (node, op_result_type ltype op)
  96. | Binop (op, left, right, loc) ->
  97. typecheck (Binop (op, typecheck left, typecheck right, loc))
  98. | Cond (Type (cond, condtype), Type (texpr, ttype), fexpr, loc) ->
  99. check_type ttype fexpr;
  100. Type (node, ttype)
  101. | VarLet (Assign (_, None, (Type _ as value), _), dec_type, depth) ->
  102. check_type dec_type value;
  103. node
  104. | VarLet (Assign (_, Some dims, (Type _ as value), _) as assign, dec_type, depth) ->
  105. (* Number of assigned indices must match array definition *)
  106. check_dims_match dims dec_type assign;
  107. (* Array indices must be ints *)
  108. List.iter (check_type Int) dims;
  109. (* Assigned value must match array base type *)
  110. check_type (base_type dec_type) value;
  111. node
  112. | VarLet (assign, dec_type, depth) ->
  113. typecheck (VarLet (typecheck assign, dec_type, depth))
  114. | TypeCast (ctype, (Type _ as value), loc) ->
  115. check_type_op [Bool; Int; Float] "typecast" value;
  116. Type (node, ctype)
  117. | TypeCast (ctype, value, loc) ->
  118. typecheck (TypeCast (ctype, typecheck value, loc))
  119. | VarUse (Deref (_, dims, _) as deref, dec_type, depth) ->
  120. let dims = List.map typecheck dims in
  121. List.iter (check_type Int) dims;
  122. check_dims_match dims dec_type deref;
  123. let ctype = base_type dec_type in
  124. typecheck (VarUse (Type (deref, ctype), ctype, depth))
  125. | Allocate (name, dims, dec, loc) ->
  126. let dims = List.map typecheck dims in
  127. List.iter (check_type Int) dims;
  128. Allocate (name, dims, dec, loc)
  129. | Return (Type _, _) -> node
  130. | Return (value, loc) -> typecheck (Return (typecheck value, loc))
  131. | FunDef (export, ret_type, name, params, body, loc) ->
  132. let params = transform_all typecheck params in
  133. let body = typecheck body in
  134. let rec find_return = function
  135. | [] -> None
  136. | [Return (Type (_, rtype), _) as ret] -> Some (ret, rtype)
  137. | hd :: tl -> find_return tl
  138. in (
  139. match (ret_type, find_return (block_body body)) with
  140. | (Void, Some (ret, _)) ->
  141. raise (NodeError (ret, "void function should not have a return value"))
  142. | ((Bool | Int | Float), None) ->
  143. let msg =
  144. sprintf "expected return value of type %s for function \"%s\""
  145. (type2str ret_type) name
  146. in
  147. raise (NodeError (node, msg))
  148. | ((Bool | Int | Float), Some (ret, t)) when t != ret_type ->
  149. let msg =
  150. sprintf "function \"%s\" has return type %s, got %s"
  151. name (type2str ret_type) (type2str t)
  152. in
  153. raise (NodeError (ret, msg))
  154. | _ ->
  155. FunDef (export, ret_type, name, params, body, loc)
  156. )
  157. (* Conditions in if-statements and loop must be type bool *)
  158. | If (Type _ as cond, _, _)
  159. | IfElse (Type _ as cond, _, _, _)
  160. | While (Type _ as cond, _, _)
  161. | DoWhile (Type _ as cond, _, _) ->
  162. check_type Bool cond (*~msg:"condition should have type bool"*);
  163. node
  164. | If (cond, body, loc) ->
  165. typecheck (If (typecheck cond, typecheck body, loc))
  166. | IfElse (cond, tbody, fbody, loc) ->
  167. typecheck (IfElse (typecheck cond, typecheck tbody, typecheck fbody, loc))
  168. | While (cond, body, loc) ->
  169. typecheck (While (typecheck cond, typecheck body, loc))
  170. | DoWhile (cond, body, loc) ->
  171. typecheck (DoWhile (typecheck cond, typecheck body, loc))
  172. | BoolConst (value, _) -> Type (node, Bool)
  173. | IntConst (value, _) -> Type (node, Int)
  174. | FloatConst (value, _) -> Type (node, Float)
  175. | VarUse (_, ctype, _) -> Type (node, ctype)
  176. | _ -> transform_children typecheck node
  177. let rec phase input =
  178. prerr_endline "- Type checking";
  179. match input with
  180. | Ast (node, args) ->
  181. Ast (typecheck node, args)
  182. | _ -> raise (InvalidInput "typecheck")