context_analysis.ml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, None, value, loc) ->
  75. let (decl, dec_depth) = check_in_scope (Varname name) node scope in
  76. let assign = Assign (name, None, collect value, loc) in
  77. VarLet (assign, ctypeof decl, depth - dec_depth)
  78. | Assign (name, Some dims, value, loc) ->
  79. let (decl, dec_depth) = check_in_scope (Varname name) node scope in
  80. let dims = Some (List.map collect dims) in
  81. let assign = Assign (name, dims, collect value, loc) in
  82. VarLet (assign, ctypeof decl, depth - dec_depth)
  83. | _ -> transform_children collect node
  84. in
  85. (*let print_scope () =
  86. let (vars, funs) = scope in
  87. let print_key key value = prerr_string (" " ^ key) in
  88. prerr_string "vars: ";
  89. Hashtbl.iter print_key vars;
  90. prerr_endline "";
  91. prerr_string "funs: ";
  92. Hashtbl.iter print_key funs;
  93. prerr_endline "";
  94. in*)
  95. let rec traverse scope depth node =
  96. match node with
  97. (* Increase nesting level when entering function *)
  98. | FunDef (export, ret_type, name, params, body, loc) ->
  99. let (vars, funs) = scope in
  100. let local_scope = (Hashtbl.copy vars, Hashtbl.copy funs) in
  101. let params = List.map (traverse local_scope (depth + 1)) params in
  102. let body = analyse local_scope (depth + 1) args body in
  103. FunDef (export, ret_type, name, params, body, loc)
  104. | Param (ArrayDec (_, dims), name, _) as node ->
  105. let rec add_dims = function
  106. | [] -> ()
  107. | Dim (name, _) as dim :: tail ->
  108. add_to_scope (Varname name) (DimDec dim) depth scope;
  109. add_dims tail
  110. | _ -> raise InvalidNode
  111. in
  112. add_dims dims;
  113. add_to_scope (Varname name) node depth scope;
  114. node
  115. | Param (_, name, _) ->
  116. add_to_scope (Varname name) node depth scope;
  117. node
  118. (* Do not traverse into external function declarations, since their
  119. * parameters must not be added to the namespace *)
  120. | FunDec _ -> node
  121. | _ -> transform_children (traverse scope depth) node
  122. in
  123. (*
  124. * First collect all definitions at the current depth. Then, traverse into
  125. * functions with a copy of the current scope. This is needed because
  126. * functions can access all identifiers in their surrounding scope.
  127. * E.g., the following is allowed:
  128. *
  129. * void foo() { glob = 1; }
  130. * int glob;
  131. *)
  132. (*prerr_endline "";
  133. prerr_endline ("node:----\n" ^ Stringify.node2str node);
  134. prerr_endline "----";*)
  135. let node = collect node in
  136. (*prerr_endline "collected";
  137. print_scope ();
  138. prerr_endline "\ntraversing";*)
  139. let node = traverse scope depth node in
  140. (*prerr_endline "traversed";
  141. print_scope ();
  142. prerr_endline "";*)
  143. node
  144. let analyse_context args program =
  145. let scope = (Hashtbl.create 20, Hashtbl.create 20) in
  146. analyse scope 0 args program
  147. let rec phase input =
  148. prerr_endline "- Context analysis";
  149. match input with
  150. | Ast (node, args) ->
  151. Ast (analyse_context args node, args)
  152. | _ -> raise (InvalidInput "context analysis")