@@ -332,16 +332,18 @@ let monomorphize_data_types map = object(self)
332332 * order declarations as they are needed, including that of the node we are
333333 * visiting. *)
334334 method private visit_node (under_ref : bool ) (n : node ) =
335- (* Fast-path: there are no type arguments (so: nothing to monomorphize here)
336- and we are not under a ref (so: no forward declarations to insert here),
337- so we do not visit this lid, and do not self#record it.
335+ (* Fast-path:
336+ - there are no type arguments (so: nothing to monomorphize here)
337+ - we are not under a ref (so: no forward declarations to insert here),
338+ - we are not visiting ourselves either (so: no hopeful forward declaration to insert here)
339+ --> we do not visit this lid, and do not self#record it.
338340
339341 1. If we saw this lid, great.
340342 2. If we haven't seen this lid yet, no problem, its contents will be
341343 visited through self#visit_decl in the case where n = 0 && n_cgs = 0,
342344 and it will be inserted in its original spot, which has the nice
343345 side-effect of preserving the source order.*)
344- if snd3 n = [] && thd3 n = [] && not under_ref then
346+ if snd3 n = [] && thd3 n = [] && not under_ref && Option. map fst @@ Hashtbl. find_opt state n <> Some Gray then
345347 fst3 n
346348 else
347349 (* We recursively visit the arguments. This avoids inconsistencies where
@@ -445,7 +447,15 @@ let monomorphize_data_types map = object(self)
445447 end ;
446448 chosen_lid
447449 | Gray , chosen_lid ->
448- (* FORWARD DECLARATION: simple case of a recursive type that needs a forward declaration *)
450+ (* FORWARD DECLARATION: simple case of a recursive type that needs a
451+ forward declaration.
452+
453+ We still insert something to deal with cases like
454+
455+ typedef struct s {
456+ void f(t x);
457+ } t;
458+ *)
449459 if Options. debug " data-types-traversal" then
450460 KPrint. bprintf " visiting %a: Gray\n " ptyp (fold_tapp n);
451461 begin match Hashtbl. find map lid with
@@ -537,7 +547,9 @@ let monomorphize_data_types map = object(self)
537547 assert (n = 0 && n_cgs = 0 );
538548 (* This was not inserted earlier (see comment at the beginning of
539549 visit_node, so we visit it here *)
550+ Hashtbl. add state (lid, [] , [] ) (Gray , lid);
540551 let d = self#visit_decl false d in
552+ Hashtbl. replace state (lid, [] , [] ) (Black , lid);
541553 Hashtbl. add seen_declarations lid () ;
542554 self#clear () @ [ d ]
543555
0 commit comments