typecheck.ml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. open Printf
  2. open Ast
  3. open Util
  4. open Stringify
  5. (*
  6. * Do a number of checks:
  7. * x A void function must not return a value.
  8. * x 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. * x The type on the right-hand side of an assignment must match the type on
  12. * the left-hand side.
  13. * x The number of arguments used for a function call must match the number of
  14. * parameters for that function.
  15. * x The types of the function arguments must match the types of parameters.
  16. * x The operands of a unary or binary operation must have valid types.
  17. * x The predicate expression of an if, while, or do-while statement must be
  18. * a boolean.
  19. * x Only values having a basic type can be type cast.
  20. *)
  21. let spec = function
  22. | ArrayDec (ctype, dims) -> ArraySpec (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 "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 =
  47. sprintf "%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 rec typecheck node = match node with
  54. | BoolConst (value, _) -> Type (node, Bool)
  55. | IntConst (value, _) -> Type (node, Int)
  56. | FloatConst (value, _) -> Type (node, Float)
  57. | VarUse (_, ctype, _) -> Type (node, ctype)
  58. | FunUse (FunCall (_, args, _), FunDef (_, ftype, name, params, _, _), _) ->
  59. (match (list_size args, list_size params) with
  60. | (nargs, nparams) when nargs != nparams ->
  61. let msg =
  62. sprintf "function \"%s\" expects %d arguments, got %d"
  63. name nparams nargs
  64. in
  65. raise (NodeError (node, msg))
  66. | _ ->
  67. let check_arg_type arg param =
  68. check_type (ctypeof param) (typecheck arg);
  69. in
  70. List.iter2 check_arg_type args params;
  71. Type (node, ftype)
  72. )
  73. | Arg (Type (_, vtype)) -> Type (node, vtype)
  74. | Arg value -> typecheck (Arg (typecheck value))
  75. | Monop (op, (Type (_, vtype) as value), _) ->
  76. let desc = sprintf "unary operator \"%s\"" (op2str op) in
  77. check_type_op (op_types op) desc value;
  78. Type (node, op_result_type vtype op)
  79. | Monop (op, value, loc) ->
  80. typecheck (Monop (op, typecheck value, loc))
  81. | Binop (op, (Type (_, ltype) as left), right, loc) ->
  82. let desc = sprintf "binary operator \"%s\"" (op2str op) in
  83. check_type_op (op_types op) desc left;
  84. check_type ltype right;
  85. Type (node, op_result_type ltype op)
  86. | Binop (op, left, right, loc) ->
  87. typecheck (Binop (op, typecheck left, typecheck right, loc))
  88. | Cond (Type (cond, condtype), Type (texpr, ttype), fexpr, loc) ->
  89. check_type ttype fexpr;
  90. Type (node, ttype)
  91. | VarLet (Assign (_, (Type _ as value), _), dec_type, depth) ->
  92. check_type dec_type value;
  93. node
  94. | VarLet (assign, dec_type, depth) ->
  95. typecheck (VarLet (typecheck assign, dec_type, depth))
  96. | TypeCast (ctype, (Type _ as value), loc) ->
  97. check_type_op [Bool; Int; Float] "typecast" value;
  98. Type (node, ctype)
  99. | TypeCast (ctype, value, loc) ->
  100. typecheck (TypeCast (ctype, typecheck value, loc))
  101. | Return (Type _, _) -> node
  102. | Return (value, loc) -> typecheck (Return (typecheck value, loc))
  103. | FunDef (export, ret_type, name, params, body, loc) ->
  104. let params = transform_all typecheck params in
  105. let body = typecheck body in
  106. let rec find_return = function
  107. | [] -> None
  108. | [Return (Type (_, rtype), _) as ret] -> Some (ret, rtype)
  109. | hd :: tl -> find_return tl
  110. in (
  111. match (ret_type, find_return (block_body body)) with
  112. | (Void, Some (ret, _)) ->
  113. raise (NodeError (ret, "void function should not have a return value"))
  114. | ((Bool | Int | Float), None) ->
  115. let msg =
  116. sprintf "expected return value of type %s for function \"%s\""
  117. (type2str ret_type) name
  118. in
  119. raise (NodeError (node, msg))
  120. | ((Bool | Int | Float), Some (ret, t)) when t != ret_type ->
  121. let msg =
  122. sprintf "function \"%s\" has return type %s, got %s"
  123. name (type2str ret_type) (type2str t)
  124. in
  125. raise (NodeError (ret, msg))
  126. | _ ->
  127. FunDef (export, ret_type, name, params, body, loc)
  128. )
  129. (* Conditions in if-statements and loop must be type bool *)
  130. | If (Type _ as cond, _, _)
  131. | IfElse (Type _ as cond, _, _, _)
  132. | While (Type _ as cond, _, _)
  133. | DoWhile (Type _ as cond, _, _) ->
  134. check_type Bool cond (*~msg:"condition should have type bool"*);
  135. node
  136. | If (cond, body, loc) ->
  137. typecheck (If (typecheck cond, typecheck body, loc))
  138. | IfElse (cond, tbody, fbody, loc) ->
  139. typecheck (IfElse (typecheck cond, typecheck tbody, typecheck fbody, loc))
  140. | While (cond, body, loc) ->
  141. typecheck (While (typecheck cond, typecheck body, loc))
  142. | DoWhile (cond, body, loc) ->
  143. typecheck (DoWhile (typecheck cond, typecheck body, loc))
  144. | _ -> transform_children typecheck node
  145. let rec phase input =
  146. prerr_endline "- Type checking";
  147. match input with
  148. | Ast (node, args) ->
  149. Ast (typecheck node, args)
  150. | _ -> raise (InvalidInput "typecheck")