Skip to content
This repository
tag: v1433
Fetching contributors…

Cannot retrieve contributors at this time

file 542 lines (435 sloc) 18.826 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(**
Generic Library for compilers working in passes.

@author Mathieu Barbin
@author Quentin Bourgerie
@author Valentin Gatien-Baron
*)

(**
{6 A compiler working in passes}

- First {b parse command line} with module [Arg] and adding to options
specifications [Arg.options].

- Second {b write your passes} Your passes can have
preconditions and/or postconditions (See {!passCond}). And it
should have a function which compute on a environment and
eventually redefine [printer] and [iter_tracker], mostly if the
environment type has been transformed.

- Third {b create initial environment}, you can use either
[init] for make a empty environment or [make_env] for make your
initial environment.

- Finally {b handle your passes}. You can use two programming
style for make this.

{9 Simple programming style}

Use the pass handlers functions (See Section {!passHandlers})
Thereafter a typical example :
{[
let _ = Arg.parse (PassHandler.Arg.options @ ... ) ...

(* An if function *)
let if_toto ~options e = options.toto

(* The main handling *)
let e = PassHandler.init
let e = PassHandler.handler "MyFirstPass" pass_First
let e = PassHandler.if_handler ~if_:if_toto "MyTotoPass" pass_Toto
...
let r = PassHandler.return e
]}

{9 Binary operators programming style}

For handle your passes you can use also the binary operators (See
{!binop}), it's a most pretty programming style. You can see that
on this example :
{[
(* Parsing command line *)
let _ = Arg.parse (PassHandler.Arg.options @ ... ) ...

(* An if function *)
let if_toto ~options e = options.toto

(* The main handling *)
open PassHandler

let r =
init
|+> ("MyFirstPass", pass_First)
|?> (if_toto,
"MyTotoPass", pass_Toto)
...
|> return
]}

*)

(** {6 Options} *)
(** Provides a specification list for parse line command. *)
module Arg : sig
  (* TODO: document this *)
  (** The specification list of options for [PassHandler]. Define :
- --check-all
- --check-pass
- --check
- --track-all
- --track-pass
- --track
- --track-dir
- --print-all
- --print-pass
- --print
*)
  val options : (Base.Arg.key * Base.Arg.spec * Base.Arg.doc) list

end

(** Set title of the pass system.*)
val set_title : string -> unit

(**{6 Passes environment} *)

(** Just a alias for readability of the interface *)
type passname = string

(** Type of a env printer *)
type 'env printer = 'env PassTracker.printer

(** Type of a env tracker *)
type 'env tracker = 'env PassTracker.tracker

(**
Type for identifying printers and trackers

use [--print-help | --track-help] for listing all available printers
and trackers.
Constructor of id are [define_printer] and [define_tracker]
*)
(** *)
type printer_id
type tracker_id

(**
Type of environment

The environment contains a local ['env] which is the real compilation
environment, the [options] of the compilation, and [printers] and [trackers]
are functions to follow the transformations of the 'env along of passes.

Todo: see if there is generalization problems if we remove the ['opt ->]
from [printers and trackers]. Maybe the interface would be simpler.
*)
type ('opt, 'env) one_env = {
  env : 'env;
  options : 'opt;
  printers : 'opt -> (printer_id * 'env printer) list;
  trackers : 'opt -> (tracker_id * 'env tracker) list;
}

(** Create a environment. By default have no printers and no
trackers. *)
val make_env :
  ?printers:('opt -> (printer_id * 'env printer) list) ->
  ?trackers:('opt -> (tracker_id * 'env tracker) list) ->
  'opt -> 'env -> ('opt, 'env) one_env

(**{6:passCond Passes condition} *)
(** Type of identifier of condition *)
type cond_id

(** Type of pass condition *)
type 'env cond

(**
Define a condition on the pass handler system.

This function does a side effect on a local table.
Conditions are related to a specific warning class,
which must be a child of the [cond] warning class.
@see "WarningClass" for pre/post conditions warnings
@raise Invalid_argument if the condition is already
defined.
*)
val define_cond : WarningClass.wclass -> cond_id

(**
Define a printer on the pass handler system.

Like [define_cond], this function does a side effect on
a local table, for checking the presence of a printer
when the compiler is called with the option [--print]
@raise Invalid_argument if the condition is already
defined.
*)
val define_printer : string -> printer_id

(**
Same function than [define_printer] but for tracker [--track]
instead of printer.
*)
val define_tracker : string -> tracker_id

(**
Return the string version of a cond_id.
It is the same string than the string version
of the warning class, which is the same string
than what parse WarningClass.Arg.options.

Example: ["cond.annot.unicity"]
Warning class are hierarchic, and children are separated
by a '.'

@see "WarningClass" for more details.
*)
val cond_id : cond_id -> string

(**
Return the string version of a printer_id
*)
val printer_id : printer_id -> string

(**
Return the string version of a tracker_id
*)
val tracker_id : tracker_id -> string

(** Make a condition.
+ The first argument is the [cond_id] of the
condition which must have been created with [define_cond].
+ Second it's a function to check the environment.
The function which checks the environment should necessarily use
a function dedicated to condition checkers if it want to fail.
+ ErrorManagment in this module
+ [LangCheck.check_fail] and related functions

Internally, the good way for a checker to fail is to perform
a warning of its class definition. That is what is done
in any higher level function which takes all the [cond_id]
as argument which has been built with the warning class,
so that no confusion is possible.

Read carefully the documentation before writing a checker.*)
val make_condition : cond_id -> ('env -> unit) -> 'env cond

(**
Compose condition.
It asserts that all conditions have been built with the same [cond_id].
Uses [List.hd] internally.
*)
val compose_condition : 'env cond list -> 'env cond

(**
Apply a function before the condition evaluation
*)
val compose_fun_condition : ('env_a -> 'env_b) -> 'env_b cond -> 'env_a cond

(** Check a condition. Usefull for check yourself a condition. *)
val check_condition : 'env -> 'env cond -> unit

(**{6 Invariants} *)
(** Type of an invariant *)
type ('env, 'env2) invariant

(** Make an invariant with two conditions. These conditions must be
have the same id else raise [Invalid_argument]. *)
val make_invariant : 'env cond -> 'env2 cond -> ('env, 'env2) invariant

(**
Make a new invariant with another environement : we apply a function before the invariant checking
*)
val compose_fun_invariant : ('env_1b -> 'env_1a) -> ('env_2b -> 'env_2a) -> ('env_1a, 'env_2a) invariant -> ('env_1b, 'env_2b) invariant

(** Make an invariant for a constant pass. *)
val make_cons_invariant : 'env cond -> ('env, 'env) invariant

(** {6 Passes}*)
(** Type of pass. *)
type ('opt, 'opt2, 'env, 'env2) pass = {
  invariant : ('env, 'env2) invariant list;
  precond : 'env cond list;
  postcond : 'env2 cond list;
  f : ('opt, 'env) one_env -> ('opt2, 'env2) one_env;
}

(** Make a pass from a function which takes a environment and returns an
another. And from optional pre and post conditions. *)
val make_pass :
  ?invariant:('env, 'env2) invariant list ->
  ?precond:'env cond list ->
  ?postcond:'env2 cond list ->
  (('opt, 'env) one_env -> ('opt2, 'env2) one_env) ->
  ('opt, 'opt2, 'env, 'env2) pass

(**{6:passHandlers Passes handlers} *)
(** Initial (or unit) environment. *)
val init : (unit, unit) one_env

(** Return handler, extract environment of generic environment. *)
val return : ('opt, 'env) one_env -> 'env

(** [handler name pass] Handle [pass] named by the given [name].
[count_time] indicates whether the time of this pass should be stored
*)
val handler :
  ?count_time:bool ->
  passname -> ('opt, 'opt2, 'env, 'env2) pass ->
  ('opt, 'env) one_env -> ('opt2, 'env2) one_env

(** Handle pass if [_if] returns true. *)
val if_handler :
  ?if_:(options:'opt -> 'env -> bool) ->
  passname -> ('opt, 'opt, 'env, 'env) pass ->
  ('opt, 'env) one_env -> ('opt, 'env) one_env

(** [alt_handler if_ (name1, pass1) (name2, pass2)] handle pass1 if
[if_] return [true] else handle pass2 *)
val alt_handler :
  (options:'opt -> 'env -> bool) ->
  (string * (('opt, 'opt2, 'env, 'env2) pass)) ->
  (string * (('opt, 'opt2, 'env, 'env2) pass)) ->
  ('opt, 'env) one_env -> ('opt2, 'env2) one_env

(** [switch_handler select switch_pass] [select] should produce from
pass environment a value that be used by [switch_pass] to switch
to the wanted pass. *)
val switch_handler :
  (options:'opt -> 'env -> 'switch) ->
  ('switch -> (string * (('opt, 'opt2, 'env, 'env2) pass))) ->
  ('opt, 'env) one_env -> ('opt2, 'env2) one_env

(** Compose if functions. The resulting function return true if all
composed functions returns true.*)
val and_if :
  (options:'opt -> 'env -> bool) list -> (options:'opt -> 'env -> bool)

(** Compose if functions. The resulting function return true if at
least one composed functions returns true.*)
val or_if :
  (options:'opt -> 'env -> bool) list -> (options:'opt -> 'env -> bool)

(**{6:binop Binary operators} *)
(** A binary operator for [handler] *)
val (|+>) :
  ('opt, 'env) one_env ->
  (passname * ('opt, 'opt2, 'env, 'env2) pass) ->
  ('opt2, 'env2) one_env

(** A binary operator for [if_handler] *)
val (|?>) :
  ('opt, 'env) one_env ->
  ((options:'opt -> 'env -> bool) *
     passname * ('opt, 'opt, 'env, 'env) pass) ->
  ('opt, 'env) one_env

(** A binary operator for [alt_handler] *)
val (<?>) :
  ('opt, 'env) one_env ->
  ((options:'opt -> 'env -> bool)
   * (passname * (('opt, 'opt2, 'env, 'env2) pass))
   * (passname * (('opt, 'opt2, 'env, 'env2) pass))) ->
  ('opt2, 'env2) one_env

(** A binary operator for [switch_handler] *)
val (|?|) :
  ('opt, 'env) one_env ->
  (options:'opt -> 'env -> 'switch) *
  ('switch -> (string * (('opt, 'opt2, 'env, 'env2) pass))) ->
  ('opt2, 'env2) one_env

(** A binary operator for make a pipe [a |> f] equals to [f a] *)
val (|>) : 'a -> ('a -> 'b) -> 'b

(**
A few combinators for if_handlers
*)
val (or) : (options:'opt -> 'env -> bool) -> (options:'opt -> 'env -> bool) -> (options:'opt -> 'env -> bool)
val (&) : (options:'opt -> 'env -> bool) -> (options:'opt -> 'env -> bool) -> (options:'opt -> 'env -> bool)
val neg : (options:'opt -> 'env -> bool) -> (options:'opt -> 'env -> bool)

(**{6 Error management} *)

(**
Internally, the [handler] function performs checks, printing, tracking, etc...
For the good behavior of the system, error flows should be controlled,
for an optimal reporting.

That means that a discipline is asked to what passes and checkers do
in case of errors. They should follow the rules.
*)

(**{9 Functions used by passes}*)

(**
This functions are called indirectly by a pass when a condition has been
violated but was not checked before the pass.
It is not called directly, but overlayed with a [LangError] module, which
precise once for all the ['context printer] corresponding to the context
of the language it checks.

During the passsystem, if this function is called, it will lead to try to
start the corresponding check on the previous compilation environment
(the one returned by the previous pass), for having a better error report.

If the precondition is found in the list of precondition of the pass,
the check is done. If the test was already activated, somebody is laying,
this error is reported too. If the precondition is not part of the pass,
the pass is declared incoherent, and the error is reported.

It stores everything possible in the track system, and exit.

Do not use this function Outside of the passsystem, this function
would raise an internal exception not exported in the api.
*)
val cond_violation : 'context printer -> cond_id -> 'context -> ('c, 'error) OManager.oformat -> 'c

(**
The same function as [cond_violation] but returns unit.
It allows you to report several errors before failing.
*)
val scond_violation : 'context printer -> cond_id -> 'context -> ('c, unit) OManager.oformat -> 'c

(**
Anonymous Internal error.

Sense of the [cond_id option]
+ With [Some cond_id] : it is an alias for [cond_violation]
+ With [None] :
Basically the same function than [cond_violation], but this error cannot be related to a precondition.

Why we let the [cond_id option] in the interface ? So that if you uses this function, and
ask yourself about what you should do with it, it will normally lead you until this part
of the documentation. So please, read the following :

<!> The case [None] is pretty rare, so consider that if you are using this function without precising
a related condition to your internal error, you could maybe extract for your current problem
a generic enough invariant to be checked officially at some point during the passes, and let
potentially other passes take benefits of this check.

If you need a new condition, consider adding this condition in the [LangCheck] module
corresponding to the language your are compiling, and then add the precondition to your pass,
and finally use [Some cond] or directly [cond_violation] instead of [i_error] in your pass.
*)
val i_error : 'context printer -> cond_id option -> 'context -> ('c, 'error) OManager.oformat -> 'c

(** The same function [i_error] but returns unit for several errors reporting *)
val i_serror : 'context printer -> cond_id option -> 'context -> ('c, unit) OManager.oformat -> 'c

(** {9 Functions used by conditions checkers} *)

(**
Condition failure: [check_fail] and [scheck_fail].

+ These functions are usually not called directly by checkers because it would duplicate
the work of context formatting for a same language. (how to print an error message depending
on the expressions, code kind your are manipulating, etc...)
+ Normally, you should find a [LangError] module dedicated to your language which uses
this functions but applying specifics [printers] and [context] once for all, so that
you can call directly functions from [LangError] without specifying the printers used
for error reporting in your language.
+ There is a simple functor in [PassError] for making the life of [LangError] easier.
+ If you are outside of a language, or if your context is very different that the
common error context defined in [LangError], you are invited to call this function directly,
specifying [printers] which apply to your local ad-hoc context.
+ Typically, these functions are indirectly used in [LangCheck] modules, using the [LangError]
redefinition. [LangError] is invited to keep the same names by redefining functions, for clarity.
+ These functions should not be used by a pass, only by checkers !
if a pass failed because of a broken invariant related to a cond,
the pass should use a function from [LangError] dedicated to this case.

Order of args are for partial application :
The [LangError] module knows how to print its own context
because it defines the type context :

In langError.ml :
{[
type context = ...
let context_printer = ....
let check_fail c = PassHandler.check_fail context_printer c
]}

In a checker, we will use :
{[
let rewrite ... =
let context = ... in
Lang.Error.check_fail context Lang.Check.Cond.check "%s is broken" "this invariant"
]}

Finally for readability of the code, you can define as soon as you have the context in your scope
an alias for [Lang.Error.check_fail] with e.g. ['!!'] which looks like a danger sign.

{[
let my_check code =
let rec aux expr =
....
(* from there you can build your context, and then : *)
let (!!) = Lang.error.check_fail mycond_id context in
...
match ... with
| ... -> !! "this is an %s" "error"
| e -> !! "an other one related to %s" (... e)
]}

*)

(**
What is happening with a [check_fail] ?

+ Will store the full context in the track system, using the [full] printer
+ Will print a reduced version on stderr using the [console] printer
+ Will call a warning with the given format, using the warning class of the cond_id

@see "PassError" for a common implementation of context overlay.
*)
val check_fail : full:'context printer -> console:'context printer -> cond_id -> 'context -> ('c, 'error) OManager.oformat -> 'c

(**
The same function, but for outputting many messages without killing the check with the first error.
*)
val scheck_fail : full:'context printer -> console:'context printer -> cond_id -> 'context -> ('c, unit) OManager.oformat -> 'c

(** {6 Marshal Pass} *)

val register_printer : (PassTracker.passname -> ('opt -> (printer_id * 'env PassTracker.printer) list) option) -> unit

(** {6 Deprecated API} *)

(** Old passes handlers
This handlers shouldn't used except for handle old passes which are not yet ported, or which will never be. *)

(** Type of an old pass.
@deprecated Use [pass]
*)
type ('opt, 'env, 'env2) old_pass = options:'opt -> 'env -> 'env2

(** Handle old passes which transform environment. Generated
environment contains default printer and tracker.
@deprecated Use [handler] *)
val old_handler :
  ?precond:('env cond list) ->
  ?postcond:('env2 cond list) ->
  passname -> ('opt, 'env, 'env2) old_pass ->
  ('opt, 'env) one_env -> ('opt, 'env2) one_env

(** Handle old passes.
@deprecated Use [if_handler] *)
val old_if_handler :
  ?if_:(options:'opt -> 'env -> bool) ->
  ?precond:('env cond list) ->
  ?postcond:('env cond list) ->
  passname -> ('opt, 'env, 'env) old_pass ->
  ('opt, 'env) one_env -> ('opt, 'env) one_env
Something went wrong with that request. Please try again.