context.ml 5.8 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 err =
  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. err := !err @ [NodeMsg (errnode, msg)];
  23. (DummyNode, -1)
  24. let add_to_scope name dec depth (vars, funs) =
  25. let (name, tbl, name_type) = match name with
  26. | Varname name -> (name, vars, "variable")
  27. | Funcname name -> (name, funs, "function")
  28. in
  29. match mapfind name tbl with
  30. (* Identifiers of lower depth may be overwritten, but idenetifiers at
  31. * the same depth must be unique for consistency *)
  32. | Some (orig, orig_depth, _) when orig_depth >= depth ->
  33. (* For generated variables, don't gove an error, since the error variable
  34. * is a derived array dimension of a redefined array, which will yield an
  35. * error later on *)
  36. if is_generated_id name then
  37. Hashtbl.replace tbl name (dec, depth, name_type)
  38. else
  39. let msg = sprintf "Error: cannot redeclare %s \"%s\"" name_type name in
  40. prerr_loc_msg (locof dec) msg;
  41. prerr_loc_msg (locof orig) "Previously declared here:";
  42. raise (FatalError NoMsg)
  43. | Some _ ->
  44. Hashtbl.replace tbl name (dec, depth, name_type)
  45. | None ->
  46. Hashtbl.add tbl name (dec, depth, name_type)
  47. let rec analyse scope depth node err =
  48. let rec collect node = match node with
  49. (* For extern array declarations, add the dimension names as well *)
  50. | GlobalDec (ArrayDims (ctype, dims), name, ann) ->
  51. let t = ArrayDims (ctype, List.map (annotate (Depth depth)) dims) in
  52. let node = GlobalDec (t, name, Depth depth :: ann) in
  53. add_to_scope (Varname name) node depth scope;
  54. node
  55. (* For variables, add the name (array dimensions are added
  56. * implicitly, since they have separate VarDec nodes which were added
  57. * during the desugaring phase *)
  58. | VarDec (_, name, _, _)
  59. | GlobalDec (_, name, _)
  60. | GlobalDef (_, _, name, _, _) ->
  61. let node = annotate (Depth depth) node in
  62. add_to_scope (Varname name) node depth scope;
  63. node
  64. (* Functions are traversed later on, for now only add the name *)
  65. | FunDec (_, name, _, _)
  66. | FunDef (_, _, name, _, _, _) ->
  67. let node = annotate (Depth depth) node in
  68. add_to_scope (Funcname name) node depth scope;
  69. node
  70. (* For a variable or function call, look for its declaration in the
  71. * current scope and save a its type/depth information *)
  72. | Var (name, dims, ann) ->
  73. let (dec, dec_depth) = check_in_scope (Varname name) node scope err in
  74. VarUse (dec, optmap collect dims, Depth depth :: ann)
  75. | FunCall (name, args, ann) ->
  76. let (dec, dec_depth) = check_in_scope (Funcname name) node scope err in
  77. FunUse (dec, List.map collect args, Depth depth :: ann)
  78. (* Assign statements are replaced with VarLet nodes, which stores the
  79. * declaration of the assigned variable *)
  80. | Assign (name, dims, value, ann) ->
  81. let (dec, dec_depth) = check_in_scope (Varname name) node scope err in
  82. VarLet (dec, optmap collect dims, collect value, Depth depth :: ann)
  83. | Allocate (dec, dims, ann) ->
  84. let (dec, dec_depth) = check_in_scope (Varname (nameof dec)) node scope err in
  85. Allocate (dec, List.map collect dims, Depth depth :: ann)
  86. | _ -> traverse_unit 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) body err in
  96. FunDef (export, ret_type, name, params, body, ann)
  97. | Param (ArrayDims (ctype, dims), name, ann) ->
  98. let rec add_dims = function
  99. | [] -> []
  100. | Dim (name, ann) :: tl ->
  101. let dim = Dim (name, Depth depth :: ann) in
  102. add_to_scope (Varname name) dim depth scope;
  103. dim :: (add_dims tl)
  104. | _ -> raise InvalidNode
  105. in
  106. let node = Param (ArrayDims (ctype, add_dims dims), name, ann) in
  107. add_to_scope (Varname name) node depth scope;
  108. node
  109. | VarDec _ -> node
  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. | _ -> traverse_unit (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. let err = ref [] in
  134. let node = analyse scope 0 program err in
  135. quit_on_error node !err
  136. let phase = function
  137. | Ast node -> Ast (analyse_context node)
  138. | _ -> raise InvalidInput