[gnu.g++.bug] "virtual XXX operator XXX" functions

staelin@PRINCETON.EDU (Carl Staelin) (05/25/89)

There seems to be a bug in G++ version 1.35.  I am running g++-1.35 on 
a vax 785 under the mach operating system (but this program was loaded
without just the standard BSD4.3 libraries).  I also ran it on a VAX 8650 
running 4.3+NFS with the same result.

The basic problem seems to be within "virtual XXX operator XXX" functions.  
The child has no means of accessing the parent's "operator XXX" function,
and when it tries, the child simply calls itself.  I have tried several 
means of specifying the parent function from within the child's function
"operator XXX", including: Parent::operator XXX(), (XXX&)*(Parent*)this,
and (XXX&)(Parent&)*this.  All forms cause the child to call itself, not
the parent's function.  (some of them should call the child's routine,
but parent::operator XXX() should call the parent's routine).

Some sample source code follows:

compiled as: g++ -o XXX XXX.cc

// this may look like C code, but it is really -*- C++ -*-

#include <std.h>
#include <stdio.h>
#include <stream.h>
#include <String.h>

class Parent
{
public:
  Parent () { data = 5; }
  ~Parent() {}

  int data;
  virtual String operator String();
};

typedef Parent* Parent_p;

class Child : public Parent
{
public:
  Child () { data = 10 ; }
  ~Child() {}

  virtual String operator String();
};

typedef Child* Child_p;

main (int argc, char* argv[])
{
  Child child;
  String s = (String&)child;
  cerr<<s<<"\n";
}

String Parent::operator String()
{
  cerr<<"parent::operator String(): entering\n";

  String result = form("parent <0x%x> { data = %d }", (unsigned int)this, data);
  return result;
}

String Child::operator String()
{
  cerr<<"Child::operator String(): entering\n";

  String result = Parent::operator String();

  return result;
}

/*
  Carl Staelin
  staelin@princeton.edu
  */

tiemann@YAHI.STANFORD.EDU (Michael Tiemann) (07/29/89)

    There seems to be a bug in G++ version 1.35.  I am running g++-1.35 on 
    a vax 785 under the mach operating system (but this program was loaded
    without just the standard BSD4.3 libraries).  I also ran it on a VAX 8650 
    running 4.3+NFS with the same result.

    The basic problem seems to be within "virtual XXX operator XXX" functions.  
    The child has no means of accessing the parent's "operator XXX" function,
    and when it tries, the child simply calls itself.  I have tried several 
    means of specifying the parent function from within the child's function
    "operator XXX", including: Parent::operator XXX(), (XXX&)*(Parent*)this,
    and (XXX&)(Parent&)*this.  All forms cause the child to call itself, not
    the parent's function.  (some of them should call the child's routine,
    but parent::operator XXX() should call the parent's routine).

The first element of the following diff is a bonus.  The second
element is the fix you need.

yahi% diff -c2 cplus-method.c~ cplus-method.c
*** cplus-method.c~	Fri May 26 00:06:51 1989
--- cplus-method.c	Fri Jul 28 13:48:13 1989
***************
*** 2172,2177 ****
  		&& TREE_CODE (type) != METHOD_TYPE
  		&& (TREE_CODE (type) != POINTER_TYPE
! 		    || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
! 		    || TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE))
  	    {
  	      error ("component `%s' is not a method",
--- 2172,2177 ----
  		&& TREE_CODE (type) != METHOD_TYPE
  		&& (TREE_CODE (type) != POINTER_TYPE
! 		    || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
! 			&& TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)))
  	    {
  	      error ("component `%s' is not a method",
***************
*** 2332,2336 ****
  	      this_this = convert_to_nonzero_pointer (build_pointer_type (TREE_TYPE (of)), current_class_decl);
  	      return build_method_call (this_this, name, NULL_TREE,
! 					NULL_TREE, flags);
  	    }
  	  else if (current_class_decl)
--- 2332,2336 ----
  	      this_this = convert_to_nonzero_pointer (build_pointer_type (TREE_TYPE (of)), current_class_decl);
  	      return build_method_call (this_this, name, NULL_TREE,
! 					NULL_TREE, flags | LOOKUP_NONVIRTUAL);
  	    }
  	  else if (current_class_decl)
yahi% 

Michael