Skip to content

Commit

Permalink
fbc: typeini - allow implicit up-casting for NEW()
Browse files Browse the repository at this point in the history
- revert behaviour where NEW() disallowed up-casting
- allow up-casting for NEW() initializers
- for example, this is now - allowed again:

    type parent
        i as integer
    end type
    type child extends parent
        j as integer
    end type

    dim c as child

    '' implicit up-cast of c to parent
    dim p as parent ptr = new parent( c )
  • Loading branch information
jayrm committed Dec 27, 2022
1 parent 1438621 commit b2e1fe0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/compiler/parser-decl-symb-init.bas
Expand Up @@ -633,6 +633,7 @@ function cInitializer _
'' [x] top level -> allow up-casting
'' [x] nested UDT initialization -> do not allow up-casting
'' [ ] array element initialization -> allow up-casting
'' [x] NEW() -> allow up-casting

'' No override in options?
if( (options and FB_INIOPT_NOUPCAST) = 0 ) then
Expand All @@ -641,8 +642,10 @@ function cInitializer _
if( ctx.last_ctx = NULL ) then
ctx.options and= not FB_INIOPT_NOUPCAST

'' anything else, assume no upcasting
else
'' up-casting not explicitly enabled?
elseif( (options and FB_INIOPT_UPCAST) = 0 ) then

'' then anything else, assume no up-casting
ctx.options or= FB_INIOPT_NOUPCAST

end if
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser-quirk-mem.bas
Expand Up @@ -190,7 +190,7 @@ function cOperatorNew( ) as ASTNODE ptr
lexSkipToken( )
end if
else
initexpr = cInitializer( tmp, FB_INIOPT_ISINI or FB_INIOPT_NOUPCAST, dtype, subtype )
initexpr = cInitializer( tmp, FB_INIOPT_ISINI or FB_INIOPT_UPCAST, dtype, subtype )
if( initexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
end if
Expand Down
1 change: 1 addition & 0 deletions src/compiler/parser.bi
Expand Up @@ -227,6 +227,7 @@ enum FB_INIOPT
FB_INIOPT_ISINI = &h00000001 '' initializer (not an expression)
FB_INIOPT_ISOBJ = &h00000002 '' object with constructor
FB_INIOPT_NOUPCAST = &h00000004 '' don't allow upcasting (base types initialized from derived types)
FB_INIOPT_UPCAST = &h00000008 '' allow upcasting (as used by NEW() to reset the recursion checks)
end enum

'' cProcHeader() flags
Expand Down

0 comments on commit b2e1fe0

Please sign in to comment.