context.ml 5.6 KB

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