context_analysis.ml 6.2 KB

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