context_analysis.ml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. open Printf
  2. open Ast
  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 (decl, dec_depth, _) ->
  16. (decl, 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 rec analyse scope depth args node =
  24. (* add_to_scope uses args, so it needs to be defined here *)
  25. let add_to_scope name decl depth scope =
  26. let (vars, funs) = scope in
  27. let (name, tbl, name_type) = match name with
  28. | Varname name -> (name, vars, "variable")
  29. | Funcname name -> (name, funs, "function")
  30. in
  31. match mapfind name tbl with
  32. (* Identifiers of lower depth may be overwritten, but idenetifiers at
  33. * the same depth must be unique for consistency *)
  34. | Some (orig, orig_depth, _) when orig_depth >= depth ->
  35. let msg = sprintf "Error: cannot redeclare %s \"%s\"" name_type name in
  36. prerr_loc_msg (locof decl) msg args.verbose;
  37. prerr_loc_msg (locof orig) "Previously declared here:" args.verbose;
  38. raise EmptyError
  39. | Some _ ->
  40. Hashtbl.replace tbl name (decl, depth, name_type)
  41. | None ->
  42. Hashtbl.add tbl name (decl, depth, name_type)
  43. in
  44. let rec collect node = match node with
  45. (* Add node reference for this varname to vars map *)
  46. | VarDec (ctype, name, init, loc) ->
  47. let node = match init with
  48. | Some value -> VarDec (ctype, name, Some (collect value), loc)
  49. | None -> node
  50. in
  51. add_to_scope (Varname name) node depth scope;
  52. node
  53. (* For global vars, only add the name *)
  54. | GlobalDec (_, name, _)
  55. | GlobalDef (_, _, name, _, _) ->
  56. add_to_scope (Varname name) node depth scope;
  57. node
  58. (* Functions are traversed later on, for now only add the name *)
  59. | FunDec (_, name, _, _)
  60. | FunDef (_, _, name, _, _, _) ->
  61. add_to_scope (Funcname name) node depth scope;
  62. node
  63. (* For a variable or function call, look for its declaration in the
  64. * current scope and save a its type/depth information *)
  65. | Var (name, _) ->
  66. let (decl, dec_depth) = check_in_scope (Varname name) node scope in
  67. VarUse (node, ctypeof decl, depth - dec_depth)
  68. | FunCall (name, args, loc) ->
  69. let (decl, dec_depth) = check_in_scope (Funcname name) node scope in
  70. let node = FunCall (name, transform_all collect args, loc) in
  71. FunUse (node, decl, depth - dec_depth)
  72. (* Assign statements are wrapped in VarLet nodes, which stores the type
  73. * and depth of the assigned variable are *)
  74. | Assign (name, value, loc) ->
  75. let (decl, dec_depth) = check_in_scope (Varname name) node scope in
  76. let assign = Assign (name, collect value, loc) in
  77. VarLet (assign, ctypeof decl, depth - dec_depth)
  78. | _ -> transform_children collect node
  79. in
  80. (*let print_scope () =
  81. let (vars, funs) = scope in
  82. let print_key key value = prerr_string (" " ^ key) in
  83. prerr_string "vars: ";
  84. Hashtbl.iter print_key vars;
  85. prerr_endline "";
  86. prerr_string "funs: ";
  87. Hashtbl.iter print_key funs;
  88. prerr_endline "";
  89. in*)
  90. let rec traverse scope depth node =
  91. match node with
  92. (* Increase nesting level when entering function *)
  93. | FunDef (export, ret_type, name, params, body, loc) ->
  94. let (vars, funs) = scope in
  95. let local_scope = (Hashtbl.copy vars, Hashtbl.copy funs) in
  96. let params = List.map (traverse local_scope (depth + 1)) params in
  97. let body = analyse local_scope (depth + 1) args body in
  98. FunDef (export, ret_type, name, params, body, loc)
  99. | Param (ArrayDec (_, dims), name, _) as node ->
  100. let rec add_dims = function
  101. | [] -> ()
  102. | Dim (name, _) as dim :: tail ->
  103. add_to_scope (Varname name) (DimDec dim) depth scope;
  104. add_dims tail
  105. | _ -> raise InvalidNode
  106. in
  107. add_dims dims;
  108. add_to_scope (Varname name) node depth scope;
  109. node
  110. | Param (_, name, _) ->
  111. add_to_scope (Varname name) node depth scope;
  112. node
  113. (* Do not traverse into external function declarations, since their
  114. * parameters must not be added to the namespace *)
  115. | FunDec _ -> node
  116. | _ -> transform_children (traverse scope depth) node
  117. in
  118. (*
  119. * First collect all definitions at the current depth. Then, traverse into
  120. * functions with a copy of the current scope. This is needed because
  121. * functions can access all identifiers in their surrounding scope.
  122. * E.g., the following is allowed:
  123. *
  124. * void foo() { glob = 1; }
  125. * int glob;
  126. *)
  127. (*prerr_endline "";
  128. prerr_endline ("node:----\n" ^ Stringify.node2str node);
  129. prerr_endline "----";*)
  130. let node = collect node in
  131. (*prerr_endline "collected";
  132. print_scope ();
  133. prerr_endline "\ntraversing";*)
  134. let node = traverse scope depth node in
  135. (*prerr_endline "traversed";
  136. print_scope ();
  137. prerr_endline "";*)
  138. node
  139. let analyse_context args program =
  140. let scope = (Hashtbl.create 20, Hashtbl.create 20) in
  141. analyse scope 0 args program
  142. let rec phase input =
  143. prerr_endline "- Context analysis";
  144. match input with
  145. | Ast (node, args) ->
  146. Ast (analyse_context args node, args)
  147. | _ -> raise (InvalidInput "context analysis")