Ocaml_typing.ShapeShapes 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:
let shape = Env.shape_of_path ~namespace env pathShape_reduce.Make functor with a way to load shapes from external units and to looks for shapes in the environment (usually using Env.shape_of_path).let shape = My_reduce.(weak_)reduce env shapeUid.t stored in the reduced shape should be the one of the definition. However, if the approximate field of the reduced shape is true then the Uid.t will 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.cmt_format.cmt_uid_to_decl table of the corresponding compilation unit.See:
module Layout = Jkind_types.Sort.Consttype base_layout = Jkind_types.Sort.basemodule Uid : sig ... endA 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.
module DeBruijn_index : sig ... endWe 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.
module Sig_component_kind : sig ... endmodule Item : sig ... endShape'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.
module Predef : sig ... endtype var = Ident.tand 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 tMu t represents a binder for a recursive type with body t. Its variables are Rec_var n below, where n is 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 @@unboxed annotation. 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.tMutrec m represents a map of (potentially mutually-recursive) declarations. Declarations with type variables are represented as abstractions inside. To project out a declaration, Proj_decl can be used.
| Proj_decl of t * Ident.tFor 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 'a poly_variant_constructors = 'a poly_variant_constructor listand record_kind = | Record_unboxedRecord_unboxed is the case for single-field records declared with @@unboxed, whose runtime representation is simply its contents without any indirection.
| Record_unboxed_productRecord_unboxed_product is the truly unboxed record that corresponds to #{ ... }.
| Record_boxed| Record_mixed of mixed_product_shape| Record_floatsBasically the same as Record_mixed, but we don't reorder the fields.
and 'a complex_constructors = 'a complex_constructor listand 'a complex_constructor = {name : string;constr_uid : Uid.t option;kind : constructor_representation;args : 'a complex_constructor_argument list;}and 'a complex_constructor_argument = {field_name : string option;field_uid : Uid.t option;field_value : 'a;}and constructor_representation = mixed_product_shapeand mixed_product_shape = Layout.t arrayval print : Format.formatter -> t -> unitval equal_record_kind : record_kind -> record_kind -> boolval equal_complex_constructor :
('a -> 'a -> bool) ->
'a complex_constructor ->
'a complex_constructor ->
boolval for_unnamed_functor_param : varval str : ?uid:Uid.t -> t Item.Map.t -> tval poly_variant : ?uid:Uid.t -> t poly_variant_constructors -> tval rec_var : ?uid:Uid.t -> DeBruijn_index.t -> tval variant : ?uid:Uid.t -> (t * Layout.t) complex_constructors -> tval mutrec : ?uid:Uid.t -> t Ident.Map.t -> tval for_persistent_unit : string -> tval leaf_for_unpack : tval poly_variant_constructors_map :
('a -> 'b) ->
'a poly_variant_constructors ->
'b poly_variant_constructorsval complex_constructor_map :
('a -> 'b) ->
'a complex_constructor ->
'b complex_constructorval complex_constructors_map :
('a -> 'b) ->
'a complex_constructors ->
'b complex_constructorsmodule Map : sig ... endval dummy_mod : tval 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.
module DeBruijn_env : sig ... endDeBruijn Environment for working with the recursive binders.