|
|
@@ -19,6 +19,8 @@ open Types
|
|
|
open Util
|
|
|
open Stringify
|
|
|
|
|
|
+let node_error (node, msg) = FatalError (NodeMsg (node, msg))
|
|
|
+
|
|
|
(* Stringify a list of types for use in error messages.
|
|
|
* ctype list -> string *)
|
|
|
let rec types2str = function
|
|
|
@@ -48,7 +50,7 @@ let check_type ?(msg="") expected node =
|
|
|
(type2str_error expected) (type2str_error got)
|
|
|
| _ -> msg
|
|
|
in
|
|
|
- raise (NodeError (node, msg))
|
|
|
+ raise (node_error (node, msg))
|
|
|
end
|
|
|
|
|
|
let op_types = function
|
|
|
@@ -69,7 +71,7 @@ let check_type_op allowed_types desc node =
|
|
|
"%s cannot be applied to type %s, only to %s"
|
|
|
desc (type2str got) (types2str allowed_types)
|
|
|
in
|
|
|
- raise (NodeError (node, msg))
|
|
|
+ raise (node_error (node, msg))
|
|
|
); ()
|
|
|
|
|
|
let check_dims_match dims dec_type errnode =
|
|
|
@@ -78,7 +80,7 @@ let check_dims_match dims dec_type errnode =
|
|
|
let msg = sprintf
|
|
|
"dimension mismatch: expected %d indices, got %d" expected got
|
|
|
in
|
|
|
- raise (NodeError (errnode, msg))
|
|
|
+ raise (node_error (errnode, msg))
|
|
|
| _ -> ()
|
|
|
|
|
|
let rec typecheck node =
|
|
|
@@ -97,7 +99,7 @@ let rec typecheck node =
|
|
|
"function \"%s\" expects %d arguments, got %d"
|
|
|
name nparams nargs
|
|
|
in
|
|
|
- raise (NodeError (node, msg))
|
|
|
+ raise (node_error (node, msg))
|
|
|
| _ ->
|
|
|
let args = List.map typecheck args in
|
|
|
let check_arg_type arg param =
|
|
|
@@ -173,21 +175,21 @@ let rec typecheck node =
|
|
|
begin
|
|
|
match (ret_type, find_return (block_body body)) with
|
|
|
| (Void, Some (ret, _)) ->
|
|
|
- raise (NodeError (ret, "void function should not have a return value"))
|
|
|
+ raise (node_error (ret, "void function should not have a return value"))
|
|
|
|
|
|
| ((Bool | Int | Float), None) ->
|
|
|
let msg = sprintf
|
|
|
"expected return value of type %s for function \"%s\""
|
|
|
(type2str ret_type) name
|
|
|
in
|
|
|
- raise (NodeError (node, msg))
|
|
|
+ raise (node_error (node, msg))
|
|
|
|
|
|
| ((Bool | Int | Float), Some (ret, t)) when t != ret_type ->
|
|
|
let msg = sprintf
|
|
|
"function \"%s\" has return type %s, got %s"
|
|
|
name (type2str ret_type) (type2str t)
|
|
|
in
|
|
|
- raise (NodeError (ret, msg))
|
|
|
+ raise (node_error (ret, msg))
|
|
|
|
|
|
| _ -> FunDef (export, ret_type, name, params, body, ann)
|
|
|
end
|
|
|
@@ -212,7 +214,7 @@ let rec typecheck node =
|
|
|
let min = Nativeint.of_int32 Int32.min_int in
|
|
|
let max = Nativeint.of_int32 Int32.max_int in
|
|
|
if cmpval < min || cmpval > max then (
|
|
|
- raise (NodeError (node, "integer value out of range (signed 32-bit)"))
|
|
|
+ raise (node_error (node, "integer value out of range (signed 32-bit)"))
|
|
|
);
|
|
|
Const (IntVal value, Type Int :: ann)
|
|
|
| Const (FloatVal value, ann) ->
|
|
|
@@ -221,7 +223,7 @@ let rec typecheck node =
|
|
|
(* Extern arrays variables are transformed to imported functions, so the
|
|
|
* pointer cannot be passed *)
|
|
|
| VarUse (GlobalDec (ArrayDims _, _, _), None, _) ->
|
|
|
- raise (NodeError (node, "imported array pointers may only be \
|
|
|
+ raise (node_error (node, "imported array pointers may only be \
|
|
|
dereferenced, not used directly"))
|
|
|
|
|
|
(* Variables inherit the type of their declaration *)
|
|
|
@@ -238,7 +240,7 @@ let rec typecheck node =
|
|
|
(* Array pointers cannot be re-assigned, because array dimension reduction
|
|
|
* makes assumptions about dimensions of an array *)
|
|
|
| VarLet (dec, None, _, _) when is_array dec ->
|
|
|
- raise (NodeError (node, "cannot assign value to array pointer after \
|
|
|
+ raise (node_error (node, "cannot assign value to array pointer after \
|
|
|
initialisation"))
|
|
|
|
|
|
(* Assigned values must match variable declaration *)
|
|
|
@@ -262,11 +264,11 @@ let rec typecheck node =
|
|
|
(* ArrayConst initialisations are transformed during desugaring, so any
|
|
|
* occurrences that are left are illegal *)
|
|
|
| ArrayConst _ ->
|
|
|
- raise (NodeError (node, "array constants can only be used in array \
|
|
|
+ raise (node_error (node, "array constants can only be used in array \
|
|
|
initialisation"))
|
|
|
|
|
|
| _ -> traverse_unit typecheck node
|
|
|
|
|
|
let phase = function
|
|
|
| Ast node -> Ast (typecheck node)
|
|
|
- | _ -> raise (InvalidInput "typecheck")
|
|
|
+ | _ -> raise InvalidInput
|