# Copyright 1992-2019 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 { [skip_cplus_tests] } { continue }

load_lib "cp-support.exp"

standard_testfile .cc

if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
    return -1
}

proc test_ptype_of_classes {} {

    # class VA

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

    # class VB

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

    # class V

    cp_test_ptype_class \
	"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 \
	"A" "" "class" "A" \
	{
	    { base           "public virtual V" }
	    { vbase          "V" }
	    { field  private "int a;" }
	    { method public  "virtual int f();" }
	}

    # class B

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

    # class C

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

    # class AD

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

    # class D

    cp_test_ptype_class \
	"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 \
	"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 "dd" "" "class" "D" ibid

    # An instance of D *

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

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

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

    # Instances of these classes.

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

    # Instances of pointers to these classes.

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

}

# Call virtual functions.

proc test_virtual_calls {} {
    global gdb_prompt
    global nl

    if [target_info exists gdb,cannot_call_functions] {
	unsupported "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()"
	}
    }
}

# A helper proc that creates a regular expression matching a
# particular vtable.  NAME is the type name.  Each element of ARGS is
# the name of a function in the vtable.

proc make_one_vtable_result {name args} {
    global hex

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

    set result "vtable for '${name}' @ $hex .subobject @ $hex.:$nls"
    set count 0
    foreach func $args {
	append result ".${count}.:( @$hex:)? $hex <(\.)?$func..>${nls}"
	incr count
    }

    return $result
}

# Test "info vtbl".

proc test_info_vtbl {} {
    global hex

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

    set vt_A [make_one_vtable_result A A::f]
    set vt_B [make_one_vtable_result B B::f]
    set vt_V [make_one_vtable_result V VB::vvb V::vv]
    set vt_V2 [make_one_vtable_result V VB::vvb "virtual thunk to E::vv"]
    set vt_D [make_one_vtable_result D D::vg D::vd]
    set vt_D2 [make_one_vtable_result D "non-virtual thunk to E::vg" D::vd]
    set vt_E [make_one_vtable_result E E::f E::vg E::vv]

    gdb_test "info vtbl a" "${vt_A}${vt_V}"
    gdb_test "info vtbl b" "${vt_B}${vt_V}"
    gdb_test "info vtbl c" "${vt_V}"
    gdb_test "info vtbl d" "${vt_D}${vt_V}"
    gdb_test "info vtbl e" "${vt_E}${vt_D2}${vt_V2}"
    gdb_test "info vtbl pEe" "${vt_E}${vt_D2}${vt_V2}"

    gdb_test "info vtbl" "Argument required.*"
    gdb_test "info vtbl va" \
	"This object does not have a virtual function table.*"
    gdb_test "info vtbl all_count" \
	"This object does not have a virtual function table.*"
}

proc do_tests {} {
    gdb_test_no_output "set language c++" ""
    gdb_test_no_output "set width 0" ""

    if ![runto_main] then {
	perror "couldn't run to breakpoint"
	return
    }
    test_ptype_of_classes
    test_info_vtbl

    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