Bug creation and email sending has been disabled, file new bugs at gcc.gnu.org/bugzilla

Bug 42

Summary: Non-POD types not implemented
Product: GDC Reporter: Johannes Pfau <johannespfau>
Component: gdcAssignee: Iain Buclaw <ibuclaw>
Status: RESOLVED FIXED    
Severity: normal    
Priority: ---    
Version: development   
Hardware: x86_64   
OS: Linux   

Description Johannes Pfau 2013-03-12 18:58:49 CET
Non-POD types are currently not correctly implemented in GDC.

Because of bugs in DMD it's not 100% clear how non-PODs should work, but this is what I've found:

In dmd, if a non-POD is returned from a function, it's returned by hidden reference. This can be achieved in GDC by doing this:

* Setting the DECL_BY_REFERENCE flag on the result decl.
* Replacing the return type of the function with a reference type of that type
* Fixing up assigments to the result value to do the dereference
* Maybe setting SET_DECL_VALUE_EXPR? (See c++ frontend)

As code: in d_genericize:
+  if (TREE_ADDRESSABLE (TREE_TYPE (DECL_RESULT (fndecl))))
+    {
+      tree t = DECL_RESULT (fndecl);
+      TREE_TYPE (t) = build_reference_type (TREE_TYPE (t));
+      DECL_BY_REFERENCE (t) = 1;
+      TREE_ADDRESSABLE (t) = 0;
+      relayout_decl (t);
+    }

in d_gimplify_expr: (in the )
+	  if(TREE_CODE(op0) == RESULT_DECL && DECL_BY_REFERENCE(op0))
+	  {
+	     TREE_OPERAND (*expr_p, 0) = gen.indirect(op0);
+	     op0 = TREE_OPERAND (*expr_p, 0);
+	  }

In dmd a non-POD is passed in memory to varargs functions. I'm not sure if it's passed by reference, but passing by reference may be necessary, see dmd bug 9704.

AFAICS this can only be achieved by setting TREE_ADDRESSABLE on the non-POD _type_.

In the future a non-POD will probably be passed by reference to normal functions. This is pending dmd bug 9704. This should probably be implemented using build_reference_type and friends for the parameters in the glue layer. Note that as long as 9704 isn't fixed this can't be implemented properly in gdc. Currently the frontend for example passes literals to function calls. We can't take the address of these. We could make a copy in the glue layer, but this should probably be fixed in the frontend.

As a final note:
The TREE_ADDRESSABLE flag on a type does not automatically make sure it's passed by reference to a function, or returned by reference. This must be enforced manually using DECL_BY_REFERECE and build_reference_type as described above.
Comment 1 Iain Buclaw 2013-06-01 14:11:14 CEST
For the time being at least, non-PODs are now *returned* in memory.

https://github.com/D-Programming-GDC/GDC/commit/b805b71ee51ea79af990fab1fa677f762d90ef83


This should make reference returns possible (also needed for NRVO).
Comment 2 Iain Buclaw 2016-06-20 09:52:58 CEST
https://github.com/D-Programming-GDC/GDC/pull/220

Going to mark this as done, as we now implement this properly.