Skip to content

Commit 9220036

Browse files
committed
udt-wstring: add TYPE EXTENDS Z|WSTRING [, udt]
- type can extend Z|WSTRING and another user defined type (pseudo multiple inheritance for z|wstring only) - extended types inherit Z|WSTRING if the base type also extended Z|WSTRING
1 parent 57360ed commit 9220036

File tree

5 files changed

+211
-33
lines changed

5 files changed

+211
-33
lines changed

src/compiler/parser-decl-struct.bas

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ private function hTypeAdd _
627627
byval isunion as integer, _
628628
byval align as integer, _
629629
byval baseDType as integer = 0, _
630-
byval baseSubtype as FBSYMBOL ptr = NULL _
630+
byval baseSubtype as FBSYMBOL ptr = NULL, _
631+
byval baseStringType as integer = 0 _
631632
) as FBSYMBOL ptr
632633

633634
dim as FBSYMBOL ptr s = any
@@ -639,12 +640,10 @@ private function hTypeAdd _
639640
s = symbStructBegin( NULL, NULL, parent, symbUniqueLabel( ), NULL, isunion, align, (baseSubtype <> NULL), 0, 0 )
640641
end if
641642

642-
select case baseDType
643+
select case baseStringType
643644
case FB_DATATYPE_CHAR
644-
assert( baseSubtype = NULL )
645645
symbSetUdtIsZstring( s )
646646
case FB_DATATYPE_WCHAR
647-
assert( baseSubtype = NULL )
648647
symbSetUdtIsWstring( s )
649648
end select
650649

@@ -1009,6 +1008,7 @@ sub cTypeDecl( byval attrib as integer )
10091008
'' (EXTENDS SymbolType)?
10101009
dim as FBSYMBOL ptr baseSubtype = NULL
10111010
dim as integer baseDType = 0
1011+
dim as integer stringType = 0
10121012

10131013
if( lexGetToken( ) = FB_TK_EXTENDS ) then
10141014
lexSkipToken( )
@@ -1022,15 +1022,61 @@ sub cTypeDecl( byval attrib as integer )
10221022
'' allow extending WSTRING and ZSTRING, the UDT
10231023
'' will use different rules for conversions,
10241024
if (baseDType = FB_DATATYPE_WCHAR) or (baseDType = FB_DATATYPE_CHAR) then
1025+
stringType = baseDType
1026+
baseDType = 0
10251027
assert( baseSubtype = NULL )
1026-
1028+
10271029
'' anything else? don't allow
10281030
else
10291031
errReport( FB_ERRMSG_EXPECTEDCLASSTYPE )
10301032
'' error recovery: skip
10311033
baseSubtype = NULL
10321034
end if
10331035
end if
1036+
1037+
'' got a string type? check for another base type
1038+
if( stringType <> 0 ) then
1039+
1040+
'' ','?
1041+
if( lexGetToken( ) = CHAR_COMMA ) then
1042+
lexSkipToken( )
1043+
1044+
'' SymbolType
1045+
hSymbolType( baseDType, baseSubtype, 0, FALSE, TRUE )
1046+
1047+
'' is the base type a struct?
1048+
if( baseDType <> FB_DATATYPE_STRUCT ) then
1049+
errReport( FB_ERRMSG_EXPECTEDCLASSTYPE )
1050+
'' error recovery: skip
1051+
baseSubtype = NULL
1052+
end if
1053+
end if
1054+
end if
1055+
1056+
'' base type? check if z|wstring was already extended
1057+
if( baseSubType ) then
1058+
select case stringType
1059+
case FB_DATATYPE_CHAR
1060+
'' can't extend zstring if inheriting from wstring
1061+
if( symbGetUdtIsWstring( baseSubtype ) ) then
1062+
errReport( FB_ERRMSG_INVALIDDATATYPES )
1063+
stringType = FB_DATATYPE_WCHAR
1064+
end if
1065+
case FB_DATATYPE_WCHAR
1066+
'' can't extend wstring if inheriting from zstring
1067+
if( symbGetUdtIsZstring( baseSubtype ) ) then
1068+
errReport( FB_ERRMSG_INVALIDDATATYPES )
1069+
stringType = FB_DATATYPE_CHAR
1070+
end if
1071+
case else
1072+
'' inherit from base type
1073+
if( symbGetUdtIsZstring( baseSubtype ) ) then
1074+
stringType = FB_DATATYPE_CHAR
1075+
elseif( symbGetUdtIsWstring( baseSubtype ) ) then
1076+
stringType = FB_DATATYPE_WCHAR
1077+
end if
1078+
end select
1079+
end if
10341080
end if
10351081

10361082
'' [FIELD '=' ConstExpression]
@@ -1049,7 +1095,7 @@ sub cTypeDecl( byval attrib as integer )
10491095
dim as FBSYMBOL ptr currprocsym = parser.currproc, currblocksym = parser.currblock
10501096
dim as integer scope_depth = parser.scope
10511097

1052-
sym = hTypeAdd( NULL, id, palias, isunion, align, baseDType, baseSubtype )
1098+
sym = hTypeAdd( NULL, id, palias, isunion, align, baseDType, baseSubtype, stringType )
10531099

10541100
'' restore the context
10551101
ast.proc.curr = currproc

tests/udt-wstring/uwstring-fixed.bas

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,78 @@
33

44
'' reference implementation of fixed length UDT wstring
55

6+
'' -------------------
7+
'' UWSTRING_FIXED_BASE
8+
'' -------------------
9+
610
''
7-
constructor UWSTRING_FIXED()
11+
constructor UWSTRING_FIXED_BASE()
812
_data = ""
913
end constructor
1014

1115
''
12-
constructor UWSTRING_FIXED( byval s as const wstring const ptr )
16+
constructor UWSTRING_FIXED_BASE( byval s as const wstring const ptr )
1317
_data = *s
1418
end constructor
1519

1620
''
17-
constructor UWSTRING_FIXED( byval s as const zstring const ptr )
21+
constructor UWSTRING_FIXED_BASE( byval s as const zstring const ptr )
1822
_data = wstr( *s )
1923
end constructor
2024

25+
const function UWSTRING_FIXED_BASE.length() as integer
26+
function = len( _data )
27+
end function
28+
29+
''
30+
operator Len( byref s as const UWSTRING_FIXED_BASE ) as integer
31+
return s.Length()
32+
end operator
33+
34+
35+
'' --------------
36+
'' UWSTRING_FIXED
37+
'' --------------
38+
39+
''
40+
constructor UWSTRING_FIXED()
41+
end constructor
42+
43+
''
44+
constructor UWSTRING_FIXED( byval s as const wstring const ptr )
45+
base( s )
46+
end constructor
47+
48+
''
49+
constructor UWSTRING_FIXED( byval s as const zstring const ptr )
50+
base( s )
51+
end constructor
52+
2153
''
2254
operator UWSTRING_FIXED.Cast() byref as const wstring
2355
operator = *cast(wstring ptr, @_data)
2456
end operator
2557

26-
const function UWSTRING_FIXED.length() as integer
27-
function = len( _data )
28-
end function
58+
59+
'' ----------------------
60+
'' UWSTRING_FIXED_MUTABLE
61+
'' ----------------------
2962

3063
''
31-
operator Len( byref s as const UWSTRING_FIXED ) as integer
32-
return s.Length()
64+
constructor UWSTRING_FIXED_MUTABLE()
65+
end constructor
66+
67+
''
68+
constructor UWSTRING_FIXED_MUTABLE( byval s as const wstring const ptr )
69+
base( s )
70+
end constructor
71+
72+
''
73+
constructor UWSTRING_FIXED_MUTABLE( byval s as const zstring const ptr )
74+
base( s )
75+
end constructor
76+
77+
''
78+
operator UWSTRING_FIXED_MUTABLE.Cast() byref as wstring
79+
operator = *cast(wstring ptr, @_data)
3380
end operator

tests/udt-wstring/uwstring-fixed.bi

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,37 @@
22

33
'' reference implementation of fixed length UDT wstring
44

5-
type UWSTRING_FIXED extends wstring
5+
type UWSTRING_FIXED_BASE extends wstring
66

7-
private:
7+
protected:
88
_data as wstring * 256
99

1010
public:
1111
declare constructor()
1212
declare constructor( byval rhs as const wstring const ptr )
1313
declare constructor( byval rhs as const zstring const ptr )
14-
declare operator cast() byref as const wstring
1514
declare const function length() as integer
1615

1716
end type
1817

19-
declare operator Len( byref s as const UWSTRING_FIXED ) as integer
18+
declare operator Len( byref s as const UWSTRING_FIXED_BASE ) as integer
19+
20+
type UWSTRING_FIXED extends UWSTRING_FIXED_BASE
21+
22+
public:
23+
declare constructor()
24+
declare constructor( byval rhs as const wstring const ptr )
25+
declare constructor( byval rhs as const zstring const ptr )
26+
27+
declare operator cast() byref as const wstring
28+
end type
29+
30+
type UWSTRING_FIXED_MUTABLE extends UWSTRING_FIXED_BASE
31+
32+
public:
33+
declare constructor()
34+
declare constructor( byval rhs as const wstring const ptr )
35+
declare constructor( byval rhs as const zstring const ptr )
36+
37+
declare operator cast() byref as wstring
38+
end type
Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,80 @@
11
#include "fbcunit.bi"
22
#include once "uzstring-fixed.bi"
33

4-
'' reference implementation of fixed length UDT wstring
4+
'' reference implementation of fixed length UDT zstring
5+
6+
'' -------------------
7+
'' UZSTRING_FIXED_BASE
8+
'' -------------------
59

610
''
7-
constructor UZSTRING_FIXED()
11+
constructor UZSTRING_FIXED_BASE()
812
_data = ""
913
end constructor
1014

1115
''
12-
constructor UZSTRING_FIXED( byval s as const zstring const ptr )
16+
constructor UZSTRING_FIXED_BASE( byval s as const zstring const ptr )
1317
_data = *s
1418
end constructor
1519

1620
''
17-
constructor UZSTRING_FIXED( byval s as const wstring const ptr )
21+
constructor UZSTRING_FIXED_BASE( byval s as const wstring const ptr )
1822
_data = str( *s )
1923
end constructor
2024

2125
''
22-
operator UZSTRING_FIXED.Cast() byref as const zstring
23-
operator = *cast(zstring ptr, @_data)
24-
end operator
25-
26-
const function UZSTRING_FIXED.length() as integer
26+
const function UZSTRING_FIXED_BASE.length() as integer
2727
function = len( _data )
2828
end function
2929

3030
''
31-
operator Len( byref s as const UZSTRING_FIXED ) as integer
31+
operator Len( byref s as const UZSTRING_FIXED_BASE ) as integer
3232
return s.Length()
3333
end operator
34+
35+
'' --------------
36+
'' UZSTRING_FIXED
37+
'' --------------
38+
39+
''
40+
constructor UZSTRING_FIXED()
41+
end constructor
42+
43+
''
44+
constructor UZSTRING_FIXED( byval s as const wstring const ptr )
45+
base( s )
46+
end constructor
47+
48+
''
49+
constructor UZSTRING_FIXED( byval s as const zstring const ptr )
50+
base( s )
51+
end constructor
52+
53+
''
54+
operator UZSTRING_FIXED.Cast() byref as const zstring
55+
operator = *cast(zstring ptr, @_data)
56+
end operator
57+
58+
59+
'' ----------------------
60+
'' UZSTRING_FIXED_MUTABLE
61+
'' ----------------------
62+
63+
''
64+
constructor UZSTRING_FIXED_MUTABLE()
65+
end constructor
66+
67+
''
68+
constructor UZSTRING_FIXED_MUTABLE( byval s as const wstring const ptr )
69+
base( s )
70+
end constructor
71+
72+
''
73+
constructor UZSTRING_FIXED_MUTABLE( byval s as const zstring const ptr )
74+
base( s )
75+
end constructor
76+
77+
''
78+
operator UZSTRING_FIXED_MUTABLE.Cast() byref as zstring
79+
operator = *cast(zstring ptr, @_data)
80+
end operator
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
#pragma once
22

3-
'' reference implementation of fixed length UDT wstring
3+
'' reference implementation of fixed length UDT zstring
44

5-
type UZSTRING_FIXED extends zstring
5+
type UZSTRING_FIXED_BASE extends zstring
66

7-
private:
7+
protected:
88
_data as zstring * 256
99

1010
public:
1111
declare constructor()
1212
declare constructor( byval rhs as const zstring const ptr )
1313
declare constructor( byval rhs as const wstring const ptr )
14-
declare operator cast() byref as const zstring
1514
declare const function length() as integer
1615

1716
end type
1817

19-
declare operator Len( byref s as const UZSTRING_FIXED ) as integer
18+
declare operator Len( byref s as const UZSTRING_FIXED_BASE ) as integer
19+
20+
type UZSTRING_FIXED extends UZSTRING_FIXED_BASE
21+
22+
public:
23+
declare constructor()
24+
declare constructor( byval rhs as const wstring const ptr )
25+
declare constructor( byval rhs as const zstring const ptr )
26+
27+
declare operator cast() byref as const zstring
28+
end type
29+
30+
type UZSTRING_FIXED_MUTABLE extends UZSTRING_FIXED_BASE
31+
32+
public:
33+
declare constructor()
34+
declare constructor( byval rhs as const wstring const ptr )
35+
declare constructor( byval rhs as const zstring const ptr )
36+
37+
declare operator cast() byref as zstring
38+
end type

0 commit comments

Comments
 (0)