title: Printing members of parent type from an instance of an extended type
body: <p><strong>Scenario (see the code below):</strong> consider some Fortran module <code>my_module</code> which contains the definition of two derived types: <code>test</code> (parent) and <code>sub_test</code> (extends <code>test</code>, subclass). In addition to the two members <code>a</code> and <code>b</code> of the parent class, <code>sub_test</code> defines a new member <code>c</code>. Eventually, this module <code>my_module</code> is to used in some program <code>my_program</code>.</p>
  <pre><code>module my_module
  
  implicit none
  
  private
  public :: test, sub_test
  
  type :: test
    integer :: a = 1
    integer :: b = 2
  end type test
  
  type, extends(test) :: sub_test
    integer :: c = 3
  contains
    procedure, public :: increment
  end type sub_test
  
  contains
  
  subroutine increment (this)
  class(sub_test) :: this
  this % a = this % a + 1
  write(*, *) &quot;Incrementation complete!&quot;
  end subroutine increment
  
  end module my_module
  !---------------------------------------
  program my_program
  
  use my_module
  
  implicit none
  
  type(test)  :: parent
  type(sub_test) :: child
  
  write(*, *) &quot;Printing parent members&quot;
  write(*, *) parent % a, parent % b
  write(*, *) &quot;Printing child members&quot;
  write(*, *) child % a, child % b, child % c
  
  call child % increment
  
  end program my_program
  
  </code></pre>
  <p><strong>Objective:</strong> When debugging, using <code>gdb</code>, we want to print the values of the parent members <code>a</code> and <code>b</code> of some variable of the child (extended) type <code>sub_test</code>. In sum, we're looking for a successful completion of something like:</p>
  <pre><code>(gdb) print child % a
  </code></pre>
  <p>yet, apparently, it is not that trivial.</p>
  <p><strong>Setup before trying to print:</strong></p>
  <ul>
  <li><p>Compile the program (here called <code>test.f90</code>) with the <code>-g</code> flag, convenient for the application of <code>gdb</code> with <code>gfortran -g test.f90 -o a.out</code></p>
  </li>
  <li><p>Run the executable with <code>gdb ./a.out</code></p>
  </li>
  <li><p>Insert a breakpoint a breakpoint in the last <code>write(*, *)</code> statement of the program and in the unique <code>write(*, *)</code> statement in subroutine <code>increment</code> and run the program</p>
  </li>
  </ul>
  <pre><code>(gdb) break test.f90:41
  Breakpoint 1 at 0xb12: file test.f90, line 41.
  (gdb) break test.f90:24
  Breakpoint 2 at 0x955: file test.f90, line 24.
  (gdb) run
  </code></pre>
  <p><strong>What I have (<em>unsuccessfully</em>) tried so far:</strong></p>
  <ul>
  <li>Print directly the members from the <code>child</code> variable (member <code>c</code> prints flawlessly)</li>
  </ul>
  <pre><code>Breakpoint 1, my_program () at test.f90:41
  41  write(*, *) child % a, child % b, child % c
  (gdb) p child%a
  There is no member named a.
  (gdb) p child%b
  There is no member named b.
  (gdb) p child%c
  $1 = 3
  </code></pre>
  <ul>
  <li>Print the <code>test</code> (parent) member of the <code>child</code> variable, as <code>gdb</code> shows it exists</li>
  <li>Print the so-wanted members from the <code>test</code> member of <code>child</code></li>
  </ul>
  <pre><code>Breakpoint 1, my_program () at test.f90:41
  41  write(*, *) child % a, child % b, child % c
  (gdb) p child
  $2 = ( test = ( a = 1, b = 2 ), c = 3 )
  (gdb) p child%test
  A syntax error in expression, near `test'.
  (gdb) p child%test%a
  A syntax error in expression, near `test%a'.
  (gdb) p child%test%b
  A syntax error in expression, near `test%b'.
  </code></pre>
  <ul>
  <li>The same steps above, but this time applied at the breakpoint inside the subroutine, therefore, trying to print the members of the dummy variable <code>this</code>. In this case, <code>gdb</code> clearly changes the pitch regarding the variable and, for instance, <code>c</code> can only be accessed through the hidden member <code>_dat</code>, yet <code>a</code> and <code>b</code> continue unreachable.</li>
  </ul>
  <pre><code>Breakpoint 2, my_module::increment (this=...) at test.f90:24
  24  write(*, *) &quot;Incrementation complete!&quot;
  (gdb) p this
  $3 = ( _data = 0x555555756018 &lt;child&gt;, _vptr = 0x555555755d20 &lt;__my_module_MOD___vtab_my_module_Sub_test&gt; )
  (gdb) p this%_data
  $4 = (PTR TO -&gt; ( Type sub_test )) 0x555555756018 &lt;child&gt;
  (gdb) p this%_data%a
  There is no member named a.
  (gdb) p this%_data%b
  There is no member named b.
  (gdb) p this%_data%c
  $5 = 3
  (gdb) p this%_data%test%a
  A syntax error in expression, near `test%a'.
  (gdb) p this%_data%test%b
  A syntax error in expression, near `test%b'.
  </code></pre>
  <p><strong>Thank you for reading:</strong> I feel like reaching a dead-end in this regards, I deeply hope someone could lend me a hand. <code>gdb</code> seems incredible but, without being able to inspect this information, I sadly fear it won't suit my needs properly.</p>
  <p><strong>More information:</strong> I am using <code>gdb-9.2</code> and <code>gfortran 7.5.0</code>.</p>