context.ml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. open Printf
  2. open Types
  3. open Util
  4. type nametype = Varname of string | Funcname of string
  5. let type2str = function Funcname _ -> "function" | Varname _ -> "variable"
  6. let mapfind name tbl =
  7. if Hashtbl.mem tbl name then Some (Hashtbl.find tbl name) else None
  8. let check_in_scope name errnode scope =
  9. let (vars, funs) = scope in
  10. let (name, tbl, other_map, desired_type) = match name with
  11. | Varname name -> (name, vars, funs, "variable")
  12. | Funcname name -> (name, funs, vars, "function")
  13. in
  14. match mapfind name tbl with
  15. | Some (dec, dec_depth, _) ->
  16. (dec, dec_depth)
  17. | None ->
  18. let msg = match mapfind name other_map with
  19. | Some _ -> sprintf "\"%s\" is not a %s" name desired_type
  20. | None -> sprintf "undefined %s \"%s\"" desired_type name
  21. in
  22. raise (NodeError (errnode, msg))
  23. let add_to_scope name dec depth (vars, funs) =
  24. let (name, tbl, name_type) = match name with
  25. | Varname name -> (name, vars, "variable")
  26. | Funcname name -> (name, funs, "function")
  27. in
  28. match mapfind name tbl with
  29. (* Identifiers of lower depth may be overwritten, but idenetifiers at
  30. * the same depth must be unique for consistency *)
  31. | Some (orig, orig_depth, _) when orig_depth >= depth ->
  32. let msg = sprintf "Error: cannot redeclare %s \"%s\"" name_type name in
  33. prerr_loc_msg (locof dec) msg;
  34. prerr_loc_msg (locof orig) "Previously declared here:";
  35. raise EmptyError
  36. | Some _ ->
  37. Hashtbl.replace tbl name (dec, depth, name_type)
  38. | None ->
  39. Hashtbl.add tbl name (dec, depth, name_type)
  40. let rec analyse scope depth node =
  41. let rec collect node = match node with
  42. (* For variables, add the name (array dimensions are added
  43. * implicitly, since they have separate VarDec nodes which were added
  44. * during the desugaring phase *)
  45. | VarDec (_, name, _, _)
  46. | GlobalDec (_, name, _)
  47. | GlobalDef (_, _, name, _, _) ->
  48. let node = annotate (Depth depth) node in
  49. add_to_scope (Varname name) node depth scope;
  50. node
  51. (* Functions are traversed later on, for now only add the name *)
  52. | FunDec (_, name, _, _)
  53. | FunDef (_, _, name, _, _, _) ->
  54. let node = annotate (Depth depth) node in
  55. add_to_scope (Funcname name) node depth scope;
  56. node
  57. (* For a variable or function call, look for its declaration in the
  58. * current scope and save a its type/depth information *)
  59. | Var (name, dims, ann) ->
  60. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  61. VarUse (dec, optmap collect dims, Depth depth :: ann)
  62. | FunCall (name, args, ann) ->
  63. let (dec, dec_depth) = check_in_scope (Funcname name) node scope in
  64. FunUse (dec, List.map collect args, Depth depth :: ann)
  65. (* Assign statements are replaced with VarLet nodes, which stores the
  66. * declaration of the assigned variable *)
  67. | Assign (name, dims, value, ann) ->
  68. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  69. VarLet (dec, optmap collect dims, collect value, Depth depth :: ann)
  70. | Allocate (dec, dims, ann) ->
  71. let (dec, dec_depth) = check_in_scope (Varname (nameof dec)) node scope in
  72. Allocate (dec, List.map collect dims, Depth depth :: ann)
  73. | _ -> transform_children collect node
  74. in
  75. let rec traverse scope depth node =
  76. match node with
  77. (* Increase nesting level when entering function *)
  78. | FunDef (export, ret_type, name, params, body, ann) ->
  79. let (vars, funs) = scope in
  80. let local_scope = (Hashtbl.copy vars, Hashtbl.copy funs) in
  81. let params = List.map (traverse local_scope (depth + 1)) params in
  82. let body = analyse local_scope (depth + 1) body in
  83. FunDef (export, ret_type, name, params, body, ann)
  84. | Param (ArrayDims (ctype, dims), name, ann) ->
  85. let rec add_dims = function
  86. | [] -> []
  87. | Dim (name, ann) :: tl ->
  88. let dim = Dim (name, Depth depth :: ann) in
  89. add_to_scope (Varname name) dim depth scope;
  90. dim :: (add_dims tl)
  91. | _ -> raise InvalidNode
  92. in
  93. let node = Param (ArrayDims (ctype, add_dims dims), name, ann) in
  94. add_to_scope (Varname name) node depth scope;
  95. node
  96. | VarDec _ -> node
  97. | Param (_, name, _) ->
  98. let node = annotate (Depth depth) node in
  99. add_to_scope (Varname name) node depth scope;
  100. node
  101. (* Do not traverse into external function declarations, since their
  102. * parameters must not be added to the namespace *)
  103. | FunDec _ -> node
  104. | _ -> transform_children (traverse scope depth) node
  105. in
  106. (*
  107. * First collect all definitions at the current depth. Then, traverse into
  108. * functions with a copy of the current scope. This is needed because
  109. * functions can access all identifiers in their surrounding scope.
  110. * E.g., the following is allowed:
  111. *
  112. * void foo() { glob = 1; }
  113. * int glob;
  114. *)
  115. let node = collect node in
  116. let node = traverse scope depth node in
  117. node
  118. let analyse_context program =
  119. let scope = (Hashtbl.create 20, Hashtbl.create 20) in
  120. analyse scope 0 program
  121. let phase = function
  122. | Ast node -> Ast (analyse_context node)
  123. | _ -> raise (InvalidInput "context analysis")