1# Copyright 2002-2023 Free Software Foundation, Inc. 2 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 3 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16# This file is part of the gdb testsuite 17 18# Test casting, especially between class types or pointer-to-class 19# types. 20 21# This file is part of the gdb testsuite 22 23# 24# test running programs 25# 26 27 28if { [skip_cplus_tests] } { return } 29 30standard_testfile .cc casts03.cc 31 32if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} { 33 return -1 34} 35 36if {![runto_main]} { 37 return 38} 39 40# Prevent symbol on address 0x0 being printed. 41gdb_test_no_output "set print symbol off" \ 42 "turn of symbol printing for the first time" 43 44gdb_test "break [gdb_get_line_number "casts.exp: 1"]" \ 45 "Breakpoint.*at.* file .*" \ 46 "" 47 48gdb_test "continue" "Breakpoint .* at .*casts.cc.*" "" 49 50# Casting a pointer to a base class to a pointer to a derived class 51# should yield the entire derived class. Until August 2002, GDB got 52# the enclosing type on `(B *) a' wrong: while the value's static type 53# was `B *', as it should be, the enclosing type (which is supposed to 54# be the dynamic type) was `A *'. It's senseless to have a static 55# type derived from the dynamic type; it should be the other way 56# 'round. Dereferencing this oddly typed pointer yielded a value in 57# which only the base class's members were initialized, since GDB uses 58# the enclosing type to decide how many bytes to read. Members from 59# the derived class were garbage, from GDB's address space. 60gdb_test "print * (B *) a" ".* = {<A> = {a = 42}, b = 1729}" \ 61 "cast base class pointer to derived class pointer" 62 63# Check also that we get the same results from letting the compiler do 64# the dereference. 65gdb_test "print * b" ".* = {<A> = {a = 42}, b = 1729}" \ 66 "let compiler cast base class pointer to derived class pointer" 67 68# Check upcasting (it is trivial but still). 69gdb_test "print * (A *) b" ".* = {a = 42}" \ 70 "cast derived class pointer to base class pointer" 71 72# Casting References. 73# Check upcasting. 74gdb_test "print (A &) br" ".* = .A &.* {a = 42}" \ 75 "cast derived class reference to base class reference" 76 77# Check downcasting. 78gdb_test "print (B &) ar" ".* = .B.* {<A> = {a = 42}, b = 1729}" \ 79 "cast base class reference to derived class reference" 80 81# Check compiler casting 82gdb_test "print br" ".* = .B.* {<A> = {a = 42}, b = 1729}" \ 83 "let compiler cast base class reference to derived class reference" 84 85 86# A few basic tests of "new" casts. 87 88gdb_test "print const_cast<const B *> (b)" " = \\(const B \\*\\) $hex" \ 89 "basic test of const_cast" 90 91gdb_test "print const_cast<void *> (0)" " = \\(void \\*\\) 0x0" \ 92 "const_cast of 0" 93 94gdb_test "print static_cast<A *> (b)" " = \\(A \\*\\) $hex" \ 95 "basic test of static_cast" 96 97gdb_test "print static_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \ 98 "static_cast to reference type" 99 100gdb_test "print reinterpret_cast<A *> (b)" " = \\(A \\*\\) $hex" \ 101 "basic test of reinterpret_cast" 102 103gdb_test "print reinterpret_cast<void> (b)" "Invalid reinterpret_cast" \ 104 "test invalid reinterpret_cast" 105 106gdb_test "print reinterpret_cast<A &> (*b)" " = \\(A \\&\\) @$hex: {a = 42}" \ 107 "reinterpret_cast to reference type" 108 109# Basic tests using typeof. 110 111foreach opname {__typeof__ __typeof __decltype} { 112 gdb_test "print (${opname}(a)) (b)" " = \\(A \\*\\) $hex" \ 113 "old-style cast using $opname" 114 115 gdb_test "print static_cast<${opname}(a)> (b)" " = \\(A \\*\\) $hex" \ 116 "static_cast using $opname" 117 118 gdb_test "print reinterpret_cast<${opname}(a)> (b)" " = \\(A \\*\\) $hex" \ 119 "reinterpret_cast using $opname" 120} 121 122gdb_test "whatis __decltype(*a)" "type = A \\&" 123 124# Tests of dynamic_cast. 125 126set nonzero_hex "0x\[0-9A-Fa-f\]\[0-9A-Fa-f\]+" 127 128gdb_test "print dynamic_cast<void> (a)" \ 129 ".*must be a pointer or reference type" \ 130 "invalid dynamic_cast" 131 132gdb_test "print dynamic_cast<void *> (0)" \ 133 " = \\(void \\*\\) 0x0" \ 134 "dynamic_cast of 0 to void*" 135 136gdb_test "print dynamic_cast<Alpha *> (&derived)" \ 137 " = \\(Alpha \\*\\) $nonzero_hex" \ 138 "dynamic_cast simple upcast" 139 140gdb_test "print dynamic_cast<Alpha *> (&doublyderived)" \ 141 " = \\(Alpha \\*\\) $nonzero_hex" \ 142 "dynamic_cast upcast to unique base" 143 144gdb_test "print dynamic_cast<Alpha &> (derived)" \ 145 " = \\(Alpha \\&\\) @$nonzero_hex: {.* = ${nonzero_hex}( <vtable for Derived.*>)?}" \ 146 "dynamic_cast simple upcast to reference" 147 148gdb_test "print dynamic_cast<Derived *> (ad)" \ 149 " = \\(Derived \\*\\) ${nonzero_hex}( <vtable for Derived.*>)?" \ 150 "dynamic_cast simple downcast" 151 152gdb_test "print dynamic_cast<VirtuallyDerived *> (add)" \ 153 " = \\(VirtuallyDerived \\*\\) $nonzero_hex" \ 154 "dynamic_cast simple downcast to intermediate class" 155 156gdb_test "print dynamic_cast<VirtuallyDerived *> (ad)" \ 157 " = \\(VirtuallyDerived \\*\\) 0x0" \ 158 "dynamic_cast to non-existing base" 159 160gdb_test "print dynamic_cast<VirtuallyDerived &> (*ad)" \ 161 "dynamic_cast failed" \ 162 "dynamic_cast to reference to non-existing base" 163 164gdb_test "print dynamic_cast<DoublyDerived *> (add)" \ 165 " = \\(DoublyDerived \\*\\) ${nonzero_hex}( <vtable for DoublyDerived.*>)?" \ 166 "dynamic_cast unique downcast" 167 168gdb_test "print dynamic_cast<Gamma *> (add)" \ 169 " = \\(Gamma \\*\\) $nonzero_hex" \ 170 "dynamic_cast to sibling" 171 172gdb_test "print (unsigned long long) &gd == gd_value" " = true" 173gdb_test "print (unsigned long long) (LeftRight *) (Right *) &gd == gd_value" \ 174 " = true" 175gdb_test "print (unsigned long long) (LeftRight *) (Right *) r_value == gd_value" \ 176 " = true" 177 178if {[prepare_for_testing "failed to prepare" ${testfile}03 $srcfile2 \ 179 {debug c++ additional_flags=-std=c++03}]} { 180 return -1 181} 182 183if {![runto_main]} { 184 return 185} 186 187# Prevent symbol on address 0x0 being printed. 188gdb_test_no_output "set print symbol off" \ 189 "turn of symbol printing for the second time" 190 191gdb_breakpoint [gdb_get_line_number "casts.exp: 1" $srcfile2] 192gdb_continue_to_breakpoint "end of casts03" 193 194# Test that keyword shadowing works. 195 196gdb_test "whatis decltype(5)" " = double" 197