Skip to content

Commit 6b5be9d

Browse files
committed
Allow casts between pointers and constant 0 with any integer type
These were already allowed: 0 => ptr 0l => ptr, but on 32bit only (matching the pointer size) 0ll => ptr, but on 64bit only (matching the pointer size) cbyte(0) => ptr cshort(0) => ptr Additionally, the following are now allowed too: 0l => ptr on 64bit 0ll => ptr on 32bit any 0 ptr => any integer type (opposite direction) Updating hCalcTypesDiff() (overload resolution) accordingly is not necessary, because it re-uses the code from ast-node-conv since db40150.
1 parent 94e13b6 commit 6b5be9d

File tree

4 files changed

+127
-33
lines changed

4 files changed

+127
-33
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Version 1.01.0
77
[added]
88
- On Linux/*BSD, the FB runtime will now respect the __fb_enable_vt100_escapes global variable (also see examples/console/disable-vt100-escapes.bas). FB programs can set it to 0 (FALSE) in order to manually disable the use of hard-coded VT100-specific escape sequences by FB's console I/O commands. This can help getting FB programs to run on older terminals which don't accept these escape sequences. However, it may also cause FB's console I/O commands and functions to behave differently. For example, if the use of the escape sequence for querying cursor position is disabled, then the FB runtime will assume that the cursor starts out at position 1,1 even if that is not actually true.
99
- WITH compounds now also accept type<UDT>(...) and UDT(...) expressions
10+
- Constant 0 integers can now be assigned to pointers no matter what integer type it is. Previously it didn't work with [U]LongInts on 32bit and [U]Longs on 64bit.
11+
- Constant 0 pointers can now be assigned to any integer type. Previously this was only allowed with integer types matching the pointer size (32bit or 64bit).
1012
- Bindings (new/updated, including 64bit support):
1113
Allegro 4.4.2 (allegro.bi)
1214
algif 1.3 (allegro/algif.bi)

src/compiler/ast-node-conv.bas

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,19 @@ function astCheckConvNonPtrToPtr _
115115
assert( typeIsPtr( to_dtype ) )
116116
assert( typeIsPtr( expr_dtype ) = FALSE )
117117

118-
select case as const( typeGetDtAndPtrOnly( expr_dtype ) )
119-
case FB_DATATYPE_INTEGER, FB_DATATYPE_UINT, FB_DATATYPE_ENUM, _
120-
FB_DATATYPE_LONG, FB_DATATYPE_ULONG, _
121-
FB_DATATYPE_LONGINT, FB_DATATYPE_ULONGINT
122-
'' Allow integer-to-pointer casts if same size
123-
if( typeGetSize( expr_dtype ) = env.pointersize ) then
124-
return FB_ERRMSG_OK
125-
end if
126-
127-
'' only allow other int dtypes if it's 0 (due QB's INTEGER = short)
128-
case FB_DATATYPE_BYTE, FB_DATATYPE_UBYTE, _
129-
FB_DATATYPE_SHORT, FB_DATATYPE_USHORT
118+
if( typeGetClass( expr_dtype ) = FB_DATACLASS_INTEGER ) then
119+
'' Allow converting literal 0 with any integer type to any pointer type
130120
if( astIsCONST( expr ) ) then
131121
if( astConstEqZero( expr ) ) then
132-
'' Allow 0-to-pointer casts
133122
return FB_ERRMSG_OK
134123
end if
135124
end if
136125

137-
end select
126+
'' Allow integer-to-pointer casts only if same size
127+
if( typeGetSize( expr_dtype ) = env.pointersize ) then
128+
return FB_ERRMSG_OK
129+
end if
130+
end if
138131

139132
function = hGetTypeMismatchErrMsg( options )
140133
end function
@@ -160,15 +153,19 @@ private function hCheckPtr _
160153

161154
'' from pointer? only allow integers of same size and pointers
162155
elseif( typeIsPtr( expr_dtype ) ) then
163-
select case as const( typeGetDtAndPtrOnly( to_dtype ) )
164-
case FB_DATATYPE_INTEGER, FB_DATATYPE_UINT, FB_DATATYPE_ENUM, _
165-
FB_DATATYPE_LONG, FB_DATATYPE_ULONG, _
166-
FB_DATATYPE_LONGINT, FB_DATATYPE_ULONGINT
156+
if( typeGetClass( to_dtype ) = FB_DATACLASS_INTEGER ) then
157+
'' Allow converting literal 0 with any pointer type to any integer type
158+
if( astIsCONST( expr ) ) then
159+
if( astConstEqZero( expr ) ) then
160+
exit function
161+
end if
162+
end if
163+
167164
'' Allow pointer-to-integer casts if same size
168165
if( typeGetSize( to_dtype ) = env.pointersize ) then
169166
exit function
170167
end if
171-
end select
168+
end if
172169

173170
return hGetTypeMismatchErrMsg( options )
174171
else

tests/numbers/cast-nullptr.bas

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# include "fbcu.bi"
2+
3+
namespace fbc_tests.numbers.cast_nullptr
4+
5+
private sub f_b ( byval i as byte ) : end sub
6+
private sub f_sh ( byval i as short ) : end sub
7+
private sub f_l ( byval i as long ) : end sub
8+
private sub f_ll ( byval i as longint ) : end sub
9+
private sub f_i ( byval i as integer ) : end sub
10+
private sub f_ub ( byval i as ubyte ) : end sub
11+
private sub f_ush ( byval i as ushort ) : end sub
12+
private sub f_ul ( byval i as ulong ) : end sub
13+
private sub f_ull ( byval i as ulongint ) : end sub
14+
private sub f_ui ( byval i as uinteger ) : end sub
15+
16+
sub test cdecl( )
17+
dim p as any ptr
18+
p = 0l
19+
p = 0ul
20+
p = 0ll
21+
p = 0ull
22+
p = 0
23+
p = 0u
24+
p = cbyte ( 0 )
25+
p = cshort ( 0 )
26+
p = clng ( 0 )
27+
p = clngint ( 0 )
28+
p = cint ( 0 )
29+
p = cubyte ( 0 )
30+
p = cushort ( 0 )
31+
p = culng ( 0 )
32+
p = culngint( 0 )
33+
p = cuint ( 0 )
34+
p = cptr( integer ptr, 0 )
35+
p = cptr( any ptr, 0 )
36+
37+
dim b as byte
38+
dim sh as short
39+
dim l as long
40+
dim ll as longint
41+
dim i as integer
42+
dim ub as ubyte
43+
dim ush as ushort
44+
dim ul as ulong
45+
dim ull as ulongint
46+
dim ui as uinteger
47+
48+
b = cptr( any ptr, 0 )
49+
sh = cptr( any ptr, 0 )
50+
l = cptr( any ptr, 0 )
51+
ll = cptr( any ptr, 0 )
52+
i = cptr( any ptr, 0 )
53+
ub = cptr( any ptr, 0 )
54+
ush = cptr( any ptr, 0 )
55+
ul = cptr( any ptr, 0 )
56+
ull = cptr( any ptr, 0 )
57+
ui = cptr( any ptr, 0 )
58+
59+
b = cptr( integer ptr, 0 )
60+
sh = cptr( integer ptr, 0 )
61+
l = cptr( integer ptr, 0 )
62+
ll = cptr( integer ptr, 0 )
63+
i = cptr( integer ptr, 0 )
64+
ub = cptr( integer ptr, 0 )
65+
ush = cptr( integer ptr, 0 )
66+
ul = cptr( integer ptr, 0 )
67+
ull = cptr( integer ptr, 0 )
68+
ui = cptr( integer ptr, 0 )
69+
70+
f_b ( cptr( any ptr, 0 ) )
71+
f_sh ( cptr( any ptr, 0 ) )
72+
f_l ( cptr( any ptr, 0 ) )
73+
f_ll ( cptr( any ptr, 0 ) )
74+
f_i ( cptr( any ptr, 0 ) )
75+
f_ub ( cptr( any ptr, 0 ) )
76+
f_ush( cptr( any ptr, 0 ) )
77+
f_ul ( cptr( any ptr, 0 ) )
78+
f_ull( cptr( any ptr, 0 ) )
79+
f_ui ( cptr( any ptr, 0 ) )
80+
81+
f_b ( cptr( integer ptr, 0 ) )
82+
f_sh ( cptr( integer ptr, 0 ) )
83+
f_l ( cptr( integer ptr, 0 ) )
84+
f_ll ( cptr( integer ptr, 0 ) )
85+
f_i ( cptr( integer ptr, 0 ) )
86+
f_ub ( cptr( integer ptr, 0 ) )
87+
f_ush( cptr( integer ptr, 0 ) )
88+
f_ul ( cptr( integer ptr, 0 ) )
89+
f_ull( cptr( integer ptr, 0 ) )
90+
f_ui ( cptr( integer ptr, 0 ) )
91+
end sub
92+
93+
private sub ctor( ) constructor
94+
fbcu.add_suite( "tests/numbers/cast-nullptr" )
95+
fbcu.add_test( "test", @test )
96+
end sub
97+
98+
end namespace

tests/overload/pointers.bas

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -814,26 +814,23 @@ namespace const0
814814
makeFunction1( ui, uinteger )
815815

816816
private sub test cdecl( )
817-
a( cptr( any ptr, 0 ) )
817+
a( 0l )
818+
a( 0ul )
819+
a( 0ll )
820+
a( 0ull )
818821
a( 0 )
819822
a( 0u )
820-
a( cint( 0 ) )
821-
a( cuint( 0 ) )
822-
#ifdef __FB_64BIT__
823-
a( 0ll )
824-
a( 0ull )
825-
a( clngint( 0 ) )
826-
a( culngint( 0 ) )
827-
#else
828-
a( 0l )
829-
a( 0ul )
830-
a( clng( 0 ) )
831-
a( culng( 0 ) )
832-
#endif
833823
a( cbyte( 0 ) )
834824
a( cubyte( 0 ) )
835825
a( cshort( 0 ) )
836826
a( cushort( 0 ) )
827+
a( clng( 0 ) )
828+
a( culng( 0 ) )
829+
a( clngint( 0 ) )
830+
a( culngint( 0 ) )
831+
a( cint( 0 ) )
832+
a( cuint( 0 ) )
833+
a( cptr( any ptr, 0 ) )
837834

838835
#macro makeTest1( suffix, result )
839836
CU_ASSERT( f1_##suffix( cptr( any ptr, 0 ) ) = "any ptr" )

0 commit comments

Comments
 (0)