util.mli 8.0 KB

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