[PATCH] D138802: [clang][Interp] Implement DecompositionDecls

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 2 01:34:57 PST 2023


tbaeder added inline comments.


================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:1417-1418
+      assert(!BD->getHoldingVar()); // FIXME
+      if (!this->allocateVariable(BD, BD->getBinding()))
+        return false;
+    }
----------------
aaron.ballman wrote:
> tbaeder wrote:
> > aaron.ballman wrote:
> > > <uncertain>Is this correct? IIRC, the decomposition declaration is its own object, but the bindings themselves are references back to the decomposition declaration object directly and so they're not distinct objects themselves (they're more like aliases).</uncertain>
> > Is this not reflected in the individual bindings? What does it mean that the bindings aren't "distinct objects themselves"?
> Bindings are basically a label back to an object. Taking the easy case of a structure being bound:
> ```
> struct S { int i, j; } s;
> 
> int main() {
>   auto [val1, val2] = s;
>   return val1 + val2;
> }
> ```
> The way this works under the hood is akin to:
> ```
> struct S { int i, j; } s;
> 
> int main() {
>   S __s = s; // This is the decomposition declaration
>   return __s.i + __s.j; // And the structured bindings give alternative names to the fields in the decomposition declaration
> }
> ```
> so there's no allocation made for `val1` or `val2` because they're not really objects, just names. You can see that in: https://godbolt.org/z/sdj3Mvqhb (note the LLVM IR, which is identical between `foo` and `bar` aside from debug info).
Ah, I see. I guess my way works as well, but yours is better, so I'll change the patch. I can basically replace the added stuff in `VisitDeclRefExpr()` by just `return this->visit(BD->getBinding())` and that will give me the right thing. I hope that way I can remove some of the `BindingDecl` special cases I've added as well.

Thanks!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138802/new/

https://reviews.llvm.org/D138802



More information about the cfe-commits mailing list