Module Ocaml_typing.ShapeSource
Shapes are an abstract representation of modules' implementations which allow the tracking of definitions through functor applications and other module-level operations.
The Shape of a compilation unit is elaborated during typing, partially reduced (without loading external shapes) and written to the cmt file.
External tools can retrieve the definition of any value (or type, or module, etc) by following this procedure:
- Build the Shape corresponding to the value's path:
let shape = Env.shape_of_path ~namespace env path
- Instantiate the
Shape_reduce.Makefunctor with a way to load shapes from external units and to looks for shapes in the environment (usually usingEnv.shape_of_path).
- Completely reduce the shape:
let shape = My_reduce.(weak_)reduce env shape
- The
Uid.tstored in the reduced shape should be the one of the definition. However, if theapproximatefield of the reduced shape istruethen theUid.twill not correspond to the definition, but to the closest parent module's uid. This happens when Shape reduction gets stuck, for example when hitting first-class modules.
- The location of the definition can be easily found with the
cmt_format.cmt_uid_to_decltable of the corresponding compilation unit.
See:
A Uid.t is associated to every declaration in signatures and implementations. They uniquely identify bindings in the program. When associated with these bindings' locations they are useful to external tools when trying to jump to an identifier's declaration or definition. They are stored to that effect in the uid_to_decl table of cmt files.
We use de Bruijn indices for some binders in Shape.t below to increase sharing. That is, de Bruijn indices ensure that alpha-equivalent terms are actually equal. This reduces redundancy when we emit shape information into the debug information in later stages of the compiler (see dwarf_type.ml), since equal shapes produce the same debug information.
Shape's items are elements of a structure or, in the case of constructors and labels, elements of a record or variants definition seen as a structure. These structures model module components and nested types' constructors and labels.
and desc = | Var of var| Abs of var * t| App of t * t| Struct of t Item.Map.t| Alias of t| Leaf| Proj of t * Item.t| Comp_unit of string| Error of string| Constr of Ident.t * t list| Tuple of t list| Unboxed_tuple of t list| Predef of Predef.t * t list| Arrow| Poly_variant of t poly_variant_constructors| Mu of t(*
*)Mu trepresents a binder for a recursive type with bodyt. Its variables areRec_var nbelow, wherenis a DeBruijn-index to maximize sharing between alpha-equivalent shapes.| Rec_var of DeBruijn_index.t| Variant of (t * Layout.t) complex_constructors| Variant_unboxed of {name : string;variant_uid : Uid.t option;arg_name : string option;(*if this is
*)None, we are looking at a singleton tuple; otherwise, it is a singleton record.arg_uid : Uid.t option;arg_shape : t;arg_layout : Layout.t;
}(*An unboxed variant corresponds to the
*)@@unboxedannotation. It must have a single, complex constructor.| Record of {fields : (string * Uid.t option * t * Layout.t) list;kind : record_kind;
}| Mutrec of t Ident.Map.t(*
*)Mutrec mrepresents a map of (potentially mutually-recursive) declarations. Declarations with type variables are represented as abstractions inside. To project out a declaration,Proj_declcan be used.| Proj_decl of t * Ident.t| Unknown_type(*
*)Unknown_typerepresents an unknown type.| At_layout of t * Layout.t(*
*)At_layout (shape, layout)represents a shape with a known layout.
For DWARF type emission to work as expected, we store the layouts in the declaration alongside the shapes in those cases where the layout "expands" again such as variant constructors, which themselves are values but point to blocks in memory. Here, layouts are stored for the individual fields.
and record_kind = | Record_unboxed(*
*)Record_unboxedis the case for single-field records declared with@@unboxed, whose runtime representation is simply its contents without any indirection.| Record_unboxed_product(*
*)Record_unboxed_productis the truly unboxed record that corresponds to#{ ... }.| Record_boxed| Record_mixed of mixed_product_shape| Record_floats(*Basically the same as
*)Record_mixed, but we don't reorder the fields.
and 'a complex_constructor = {name : string;constr_uid : Uid.t option;kind : constructor_representation;args : 'a complex_constructor_argument list;
}val equal_complex_constructor :
('a -> 'a -> bool) ->
'a complex_constructor ->
'a complex_constructor ->
boolval poly_variant_constructors_map :
('a -> 'b) ->
'a poly_variant_constructors ->
'b poly_variant_constructorsval complex_constructors_map :
('a -> 'b) ->
'a complex_constructors ->
'b complex_constructorsval of_path :
find_shape:(Sig_component_kind.t -> Ident.t -> t) ->
namespace:Sig_component_kind.t ->
Path.t ->
tThis function returns the shape corresponding to a given path. It requires a callback to find shapes in the environment. It is generally more useful to rely directly on the Env.shape_of_path function to get the shape associated with a given path.
DeBruijn Environment for working with the recursive binders.