context.ml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. (* For generated variables, don't gove an error, since the error variable
  33. * is a derived array dimension of a redefined array, which will yield an
  34. * error later on *)
  35. if is_generated_id name then
  36. Hashtbl.replace tbl name (dec, depth, name_type)
  37. else
  38. let msg = sprintf "Error: cannot redeclare %s \"%s\"" name_type name in
  39. prerr_loc_msg (locof dec) msg;
  40. prerr_loc_msg (locof orig) "Previously declared here:";
  41. raise EmptyError
  42. | Some _ ->
  43. Hashtbl.replace tbl name (dec, depth, name_type)
  44. | None ->
  45. Hashtbl.add tbl name (dec, depth, name_type)
  46. let rec analyse scope depth node =
  47. let rec collect node = match node with
  48. (* For extern array declarations, add the dimension names as well *)
  49. | GlobalDec (ArrayDims (ctype, dims), name, ann) ->
  50. let rec add_dims = function
  51. | [] -> []
  52. | Dim (name, ann) :: tl ->
  53. let dim = Dim (name, Depth depth :: ann) in
  54. add_to_scope (Varname name) dim depth scope;
  55. dim :: (add_dims tl)
  56. | _ -> raise InvalidNode
  57. in
  58. let t = ArrayDims (ctype, add_dims dims) in
  59. let node = GlobalDec (t, name, Depth depth :: ann) in
  60. add_to_scope (Varname name) node depth scope;
  61. node
  62. (* For variables, add the name (array dimensions are added
  63. * implicitly, since they have separate VarDec nodes which were added
  64. * during the desugaring phase *)
  65. | VarDec (_, name, _, _)
  66. | GlobalDec (_, name, _)
  67. | GlobalDef (_, _, name, _, _) ->
  68. let node = annotate (Depth depth) node in
  69. add_to_scope (Varname name) node depth scope;
  70. node
  71. (* Functions are traversed later on, for now only add the name *)
  72. | FunDec (_, name, _, _)
  73. | FunDef (_, _, name, _, _, _) ->
  74. let node = annotate (Depth depth) node in
  75. add_to_scope (Funcname name) node depth scope;
  76. node
  77. (* For a variable or function call, look for its declaration in the
  78. * current scope and save a its type/depth information *)
  79. | Var (name, dims, ann) ->
  80. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  81. VarUse (dec, optmap collect dims, Depth depth :: ann)
  82. | FunCall (name, args, ann) ->
  83. let (dec, dec_depth) = check_in_scope (Funcname name) node scope in
  84. FunUse (dec, List.map collect args, Depth depth :: ann)
  85. (* Assign statements are replaced with VarLet nodes, which stores the
  86. * declaration of the assigned variable *)
  87. | Assign (name, dims, value, ann) ->
  88. let (dec, dec_depth) = check_in_scope (Varname name) node scope in
  89. VarLet (dec, optmap collect dims, collect value, Depth depth :: ann)
  90. | Allocate (dec, dims, ann) ->
  91. let (dec, dec_depth) = check_in_scope (Varname (nameof dec)) node scope in
  92. Allocate (dec, List.map collect dims, Depth depth :: ann)
  93. | _ -> traverse_unit collect node
  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, ann) ->
  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) body in
  103. FunDef (export, ret_type, name, params, body, ann)
  104. | Param (ArrayDims (ctype, dims), name, ann) ->
  105. let rec add_dims = function
  106. | [] -> []
  107. | Dim (name, ann) :: tl ->
  108. let dim = Dim (name, Depth depth :: ann) in
  109. add_to_scope (Varname name) dim depth scope;
  110. dim :: (add_dims tl)
  111. | _ -> raise InvalidNode
  112. in
  113. let node = Param (ArrayDims (ctype, add_dims dims), name, ann) in
  114. add_to_scope (Varname name) node depth scope;
  115. node
  116. | VarDec _ -> node
  117. | Param (_, name, _) ->
  118. let node = annotate (Depth depth) node in
  119. add_to_scope (Varname name) node depth scope;
  120. node
  121. (* Do not traverse into external function declarations, since their
  122. * parameters must not be added to the namespace *)
  123. | FunDec _ -> node
  124. | _ -> traverse_unit (traverse scope depth) node
  125. in
  126. (*
  127. * First collect all definitions at the current depth. Then, traverse into
  128. * functions with a copy of the current scope. This is needed because
  129. * functions can access all identifiers in their surrounding scope.
  130. * E.g., the following is allowed:
  131. *
  132. * void foo() { glob = 1; }
  133. * int glob;
  134. *)
  135. let node = collect node in
  136. let node = traverse scope depth node in
  137. node
  138. let analyse_context program =
  139. let scope = (Hashtbl.create 20, Hashtbl.create 20) in
  140. analyse scope 0 program
  141. let phase = function
  142. | Ast node -> Ast (analyse_context node)
  143. | _ -> raise (InvalidInput "context analysis")