# Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
# 2006, 2007, 2008 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file was written by Fred Fish. (fnf@cygnus.com)
# And rewritten by Michael Chastain <mec.gnu@mindspring.com>.

set nl		"\[\r\n\]+"

if $tracelevel then {
    strace $tracelevel
}

if { [skip_cplus_tests] } { continue }

load_lib "cp-support.exp"

set testfile "virtfunc"
set srcfile ${testfile}.cc
set binfile ${objdir}/${subdir}/${testfile}

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {c++ debug}] != "" } {
     untested virtfunc.exp
     return -1
}

proc test_ptype_of_classes {} {

    # class VA

    cp_test_ptype_class \
	"ptype VA" "" "class" "VA" \
	{
	    { field public "int va;" }
	}

    # class VB

    cp_test_ptype_class \
	"ptype VB" "" "class" "VB" \
	{
	    { field  public "int vb;" }
	    { method public "int fvb();" }
	    { method public "virtual int vvb();" }
	}

    # class V

    cp_test_ptype_class \
	"ptype V" "" "class" "V" \
	{
	    { base          "public VA" }
	    { base          "public VB" }
	    { field  public "int w;" }
	    { method public "int f();" }
	    { method public "virtual int vv();" }
	}

    # class A

    cp_test_ptype_class \
	"ptype A" "" "class" "A" \
	{
	    { base           "public virtual V" }
	    { vbase          "V" }
	    { field  private "int a;" }
	    { method public  "virtual int f();" }
	}

    # class B

    cp_test_ptype_class \
	"ptype B" "" "class" "B" \
	{
	    { base           "public A" }
	    { field  private "int b;" }
	    { method public  "virtual int f();" }
	}

    # class C

    cp_test_ptype_class \
	"ptype C" "" "class" "C" \
	{
	    { base         "public virtual V" }
	    { vbase        "V" }
	    { field public "int c;" }
	}

    # class AD

    cp_test_ptype_class \
	"ptype AD" "" "class" "AD" \
	{
	    { method public "virtual int vg();" }
	}

    # class D

    cp_test_ptype_class \
	"ptype D" "" "class" "D" \
	{
	    { base          "public AD" }
	    { base          "public virtual V" }
	    { vbase         "V" }
	    { method public "static void s();" }
	    { method public "virtual int vg();" }
	    { method public "virtual int vd();" }
	    { method public "int fd();" }
	    { field  public "int d;" }
	}

    # class E

    cp_test_ptype_class \
	"ptype E" "" "class" "E" \
	{
	    { base          "public B" }
	    { base          "public virtual V" }
	    { base          "public D" }
	    { base          "public C" }
	    { vbase         "V" }
	    { method public "virtual int f();" }
	    { method public "virtual int vg();" }
	    { method public "virtual int vv();" }
	    { field  public "int e;" }
	}

    # An instance of D

    cp_test_ptype_class "ptype dd" "" "class" "D" ibid

    # An instance of D *

    cp_test_ptype_class "ptype ppd" "" "class" "D" ibid "*"

    # An instance of AD *
    # TODO: this should be named pADd, not pAd.

    cp_test_ptype_class "ptype pAd" "" "class" "AD" ibid "*"

    # Instances of these classes.

    cp_test_ptype_class "ptype a" "" "class" "A" ibid
    cp_test_ptype_class "ptype b" "" "class" "B" ibid
    cp_test_ptype_class "ptype c" "" "class" "C" ibid
    cp_test_ptype_class "ptype d" "" "class" "D" ibid
    cp_test_ptype_class "ptype e" "" "class" "E" ibid
    cp_test_ptype_class "ptype v" "" "class" "V" ibid
    cp_test_ptype_class "ptype vb" "" "class" "VB" ibid

    # Instances of pointers to these classes.

    cp_test_ptype_class "ptype pAa" "" "class" "A" ibid "*"
    cp_test_ptype_class "ptype pAe" "" "class" "A" ibid "*"
    cp_test_ptype_class "ptype pBe" "" "class" "B" ibid "*"
    cp_test_ptype_class "ptype pDd" "" "class" "D" ibid "*"
    cp_test_ptype_class "ptype pDe" "" "class" "D" ibid "*"
    cp_test_ptype_class "ptype pVa" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVv" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVe" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pVd" "" "class" "V" ibid "*"
    cp_test_ptype_class "ptype pADe" "" "class" "AD" ibid "*"
    cp_test_ptype_class "ptype pEe" "" "class" "E" ibid "*"
    cp_test_ptype_class "ptype pVB" "" "class" "VB" ibid "*"

}

# Call virtual functions.

proc test_virtual_calls {} {
    global gdb_prompt
    global nl

    if [target_info exists gdb,cannot_call_functions] {
	setup_xfail "*-*-*" 2416
	fail "This target can not call functions"
	return 0
    }

    gdb_test "print pAe->f()"   "\\$\[0-9\]+ = 20"
    gdb_test "print pAa->f()"   "\\$\[0-9\]+ = 1"
    gdb_test "print pDe->vg()"  "\\$\[0-9\]+ = 202"
    gdb_test "print pADe->vg()" "\\$\[0-9\]+ = 202"
    gdb_test "print pDd->vg()"  "\\$\[0-9\]+ = 101"
    gdb_test "print pEe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pVB->vvb()" "\\$\[0-9\]+ = 407"
    gdb_test "print pBe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pDe->vvb()" "\\$\[0-9\]+ = 411"
    gdb_test "print pEe->vd()"  "\\$\[0-9\]+ = 282"
    gdb_test "print pEe->fvb()" "\\$\[0-9\]+ = 311"

    # more recent results:
    # wrong value "202"
    #   gcc 2.95.3 -gdwarf-2
    #   gcc 2.95.3 -gstabs+
    # attempt to take addres of value not located in memory
    #   gcc 3.3.2 -gdwarf-2
    #   gcc 3.3.2 -gstabs+
    #
    # -- chastain 2003-12-31

    gdb_test_multiple "print pEe->D::vg()" "print pEe->D::vg()" {
	-re "\\$\[0-9]+ = 102$nl$gdb_prompt $" {
	    pass "print pEe->D::vg()"
	}
	-re "\\$\[0-9]+ = 202$nl$gdb_prompt $" {
	    # To get this result, we have called pEe->*(&D::vg) ().
	    # That's how GDB interprets this, but it's wrong; in fact
	    # the explicit D:: means to bypass virtual function lookup,
	    # and call D::vg as if it were non-virtual.  We still have
	    # to e.g. adjust "this", though.
	    kfail "gdb/1064" "print pEe->D::vg()"
	}
	-re "Attempt to take address of value not located in memory.$nl$gdb_prompt $" {
	    kfail "gdb/1064" "print pEe->D::vg()"
	}
    }
}

proc do_tests {} {
    global prms_id
    global bug_id
    global srcdir subdir binfile
    global gdb_prompt

    set prms_id 0
    set bug_id 0

    gdb_exit
    gdb_start
    gdb_reinitialize_dir $srcdir/$subdir
    gdb_load $binfile

    gdb_test "set language c++" "" ""
    gdb_test "set width 0" "" ""

    runto_main
    test_ptype_of_classes

    gdb_breakpoint test_calls
    gdb_test "continue" ".*Breakpoint .* test_calls.*" ""
    test_virtual_calls

    gdb_test "next" ".*pAa->f.*" "next to pAa->f call"
    gdb_test "next" ".*pDe->vg.*" "next to pDe->vg call"
    gdb_test "step" ".*E::vg.*" "step through thunk into E::vg"
}

do_tests
