<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi,<br>
      it seems like the binding generator is emitting bindings for
      private string fields?</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h#L211">https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h#L211</a></p>
    <p>While PhantomData seems some magic Rust thingie:</p>
    <p><a class="moz-txt-link-freetext" href="https://doc.rust-lang.org/stable/std/marker/struct.PhantomData.html">https://doc.rust-lang.org/stable/std/marker/struct.PhantomData.html</a></p>
    <p>Which is probably used to deal with lifetime of the string char
      array (but this is a guess).</p>
    <p>Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 17/08/2023 05:14, Rel wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:Ti-jM61LowYfcCfccb85mxSJYO1KLQ9l5MiHNkMXmpv0ZE4bBPvZ87IC6X4BSFgC6EaWQq3ac9pDIMioQY6B-PBsMBBH8n9RlppndLV7WYA=@proton.me">
      
      <span>Hi,</span>
      <div><br>
      </div>
      <div><span>I start to look on rust-bindgen and did few experiments
          here:</span></div>
      <div><br>
      </div>
      <div><span><a target="_blank" rel="noreferrer nofollow noopener" href="https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments/blob/main/rust-bindgen/README.md__;!!ACWV5N9M2RV99hQ!JKkwWpDI9MsiWp4MJSA43oz6BbNqfK_Vikm4I5JiB87pf4lcNF8rWmmkvMYVHG1RYRG4-KJx-_sorV5wvpEAntI$" moz-do-not-send="true">https://github.com/enatai/panamaexperiments/blob/main/rust-bindgen/README.md</a></span></div>
      <div><br>
      </div>
      <div><span>My question is around what rust-bindgen generated for
          std::string</span></div>
      <div><br>
      </div>
      <div><span>Mostly those are ordinary data fields for which
          jextract possibly can generate layout but I don't really
          understand purpose of:</span></div>
      <div><br>
      </div>
      <div><span>pub _phantom_0:
          ::std::marker::PhantomData<::std::cell::UnsafeCell<_CharT>>,</span></div>
      <div><br>
      </div>
      <div><span>and what alternative for that can be from jextract. Is
          it some special Rust feature, or?</span></div>
      <span></span>
      <div class="protonmail_quote"> ------- Original Message -------<br>
        On Monday, May 29th, 2023 at 9:00 AM, Maurizio Cimadamore
        <a class="moz-txt-link-rfc2396E" href="mailto:maurizio.cimadamore@oracle.com"><maurizio.cimadamore@oracle.com></a> wrote:<br>
        <br>
        <blockquote class="protonmail_quote" type="cite">
          <p><br>
          </p>
          <div class="moz-cite-prefix">On 29/05/2023 00:20, Rel wrote:<br>
          </div>
          <blockquote type="cite"> <span>dynamic dispatch</span>
            <div><span><br>
              </span></div>
            <div><span>I tried following example [<a href="https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments/blob/main/libcppexperiments/src/main/public/happy.hpp__;!!ACWV5N9M2RV99hQ!JnMeRRKWI0A8qpGkiXzcR0f06AAFEP-dXJV8lT-ZAMcxwE3BhDeGj91EgKkeMudSckPxr_-N9AlNWSGCtyvUZL0$" rel="noreferrer nofollow noopener" target="_blank" moz-do-not-send="true">https://github.com/enatai/panamaexperiments/blob/main/libcppexperiments/src/main/public/happy.hpp</a>]
                and it works fine as long as we generate proper Java
                bindings.</span></div>
            <div><br>
            </div>
            <div><span>See test for it [<a href="https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments/blob/main/cppexperiments/src/test/java/cppexperiments/HappyTests.java*L36__;Iw!!ACWV5N9M2RV99hQ!JnMeRRKWI0A8qpGkiXzcR0f06AAFEP-dXJV8lT-ZAMcxwE3BhDeGj91EgKkeMudSckPxr_-N9AlNWSGCRH2ufOA$" rel="noreferrer nofollow noopener" target="_blank" moz-do-not-send="true">https://github.com/enatai/panamaexperiments/blob/main/cppexperiments/src/test/java/cppexperiments/HappyTests.java#L36</a>]</span></div>
            <div><br>
            </div>
            <div><span>Please let me know which dynamic dispatch use
                cases you are concerned with. Because this one seems
                works fine.</span></div>
          </blockquote>
          <p>Sorry, I can see that working fine because you declare a
            "static" function which accepts a point, so the vtable
            indirection is generated by the CPP compiler in that
            function.</p>
          <p>What I'm worried about is calling virtual methods on
            classes. E.g. calling your "distance" function directly.
            Jextract gives you two possibilities: Point2d::distance and
            Point3d::distance. If you pass a Point3d object to
            Point2d::distance you will "only" get Point2d::distance to
            be called (as if there was no dynamic dispatch).<br>
          </p>
          <p><br>
          </p>
          <blockquote type="cite">
            <div><br>
            </div>
            <div><span>std::string</span></div>
            <div><span>I totally forgot that such basic type like
                std::string in C++ is a template. </span></div>
            <div><span>But it seems possible to call functions which
                operate with string objects because symbols for them are
                present:</span></div>
            <div><br>
            </div>
            <div><span>00000000000013ad T
                _ZN7unhappy10helloWorldB5cxx11Ev</span></div>
            <div><br>
            </div>
            <div><span>std::string helloWorld();</span></div>
            <div><br>
            </div>
            <div><span>I guess it is possible to create/extract layout
                for std::string using FFM but:</span></div>
            <div><span>- how to initialize this layout from Java? we
                cannot just call std::string constructor for it, right?</span></div>
            <div><span>- this layout may differ between different C++
                runtimes (libstdc++ etc). MS C++ may have not same
                std::string layout as GCC</span></div>
          </blockquote>
          <p>On the latter, e.g. layout difference, this is no different
            than anything else with jextract. E.g. each jextract run is
            platform-dependent, as it pulls in header files that are
            heavily influenced by the platform and OS you run on.</p>
          <p>If I understand correctly, "string" is the "instantiation"
            of a template in C++. (e.g. some basic_string<char>).
            That instantiation is fully defined (e.g. not partial), and
            I believe it should be possible, with libclang, to obtain
            more information about it - such as the layout etc. (for
            partial template instantiation, my understanding, reading on
            what Rust bindgen does is that it is not possible to handle
            them with libclang).</p>
          <p>So, ideally, we should be able to construct a layout for
            basic_string<char>, and then pass that to the
            constructor, yes.</p>
          <p>Maurizio<br>
          </p>
          <p><br>
          </p>
          <blockquote type="cite">
            <div><br>
            </div>
            <div><span>> Their binding generator adopts the same
                simple approach as the one I showed in the patch.</span></div>
            <div><br>
            </div>
            <div><span>I will take a look</span></div>
            <div><br>
            </div>
            <span></span>
            <div style="font-family: Arial, sans-serif; font-size:
              14px;"><br>
            </div>
            <div class="protonmail_quote"> ------- Original Message
              -------<br>
              On Tuesday, May 23rd, 2023 at 8:58 AM, Maurizio Cimadamore
              <a href="mailto:maurizio.cimadamore@oracle.com" class="moz-txt-link-rfc2396E" rel="noreferrer nofollow
                noopener" target="_blank" moz-do-not-send="true"><maurizio.cimadamore@oracle.com></a>
              wrote:<br>
              <br>
              <blockquote type="cite" class="protonmail_quote">
                <p><br>
                </p>
                <div class="moz-cite-prefix">On 23/05/2023 05:11, Rel
                  wrote:<br>
                </div>
                <blockquote type="cite"> <span>> What I meant for
                    "robust analysis" was to try and establish how many
                    _real-world_ C++ library can really be tackled in
                    such a direct approach. </span>
                  <div><br>
                  </div>
                  <div><span>Ohh I see now, I am affraid we know the
                      answer for this :)</span></div>
                  <div><br>
                  </div>
                  <div><span>Let's imagine if number of C++ libraries
                      which can be covered end-to-end with "simple"
                      approach is 0, does it mean that we should discard
                      it and only focus on shim for binding all kinds of
                      APIs? What about those cases which can be easily
                      extracted using "simple" approach, like Point2d?</span></div>
                </blockquote>
                Perhaps we should reach out to the Rust community? Their
                binding generator adopts the same simple approach as the
                one I showed in the patch. Given how hard it is to
                support C++ (because the underlying libclang C API is
                not very solid in that respect), I'd be surprised if
                they maintained all the necessary code just for stuff
                like Point2d?<br>
                <blockquote type="cite">
                  <div><br>
                  </div>
                  <div><span>Because I thought that we would like to do
                      "analysis" of what C++ use cases can/cannot be
                      covered with "simple" approach. For example from
                      your previous message I see that we are not
                      completely sure about exceptions:</span></div>
                  <div><br>
                  </div>
                  <div><span>> * (probably way more stuff, like
                      exceptions, etc.)</span></div>
                  <div><br>
                  </div>
                  <div><span>Similarly for myself I would like to see
                      what are the problems with "dynamic dispatch". I
                      added it to "unhappy" for now, because we expect
                      it not to work, but I plan to test it to see what
                      are the issues there and share here. Similarly
                      with that anyone would be able to reproduce and
                      see same results.</span></div>
                  <div><br>
                  </div>
                  <div><span>I guess my question now is: do we think it
                      may be useful to know exactly how many C++ use
                      cases can be covered with "simple" FFM approach.
                      And if answer is yes, then we can use
                      panamaexperiments as a playground where we can
                      have tests for what is covered. This (possibly?)
                      can give us more confidence in limitations of
                      "simple" approach and how far we can go with it
                      (and this can be easily demonstrated to everyone
                      just by running those tests)<br>
                    </span></div>
                </blockquote>
                <p>I think it would be useful to know some answer to
                  that question, yes. My intuition tells me that there
                  are probably two kinds of C++ libraries: those who
                  were born that way, and those that moved over from
                  being simpler C libraries. One such example in the
                  latter category is OpenCV [1]. While its "core" header
                  [2] declares an exception, as well as a bunch of
                  classes, eyeballing it, it doesn't seem "too"
                  problematic? Perhaps that would be a good point where
                  to start, and, if a library such as that can be used
                  with some degree of success, perhaps we can expand the
                  search to other similar libraries.</p>
                <p>[1] - <a target="_blank" rel="noreferrer nofollow
                    noopener" class="moz-txt-link-freetext" href="https://urldefense.com/v3/__https://opencv.org/__;!!ACWV5N9M2RV99hQ!JnMeRRKWI0A8qpGkiXzcR0f06AAFEP-dXJV8lT-ZAMcxwE3BhDeGj91EgKkeMudSckPxr_-N9AlNWSGC3X8vPfk$" moz-do-not-send="true">https://opencv.org/</a><br>
                  [2] - <a target="_blank" rel="noreferrer nofollow
                    noopener" class="moz-txt-link-freetext" href="https://urldefense.com/v3/__https://github.com/opencv/opencv/blob/4.x/modules/core/include/opencv2/core.hpp__;!!ACWV5N9M2RV99hQ!JnMeRRKWI0A8qpGkiXzcR0f06AAFEP-dXJV8lT-ZAMcxwE3BhDeGj91EgKkeMudSckPxr_-N9AlNWSGCszOjc6Y$" moz-do-not-send="true">https://github.com/opencv/opencv/blob/4.x/modules/core/include/opencv2/core.hpp</a><br>
                </p>
                <blockquote type="cite">
                  <div style="font-family: Arial, sans-serif; font-size:
                    14px; color: rgb(0, 0, 0);"><br>
                  </div>
                  <div style="font-family: Arial, sans-serif; font-size:
                    14px; color: rgb(0, 0, 0);">Ideas?<br>
                  </div>
                  <span></span>
                  <div style="font-family: Arial, sans-serif; font-size:
                    14px;"><br>
                  </div>
                  <div class="protonmail_quote"> ------- Original
                    Message -------<br>
                    On Monday, May 22nd, 2023 at 9:14 AM, Maurizio
                    Cimadamore <a target="_blank" rel="noreferrer
                      nofollow noopener" class="moz-txt-link-rfc2396E" href="mailto:maurizio.cimadamore@oracle.com" moz-do-not-send="true"><maurizio.cimadamore@oracle.com></a>
                    wrote:<br>
                    <br>
                    <blockquote class="protonmail_quote" type="cite">
                      <p><br>
                      </p>
                      <div class="moz-cite-prefix">On 22/05/2023 04:12,
                        Rel wrote:<br>
                      </div>
                      <blockquote type="cite">
                        <blockquote style="color: #8c8c8c;" type="cite">
                          <pre class="moz-quote-pre">But I believe some more robust
analysis should be made to understand exactly how many APIs can be
supported in this "simple" fashion.
</pre>
                        </blockquote>
                        <pre class="moz-quote-pre">Yes, I started to gather such analysis here <a href="https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments__;!!ACWV5N9M2RV99hQ!KOxdK2qmmSzlaaO3kSlSUG2-ifWAVRD6OHlz9NHYvuggmy7NnnNxWvHYcxDm0Vn4gPXlbasjfC-ehIx_KlWNYiU$" class="moz-txt-link-freetext" rel="noreferrer nofollow noopener" target="_blank" moz-do-not-send="true">https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments__;!!ACWV5N9M2RV99hQ!KOxdK2qmmSzlaaO3kSlSUG2-ifWAVRD6OHlz9NHYvuggmy7NnnNxWvHYcxDm0Vn4gPXlbasjfC-ehIx_KlWNYiU$</a>
Currently there is only one happy case [<a href="https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments/blob/main/libcppexperiments/src/main/public/happy.hpp__;!!ACWV5N9M2RV99hQ!KOxdK2qmmSzlaaO3kSlSUG2-ifWAVRD6OHlz9NHYvuggmy7NnnNxWvHYcxDm0Vn4gPXlbasjfC-ehIx_K7Nl50c$" class="moz-txt-link-freetext" rel="noreferrer nofollow noopener" target="_blank" moz-do-not-send="true">https://urldefense.com/v3/__https://github.com/enatai/panamaexperiments/blob/main/libcppexperiments/src/main/public/happy.hpp__;!!ACWV5N9M2RV99hQ!KOxdK2qmmSzlaaO3kSlSUG2-ifWAVRD6OHlz9NHYvuggmy7NnnNxWvHYcxDm0Vn4gPXlbasjfC-ehIx_K7Nl50c$</a> ] which is Point2d class from your foo.hpp file.</pre>
                      </blockquote>
                      <p>This is not too surprising - after all the
                        hacky changes I shared were built around that
                        example.</p>
                      <p>What I meant for "robust analysis" was to try
                        and establish how many _real-world_ C++ library
                        can really be tackled in such a direct approach.
                        My feeling is "not many" - but I don't have any
                        hard data to back up this claim.</p>
                      <p>Maurizio<br>
                      </p>
                    </blockquote>
                    <br>
                  </div>
                </blockquote>
              </blockquote>
              <br>
            </div>
          </blockquote>
        </blockquote>
        <br>
      </div>
    </blockquote>
  </body>
</html>