context_analysis.ml 5.6 KB

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