context_analysis.ml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 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 dec 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 dec) msg args.verbose;
  37. prerr_loc_msg (locof orig) "Previously declared here:" args.verbose;
  38. raise EmptyError
  39. | Some _ ->
  40. Hashtbl.replace tbl name (dec, depth, name_type)
  41. | None ->
  42. Hashtbl.add tbl name (dec, 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, ann) ->
  47. let node = match init with
  48. | Some value -> VarDec (ctype, name, Some (collect value), ann)
  49. | None -> node
  50. in
  51. add_to_scope (Varname name) node depth scope;
  52. VarDec (ctype, name, init, Depth depth :: ann)
  53. (* For global vars, only the name and array dimensions *)
  54. | GlobalDec (Array (ctype, dims), name, ann) ->
  55. add_to_scope (Varname name) node depth scope;
  56. GlobalDec (Array (ctype, List.map collect dims), name, ann)
  57. | Dim (name, ann) ->
  58. add_to_scope (Varname name) node depth scope;
  59. Dim (name, Depth depth :: ann)
  60. | GlobalDec (ctype, name, ann) ->
  61. add_to_scope (Varname name) node depth scope;
  62. GlobalDec (ctype, name, Depth depth :: ann)
  63. | GlobalDef (export, ctype, name, init, ann) ->
  64. add_to_scope (Varname name) node depth scope;
  65. GlobalDef (export, ctype, name, init, Depth depth :: ann)
  66. (* Functions are traversed later on, for now only add the name *)
  67. | FunDec (ret_type, name, params, ann) ->
  68. add_to_scope (Funcname name) node depth scope;
  69. FunDec (ret_type, name, params, Depth depth :: ann)
  70. | FunDef (export, ret_type, name, params, body, ann) ->
  71. add_to_scope (Funcname name) node depth scope;
  72. FunDef (export, ret_type, name, params, body, Depth depth :: ann)
  73. (* For a variable or function call, look for its declaration in the
  74. * current scope and save a its type/depth information *)
  75. | Var (name, dims, ann) ->
  76. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  77. VarUse (dec, optmap collect dims, Depth depth :: ann)
  78. | FunCall (name, args, ann) ->
  79. let (dec, dec_depth) = check_in_scope (Funcname name) node scope in
  80. FunUse (dec, List.map collect args, Depth depth :: ann)
  81. (* Assign statements are replaced with VarLet nodes, which stores the type
  82. * and depth of the assigned variable are *)
  83. | Assign (name, dims, value, ann) ->
  84. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  85. VarLet (dec, optmap collect dims, collect value, Depth depth :: ann)
  86. | _ -> transform_children collect node
  87. in
  88. let rec traverse scope depth node =
  89. match node with
  90. (* Increase nesting level when entering function *)
  91. | FunDef (export, ret_type, name, params, body, ann) ->
  92. let (vars, funs) = scope in
  93. let local_scope = (Hashtbl.copy vars, Hashtbl.copy funs) in
  94. let params = List.map (traverse local_scope (depth + 1)) params in
  95. let body = analyse local_scope (depth + 1) args body in
  96. FunDef (export, ret_type, name, params, body, ann)
  97. | Param (Array (ctype, dims), name, ann) as node ->
  98. let _ = List.map (traverse scope depth) dims in
  99. add_to_scope (Varname name) node depth scope;
  100. node
  101. | Dim (name, _) as dim ->
  102. add_to_scope (Varname name) dim depth scope;
  103. node
  104. | Param (_, name, _) ->
  105. add_to_scope (Varname name) node depth scope;
  106. node
  107. (* Do not traverse into external function declarations, since their
  108. * parameters must not be added to the namespace *)
  109. | FunDec _ -> node
  110. | _ -> transform_children (traverse scope depth) node
  111. in
  112. (*
  113. * First collect all definitions at the current depth. Then, traverse into
  114. * functions with a copy of the current scope. This is needed because
  115. * functions can access all identifiers in their surrounding scope.
  116. * E.g., the following is allowed:
  117. *
  118. * void foo() { glob = 1; }
  119. * int glob;
  120. *)
  121. let node = collect node in
  122. let node = traverse scope depth node in
  123. node
  124. let analyse_context args program =
  125. let scope = (Hashtbl.create 20, Hashtbl.create 20) in
  126. analyse scope 0 args program
  127. let rec phase input =
  128. log_line 2 "- Context analysis";
  129. match input with
  130. | Types node -> Types (analyse_context args node)
  131. | _ -> raise (InvalidInput "context analysis")