util.mli 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. (** Utility functions used by multiple phases. *)
  2. open Types
  3. (** {2 Generating variables} *)
  4. (** Generate a new identifier from a given base, e.g. ["foo"] becomes
  5. ["_foo_1"]. Uses an [int] reference defined in the implementation file as a
  6. counter to assert uniqueness of the generated variable. *)
  7. val fresh_id : string -> string
  8. (** Generate a new constant (a variable that is known to be assigned only once
  9. in total) from a given id, e.g. ["foo"] becomes ["_foo_1_"]. Uses the same
  10. counter as {!fresh_id}. *)
  11. val fresh_const : string -> string
  12. (** Check if an identifier is generated by the compiler. *)
  13. val is_generated_id : string -> bool
  14. (** Check if an identifier is a constant generated by the compiler. *)
  15. val is_const_id : string -> bool
  16. (** Generate an identifier from a base and a number.
  17. E.g., [generate_id "foo" 1] returns ["_foo_1"]*)
  18. val generate_id : string -> int -> string
  19. (** Generate a constant identifier from a base and a number.
  20. E.g., [generate_id "foo" 1] returns ["_foo_1_"]*)
  21. val generate_const : string -> int -> string
  22. (** {2 AST traversal} *)
  23. (** Default transformation traversal for AST nodes of arbitrary constructor:
  24. [traverse unit_type fold_results visit_node node]. For each constructor that
  25. has one or more children of type {!Types.node}, the children are transformed
  26. with the given transformation function. A [(node, result)] tuple is
  27. returned, where the secondary result has the type of the given unit type.
  28. [visit_node] must also return such a tuple. [fold_results] is used to reduce
  29. results of multiple children to a single result.
  30. For [Program] nodes, {!flatten_blocks} is called on the resulting
  31. declaration list. This allows the transformation function to return multiple
  32. nodes as a replacement, in the form of a [Block] node with multiple
  33. children. *)
  34. val traverse : 'a -> ('a -> 'a -> 'a) -> (node -> (node * 'a)) -> node ->
  35. (node * 'a)
  36. (** Wrapper for {!traverse} that ignores the secondary traversal result. Returns
  37. {!Types.node} instead of a tuple. *)
  38. val traverse_unit : (node -> node) -> node -> node
  39. (** Wrapper for {!traverse} for list results. Defined as:
  40. [let traverse_list visit = traverse [] (@) visit] *)
  41. val traverse_list : (node -> (node * 'a list)) -> node -> (node * 'a list)
  42. (** Flatten [Block] nodes into containing node lists.
  43. E.g., [[A; Block [B; Block [C; D]]; E] -> [A; B; C; D; E]].
  44. Traverses into elements of the list recursively first, so this is
  45. essentially a traversal, as can be seen in the example. Therefore the best
  46. practice is to call this function once on the children of a [Program] node
  47. after any traversal that may generate [Block] nodes that need to be
  48. flattened. *)
  49. val flatten_blocks : node list -> node list
  50. (** Extract the list of child nodes from a [Block] node. *)
  51. val block_body : node -> node list
  52. (** {2 AST node annotations} *)
  53. (** Add a single annotation to a node. *)
  54. val annotate : annotation -> node -> node
  55. (** Extract annotations from a node of arbitrary constructor. *)
  56. val annof : node -> annotation list
  57. (** Get the value of the [Loc] annotation of a node, or {!noloc} if no location
  58. can be found. *)
  59. val locof : node -> location
  60. (** Empty node location: [("", 0, 0, 0, 0)]. Used then location of a node is
  61. unknown (if the annotation was removed at some point) or
  62. non-existant/irrelevant (for generated nodes on which no errors will occur --
  63. hopefully...). *)
  64. val noloc : location
  65. (** Get the value of the [Depth] annotation of a node. Raises
  66. {!Types.InvalidNode} if the annotation can not be found. *)
  67. val depthof : node -> int
  68. (** Get the value of the [Index] annotation of a node. Raises
  69. {!Types.InvalidNode} if the annotation can not be found. *)
  70. val indexof : node -> int
  71. (** Get the value of the [Type] annotation of a node. Some node types
  72. do not need to be annotated since they have inherent types. For example, a
  73. [VarDec] node has a type attribute, and a [Dim] node is always an [Int]
  74. (because array dimensions are integers). All nodes which have inherent types
  75. are [VarDec], [Param], [FunDec], [FunDef], [GlobalDec], [GlobalDef],
  76. [TypeCast], and [Dim]. Raises a {!Types.InvalidNode} if the annotation can
  77. not be found, and the type has no inherent type. *)
  78. val typeof : node -> ctype
  79. (** Get the value of the [LabelName] annotation of a node. Raises
  80. {!Types.InvalidNode} if the annotation can not be found. *)
  81. val labelof : node -> string
  82. (** Get the basic type of a declaration, removing array dimensions *)
  83. val basetypeof : node -> ctype
  84. (** Get the value of the name attribute from a variable or function declaration
  85. (similar to {!typeof} for nodes that have types attributes). Raises
  86. {!Types.InvalidNode} if the node is not one of [GlobalDec], [GlobalDef],
  87. [FunDec], [FunDef], [VarDec], [Param], or [Dim]. *)
  88. val nameof : node -> string
  89. (** Get the CiviC data type of a constant value. *)
  90. val const_type : const -> ctype
  91. (** Check if a constant value is eligible for creating optimized assembly
  92. instructions. E.g. [Intval (-1)] is eligible because the instruction
  93. [iloadc_m1] exists. Used to decide on which {!Types.instr} constructor to
  94. use during the assembly phases. This function always returns [false] when
  95. optimizations are disabled (when {!Globals.args}[.optimize = false]). *)
  96. val is_immediate_const : const -> bool
  97. (** Check if a node has an array type. I.e., [Array] or [ArrayDims]. *)
  98. val is_array : node -> bool
  99. (** {2 Logging} *)
  100. (** Horizontal line of ['-'] characters, used to separate output sections. *)
  101. val hline : string
  102. (** Print the stringification of a node to [stderr] (uses
  103. {!Stringify.node2str}). *)
  104. val prt_node : node -> unit
  105. (** Output a line to stderr if the verbosity level in {!Globals.args} is at
  106. least as high as the specified verbosity level. The line is indented with a
  107. number of spaces to match the longest phase identifier (so that logged lines
  108. align with ideitifiers logged by {!Main.main}). A newline is added
  109. automatically. *)
  110. val log_line : int -> string -> unit
  111. (** Print a line to [stderr] without indent (but do add a newline). *)
  112. val log_plain_line : int -> string -> unit
  113. (** Same as {!log_line}, but prints a node stringification instead of a literal
  114. string. *)
  115. val log_node : int -> node -> unit
  116. (** Generate an location tuple from Lexing data structures *)
  117. val loc_from_lexpos : Lexing.position -> Lexing.position -> location
  118. (** Print location tuple to stderr. Produces
  119. something like the following: {v
  120. int foo;
  121. ^^^ v}
  122. The location tuple is likely to originate from a node annotation, extracted
  123. using {!locof}.*)
  124. val prerr_loc : location -> unit
  125. (** Print location tuple to stderr, along with an error message. Produces
  126. something like the following: {v
  127. File "foo.cvc", line 10, characters 8-11:
  128. <message>
  129. int foo;
  130. ^^^ v} *)
  131. val prerr_loc_msg : location -> string -> unit
  132. (** Print an error message for a node. Calls {!prerr_loc_msg}. *)
  133. val node_error : node -> string -> unit
  134. (** Print a warning message for a node. Calls {!prerr_loc_msg}. *)
  135. val node_warning : node -> string -> unit
  136. (** Print an error message of type {!Types.error_msg}. *)
  137. val print_error : error_msg -> unit
  138. (** Raise a {!Types.FatalError} if the given error list is not empty, and
  139. print the errors before quitting. *)
  140. val quit_on_error : node -> error_msg list -> node
  141. (** {2 String utilities} *)
  142. (** [repeat s n] returns a new string of [n] times [s]. *)
  143. val repeat : string -> int -> string
  144. (** [expand n s] adds spaces to [s] until the resulting string is at least [n]
  145. characters long. *)
  146. val expand : int -> string -> string
  147. (** {2 List utilities} *)
  148. (** [optmap f opt] maps [f] to the list value of [opt] if [opt] exists, and
  149. [None] otherwise. *)
  150. val optmap : ('a -> 'b) -> 'a list option -> 'b list option
  151. (** Same as {!optmap}, but returns the list value instead, or an empty list if
  152. [opt] is [None]. *)
  153. val optmapl : ('a -> 'b) -> 'a list option -> 'b list
  154. (** [List.mapi] clone (only available in OCaml version >= 4.00. Maps a function
  155. to a list like [List.map] does, but the iterator function is called with the
  156. element's index as an additional argument. *)
  157. val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list