"Fossies" - the Fresh Open Source Software Archive

Member "FreeBASIC-1.07.1-source/tests/warnings/const-discard.bas" (27 Sep 2019, 27489 Bytes) of package /linux/privat/FreeBASIC-1.07.1-source.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Visual Basic source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "const-discard.bas": 1.07.0_vs_1.07.1.

    1 '' CONST qualifier discarded warnings
    2 
    3 '' this test needs constness warnings turned on
    4 #pragma constness=true
    5 
    6 #define ENABLE_SHOW_EXPECTED_ERRORS 0
    7 
    8 '' set to 1 to see errors, compile with -maxerr 1000 
    9 '' to see all errors
   10 
   11 
   12 /'
   13 To check first time run, use this helper tool:
   14     $ cd ./generator
   15     $ fbc -w constness ../const-discard.bas -c > const-discard.log 
   16     $ fbc chk-warning-log.bas
   17     $ ./chk-warning-log const-discard.log > chk-const-discard.log
   18 
   19     Line numbers reported in ./chk-const-discard.log refer to line 
   20     numbers in const-discard.log which can then be referenced to line 
   21     numbers in ../const-discard.bas
   22 
   23     If no differences found, ./chk-const-discard.log is empty file.
   24 
   25 '/
   26 
   27 '' !!! FIXME !!!: if we already get a warning on different
   28 '' pointer types, warning on CONSTness also is probably
   29 '' meaningless.
   30 
   31 '' --------------------------------------------------------
   32 
   33 #macro WARN_AND_ERROR( W, E )
   34     #if (W>0) and (E>0)
   35         #print W Warning(s), E Error(s)
   36     #elseif (W>0)
   37         #print W Warning(s)
   38     #elseif (E>0)
   39         #print E Error(s)
   40     #else
   41         #print "No Warnings"
   42     #endif
   43 #endmacro
   44 
   45 #macro WARN( W )
   46     #if (W=0)
   47         #print none expected
   48     #elseif (W=1)
   49         #print warning expected
   50     #elseif (W=2)
   51         #print 2 warnings expected
   52     #else
   53         #print argument must be 0 or 1 or 2
   54         #error
   55     #endif
   56 #endmacro
   57 
   58 '' --------------------------------------------------------
   59 
   60 #print "---- INTERNAL CONVERSIONS"
   61 
   62 '' internal init should not give warning
   63 
   64 WARN( 0 )
   65 
   66 type T_modlevel
   67     __ as string
   68 end type
   69 
   70 '' --------------------------------------------------------
   71 
   72 '' internal init should not give warning
   73 
   74 scope
   75     WARN( 0 )
   76     dim s as const string = ""
   77 end scope
   78 
   79 '' --------------------------------------------------------
   80 
   81 '' internal init should not give warning
   82 
   83 WARN( 0 )
   84 
   85 type RTTI1 extends object
   86 end type
   87 
   88 type RTTI2 extends RTTI1
   89 end type
   90 
   91 '' ----------------
   92 
   93 '' internal init should not give warning
   94 
   95 WARN( 0 )
   96 
   97 type OBJECT1 extends OBJECT
   98    static as const integer y
   99    static as integer z
  100 end type
  101 
  102 '' ----------------
  103 
  104 '' internal casts to [const] zstring const ptr ptr
  105 '' should not give warning
  106 
  107 WARN( 0 )
  108 
  109 scope
  110     dim i as integer
  111     dim k as const integer = 1
  112     scope
  113         dim s as string
  114         #print 5 UBYTE PTR to follow
  115         #print typeof( @s[0] )
  116         #print typeof( @s[1] )
  117         #print typeof( @s[i] )
  118         #print typeof( @s[k] )
  119         #print typeof( @s[k+i] )
  120     end scope
  121 
  122     scope
  123         dim s as const string = ""
  124         dim i as integer
  125         dim k as const integer = 1
  126         #print 5 CONST UBYTE PTR to follow
  127         #print typeof( @s[0] )
  128         #print typeof( @s[1] )
  129         #print typeof( @s[i] )
  130         #print typeof( @s[k] )
  131         #print typeof( @s[k+i] )
  132     end scope
  133 end scope
  134 
  135 '' ----------------
  136 
  137 #print "---- RTLIB"
  138 
  139 '' from sf.net #642
  140 
  141 scope
  142 
  143     dim as const integer a = 256
  144 
  145     WARN( 1 )
  146     *cast( integer ptr, @a ) = 257
  147 
  148     WARN( 1 )
  149     poke integer, @a, 257
  150 
  151 end scope
  152 
  153 '' --------------------------------------------------------
  154 
  155 #print "---- LOCAL CONST INTEGER"
  156 
  157 scope
  158     WARN( 0 )
  159     dim x as const integer = 1
  160 
  161     scope
  162         WARN( 0 )
  163         dim p as const integer ptr = @x
  164         p = @x
  165     end scope
  166 
  167     scope
  168         WARN( 1 )
  169         dim p as const integer ptr = cast( integer ptr, @x )
  170         WARN( 1 )
  171         p = cast( integer ptr, @x )
  172     end scope
  173 
  174     scope
  175         WARN( 0 )
  176         dim p as const integer ptr = cast( const integer ptr, @x )
  177         WARN( 0 )
  178         p = cast( const integer ptr, @x )
  179     end scope
  180 
  181     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  182     scope
  183         WARN_AND_ERROR( 1, 2 )
  184         dim p as integer ptr = @x
  185         p = @x
  186     end scope
  187     #endif
  188 
  189     scope
  190         WARN( 1 )
  191         dim p as integer ptr = cast( integer ptr, @x )
  192         WARN( 1 )
  193         p = cast( integer ptr, @x )
  194     end scope
  195 
  196     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  197     scope
  198         WARN_AND_ERROR( 1, 2 )
  199         dim p as integer ptr = cast( const integer ptr, @x )
  200         p = cast( const integer ptr, @x )
  201     end scope
  202     #endif
  203 
  204 end scope
  205 
  206 '' --------------------------------------------------------
  207 
  208 #print "---- LOCAL INTEGER"
  209 
  210 scope
  211     WARN( 0 )
  212     dim x as integer = 1
  213 
  214     scope
  215         WARN( 0 )
  216         dim p as const integer ptr = @x
  217         p = @x
  218     end scope
  219 
  220     scope
  221         WARN( 0 )
  222         dim p as const integer ptr = cast( integer ptr, @x )
  223         p = cast( integer ptr, @x )
  224     end scope
  225 
  226     scope
  227         WARN( 1 )
  228         dim p as const integer ptr = cast( const integer ptr, @x )
  229         WARN( 1 )
  230         p = cast( const integer ptr, @x )
  231     end scope
  232 
  233     scope
  234         WARN( 0 )
  235         dim p as integer ptr = @x
  236         p = @x
  237     end scope
  238 
  239     scope
  240         WARN( 0 )
  241         dim p as integer ptr = cast( integer ptr, @x )
  242         p = cast( integer ptr, @x )
  243     end scope
  244 
  245     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  246     scope
  247         WARN_AND_ERROR( 2, 3 )
  248         dim p as integer ptr = cast( const integer ptr, @x )
  249         p = cast( const integer ptr, @x )
  250     end scope
  251     #endif
  252 
  253 end scope
  254 
  255 '' --------------------------------------------------------
  256 
  257 '' internal array expressions should not give warning
  258 '' only access to the data type represented
  259 
  260 #print "---- LOCAL CONST INTEGER ARRAY"
  261 
  262 scope
  263     WARN( 0 )
  264     dim x(0 to 2) as const integer = { 1, 2, 3 }
  265     dim i as const integer = 1
  266 
  267     scope
  268         WARN( 0 )
  269         dim p0 as const integer ptr = @x(0)
  270         dim p1 as const integer ptr = @x(1)
  271         dim p2 as const integer ptr = @x(i)
  272         dim p3 as const integer ptr = @x(1+i)
  273         p0 = @x(0)
  274         p1 = @x(1)
  275         p2 = @x(i)
  276         p3 = @x(1+i)
  277     end scope
  278 
  279     scope
  280         WARN( 1 )
  281         dim p0 as const integer ptr = cast( integer ptr, @x(0) )
  282         WARN( 1 )
  283         dim p1 as const integer ptr = cast( integer ptr, @x(1) )
  284         WARN( 1 )
  285         dim p2 as const integer ptr = cast( integer ptr, @x(i) )
  286         WARN( 1 )
  287         dim p3 as const integer ptr = cast( integer ptr, @x(1+i) )
  288         WARN( 1 )
  289         p0 = cast( integer ptr, @x(0) )
  290         WARN( 1 )
  291         p1 = cast( integer ptr, @x(1) )
  292         WARN( 1 )
  293         p2 = cast( integer ptr, @x(i) )
  294         WARN( 1 )
  295         p3 = cast( integer ptr, @x(1+i) )
  296     end scope
  297 
  298     scope
  299         WARN( 0 )
  300         dim p0 as const integer ptr = cast( const integer ptr, @x(0) )
  301         dim p1 as const integer ptr = cast( const integer ptr, @x(1) )
  302         dim p3 as const integer ptr = cast( const integer ptr, @x(i) )
  303         dim p2 as const integer ptr = cast( const integer ptr, @x(1+i) )
  304         p0 = cast( const integer ptr, @x(0) )
  305         p1 = cast( const integer ptr, @x(1) )
  306         p2 = cast( const integer ptr, @x(i) )
  307         p3 = cast( const integer ptr, @x(1+i) )
  308     end scope
  309 
  310     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  311     scope
  312         WARN_AND_ERROR( 3, 6 )
  313         dim p0 as integer ptr = @x(0)
  314         dim p1 as integer ptr = @x(1)
  315         dim p2 as integer ptr = @x(i)
  316         dim p3 as integer ptr = @x(1+i)
  317         p0 = @x(0)
  318         p1 = @x(1)
  319         p2 = @x(i)
  320         p3 = @x(1+i)
  321     end scope
  322     #endif
  323 
  324     scope
  325         WARN( 1 )
  326         dim p0 as integer ptr = cast( integer ptr, @x(0) )
  327         WARN( 1 )
  328         dim p1 as integer ptr = cast( integer ptr, @x(1) )
  329         WARN( 1 )
  330         dim p2 as integer ptr = cast( integer ptr, @x(i) )
  331         WARN( 1 )
  332         dim p3 as integer ptr = cast( integer ptr, @x(1+i) )
  333         WARN( 1 )
  334         p0 = cast( integer ptr, @x(0) )
  335         WARN( 1 )
  336         p1 = cast( integer ptr, @x(1) )
  337         WARN( 1 )
  338         p2 = cast( integer ptr, @x(i) )
  339         WARN( 1 )
  340         p2 = cast( integer ptr, @x(1+i) )
  341     end scope
  342 
  343     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  344     scope
  345         WARN_AND_ERROR( 3, 6 )
  346         dim p0 as integer ptr = cast( const integer ptr, @x(0) )
  347         dim p1 as integer ptr = cast( const integer ptr, @x(1) )
  348         dim p2 as integer ptr = cast( const integer ptr, @x(i) )
  349         dim p3 as integer ptr = cast( const integer ptr, @x(1+i) )
  350         p0 = cast( const integer ptr, @x(0) )
  351         p1 = cast( const integer ptr, @x(1) )
  352         p2 = cast( const integer ptr, @x(i) )
  353         p3 = cast( const integer ptr, @x(1+i) )
  354     end scope
  355     #endif
  356 
  357 end scope
  358 
  359 '' --------------------------------------------------------
  360 
  361 '' internal array expressions should not give warning
  362 '' only access to the data type represented
  363 
  364 #print "---- LOCAL INTEGER ARRAY"
  365 
  366 scope
  367     WARN( 0 )
  368     dim x(0 to 2) as integer = { 1, 2, 3 }
  369     dim i as const integer = 1
  370 
  371     scope
  372         WARN( 0 )
  373         dim p0 as const integer ptr = @x(0)
  374         dim p1 as const integer ptr = @x(1)
  375         dim p2 as const integer ptr = @x(i)
  376         dim p3 as const integer ptr = @x(1+i)
  377         p0 = @x(0)
  378         p1 = @x(1)
  379         p2 = @x(i)
  380         p3 = @x(1+i)
  381     end scope
  382 
  383     scope
  384         WARN( 0 )
  385         dim p0 as const integer ptr = cast( integer ptr, @x(0) )
  386         WARN( 0 )
  387         dim p1 as const integer ptr = cast( integer ptr, @x(1) )
  388         WARN( 0 )
  389         dim p2 as const integer ptr = cast( integer ptr, @x(i) )
  390         WARN( 0 )
  391         dim p3 as const integer ptr = cast( integer ptr, @x(1+i) )
  392         WARN( 0 )
  393         p0 = cast( integer ptr, @x(0) )
  394         WARN( 0 )
  395         p1 = cast( integer ptr, @x(1) )
  396         WARN( 0 )
  397         p2 = cast( integer ptr, @x(i) )
  398         WARN( 0 )
  399         p3 = cast( integer ptr, @x(1+i) )
  400     end scope
  401 
  402     scope
  403         WARN( 1 )
  404         dim p0 as const integer ptr = cast( const integer ptr, @x(0) )
  405         WARN( 1 )
  406         dim p1 as const integer ptr = cast( const integer ptr, @x(1) )
  407         WARN( 1 )
  408         dim p2 as const integer ptr = cast( const integer ptr, @x(i) )
  409         WARN( 1 )
  410         dim p3 as const integer ptr = cast( const integer ptr, @x(1+i) )
  411         WARN( 1 )
  412         p0 = cast( const integer ptr, @x(0) )
  413         WARN( 1 )
  414         p1 = cast( const integer ptr, @x(i) )
  415         WARN( 1 )
  416         p2 = cast( const integer ptr, @x(1+i) )
  417     end scope
  418 
  419     scope
  420         WARN( 0 )
  421         dim p0 as integer ptr = @x(0)
  422         dim p1 as integer ptr = @x(1)
  423         dim p2 as integer ptr = @x(i)
  424         dim p3 as integer ptr = @x(1+i)
  425         p0 = @x(0)
  426         p1 = @x(1)
  427         p2 = @x(i)
  428         p3 = @x(1+i)
  429     end scope
  430 
  431     scope
  432         WARN( 0 )
  433         dim p0 as integer ptr = cast( integer ptr, @x(0) )
  434         dim p1 as integer ptr = cast( integer ptr, @x(1) )
  435         dim p2 as integer ptr = cast( integer ptr, @x(i) )
  436         dim p3 as integer ptr = cast( integer ptr, @x(1+i) )
  437         p0 = cast( integer ptr, @x(0) )
  438         p1 = cast( integer ptr, @x(1) )
  439         p2 = cast( integer ptr, @x(i) )
  440         p3 = cast( integer ptr, @x(1+i) )
  441     end scope
  442 
  443     #if (ENABLE_SHOW_EXPECTED_ERRORS<>0)
  444     scope
  445         WARN_AND_ERROR( 9, 6 )
  446         dim p0 as integer ptr = cast( const integer ptr, @x(0) )
  447         dim p1 as integer ptr = cast( const integer ptr, @x(1) )
  448         dim p2 as integer ptr = cast( const integer ptr, @x(i) )
  449         dim p3 as integer ptr = cast( const integer ptr, @x(1+i) )
  450         p0 = cast( const integer ptr, @x(0) )
  451         p1 = cast( const integer ptr, @x(1) )
  452         p2 = cast( const integer ptr, @x(i) )
  453         p3 = cast( const integer ptr, @x(1+i) )
  454     end scope
  455     #endif
  456 
  457 end scope
  458 
  459 '' --------------------------------------------------------
  460 
  461 '' from sf.net #642
  462 
  463 #print "---- LOCAL INTEGER and BYTE PTR"
  464 
  465 scope
  466     scope
  467         dim x as const integer = 1
  468         dim p as const integer ptr = @x
  469         WARN( 1 )
  470         *cast(byte ptr, p) = 1
  471     end scope
  472 
  473     scope
  474         dim x as const integer = 1
  475         WARN( 1 )
  476         dim p as const integer ptr = cast( const byte ptr, @x )
  477     end scope
  478 
  479     scope
  480         dim x as const integer = 1
  481         WARN( 0 )
  482         dim p as const byte ptr = cast( const byte ptr, @x )
  483     end scope
  484 
  485     scope
  486         dim x as const integer = 1
  487         WARN( 1 )
  488         dim p as byte ptr = cast( byte ptr, @x )
  489     end scope
  490 
  491     scope
  492         dim x as const integer = 1
  493         WARN( 0 )
  494         dim p as const integer ptr = @x
  495         WARN( 2 )
  496         p = cast( byte ptr, @x )
  497     end scope
  498 end scope
  499 
  500 '' --------------------------------------------------------
  501 
  502 #print "---- MULTIPLE CAST"
  503 
  504     scope
  505         dim i as const integer = 123
  506         dim p as integer ptr
  507         WARN( 2 )
  508         p = cast( integer ptr, cast( const integer ptr, cast( integer, cast( integer ptr, @i ) ) ) )
  509     end scope
  510 
  511 '' --------------------------------------------------------
  512 
  513 '' from PR#90 discussion
  514 
  515 #print "---- PROCEDURE POINTERS"
  516 
  517 sub sub_const(byref i as const integer)
  518     print i
  519 end sub
  520 
  521 sub sub_noconst(byref i as integer)
  522     i = 123
  523     print i
  524 end sub
  525 
  526 scope
  527     type type_const as sub(byref as const integer)
  528     type type_noconst as sub(byref as integer)
  529 
  530     dim ptr_const as type_const
  531     dim ptr_noconst as type_noconst
  532 
  533     #print ASSIGNMENT
  534 
  535     WARN( 0 )
  536     ptr_const   = @sub_const   '' safe, same type
  537     WARN( 2 ) 
  538     ptr_const   = @sub_noconst '' unsafe, because when calling through the ptr the param appears const, but the sub actually modifies it
  539     WARN( 0 )
  540     ptr_noconst = @sub_const   '' safe, ptr allows more than the sub will do
  541     WARN( 0 )
  542     ptr_noconst = @sub_noconst '' safe, same type
  543 
  544     #print EXPRESSION
  545 
  546     WARN( 0 )
  547     print cptr(sub(byref as const integer), @sub_const) '' safe
  548     WARN( 1 ) 
  549     print cptr(sub(byref as const integer), @sub_noconst) '' unsafe, warn
  550     WARN( 0 )
  551     print cptr(sub(byref as integer), @sub_const) '' safe
  552     WARN( 0 )
  553     print cptr(sub(byref as integer), @sub_noconst) '' safe
  554 end scope
  555 
  556 '' from tests/warnings/ptr-const-param.bas
  557 
  558 declare sub byref__i__  ( byval as sub( byref as       integer           ) )
  559 declare sub byref_ci__  ( byval as sub( byref as const integer           ) )
  560 declare sub byref__i_p  ( byval as sub( byref as       integer       ptr ) )
  561 declare sub byref__icp  ( byval as sub( byref as       integer const ptr ) )
  562 declare sub byref_ci_p  ( byval as sub( byref as const integer       ptr ) )
  563 declare sub byref_cicp  ( byval as sub( byref as const integer const ptr ) )
  564 
  565 scope
  566     #print BYREF PARAMETER
  567 
  568     dim _i__ as sub( byref as       integer           )
  569     dim ci__ as sub( byref as const integer           )
  570     dim _i_p as sub( byref as       integer       ptr )
  571     dim _icp as sub( byref as       integer const ptr )
  572     dim ci_p as sub( byref as const integer       ptr )
  573     dim cicp as sub( byref as const integer const ptr )
  574 
  575     WARN( 0 )
  576     byref__i__( _i__ ) '' safe, same type
  577     WARN( 2 )
  578     byref_ci__( _i__ ) '' unsafe, ptr can do more than param allows
  579     WARN( 1 )
  580     byref__i_p( _i__ ) '' unsafe, different types
  581     WARN( 2 )
  582     byref__icp( _i__ ) '' unsafe, different types
  583     WARN( 1 )
  584     byref_ci_p( _i__ ) '' unsafe, different types
  585     WARN( 2 )
  586     byref_cicp( _i__ ) '' unsafe, different types
  587 
  588     WARN( 0 )
  589     byref__i__( ci__ ) '' safe, param allows more than ptr will do
  590     WARN( 0 )
  591     byref_ci__( ci__ ) '' safe, same type
  592     WARN( 1 )
  593     byref__i_p( ci__ ) '' unsafe, different types
  594     WARN( 1 )
  595     byref__icp( ci__ ) '' unsafe, different types
  596     WARN( 1 )
  597     byref_ci_p( ci__ ) '' unsafe, different types
  598     WARN( 1 )
  599     byref_cicp( ci__ ) '' unsafe, different types
  600 
  601     WARN( 1 )
  602     byref__i__( _i_p ) '' unsafe, different types
  603     WARN( 2 )
  604     byref_ci__( _i_p ) '' unsafe, different types
  605     WARN( 0 )
  606     byref__i_p( _i_p ) '' safe, same type
  607     WARN( 2 )
  608     byref__icp( _i_p ) '' unsafe, ptr can do more than param allows
  609     WARN( 2 )
  610     byref_ci_p( _i_p ) '' unsafe, ptr can do more than param allows
  611     WARN( 2 )
  612     byref_cicp( _i_p ) '' unsafe, ptr can do more than param allows
  613 
  614     WARN( 1 )
  615     byref__i__( _icp ) '' unsafe, different types
  616     WARN( 1 )
  617     byref_ci__( _icp ) '' unsafe, different types
  618     WARN( 0 )
  619     byref__i_p( _icp ) '' safe, param allows more then ptr will do
  620     WARN( 0 )
  621     byref__icp( _icp ) '' safe, same type
  622     WARN( 2 )
  623     byref_ci_p( _icp ) '' unsafe, different types
  624     WARN( 2 )
  625     byref_cicp( _icp ) '' unsafe, ptr can do more than param allows
  626 
  627     WARN( 1 )
  628     byref__i__( ci_p ) '' unsafe, different types
  629     WARN( 2 )
  630     byref_ci__( ci_p ) '' unsafe, different types
  631     WARN( 0 )
  632     byref__i_p( ci_p ) '' safe, param allows more then ptr will do
  633     WARN( 2 )
  634     byref__icp( ci_p ) '' unsafe, different types
  635     WARN( 0 )
  636     byref_ci_p( ci_p ) '' safe, same type
  637     WARN( 2 )
  638     byref_cicp( ci_p ) '' unsafe, ptr can do more than param allows
  639 
  640     WARN( 1 )
  641     byref__i__( cicp ) '' unsafe, different types
  642     WARN( 1 )
  643     byref_ci__( cicp ) '' unsafe, different types
  644     WARN( 0 )
  645     byref__i_p( cicp ) '' safe, param allows more then ptr will do
  646     WARN( 0 )
  647     byref__icp( cicp ) '' safe, param allows more then ptr will do
  648     WARN( 0 )
  649     byref_ci_p( cicp ) '' safe, param allows more then ptr will do
  650     WARN( 0 )
  651     byref_cicp( cicp ) '' safe, same type
  652 
  653 end scope
  654 
  655 '' from tests/warnings/ptr-const-param.bas
  656 
  657 declare sub byval__i__  ( byval as sub( byval as       integer           ) )
  658 declare sub byval_ci__  ( byval as sub( byval as const integer           ) )
  659 declare sub byval__i_p  ( byval as sub( byval as       integer       ptr ) )
  660 declare sub byval__icp  ( byval as sub( byval as       integer const ptr ) )
  661 declare sub byval_ci_p  ( byval as sub( byval as const integer       ptr ) )
  662 declare sub byval_cicp  ( byval as sub( byval as const integer const ptr ) )
  663 
  664 scope
  665     #print BYVAL PARAMETER
  666 
  667     dim _i__ as sub( byval as       integer           )
  668     dim ci__ as sub( byval as const integer           )
  669     dim _i_p as sub( byval as       integer       ptr )
  670     dim _icp as sub( byval as       integer const ptr )
  671     dim ci_p as sub( byval as const integer       ptr )
  672     dim cicp as sub( byval as const integer const ptr )
  673 
  674     WARN( 0 )
  675     byval__i__( _i__ ) '' safe, same type
  676     WARN( 0 )
  677     byval_ci__( _i__ ) '' safe, byval makes copy
  678     WARN( 1 )
  679     byval__i_p( _i__ ) '' unsafe, different types
  680     WARN( 1 )
  681     byval__icp( _i__ ) '' unsafe, different types
  682     WARN( 1 )
  683     byval_ci_p( _i__ ) '' unsafe, different types
  684     WARN( 1 )
  685     byval_cicp( _i__ ) '' unsafe, different types
  686 
  687     WARN( 0 )
  688     byval__i__( ci__ ) '' safe, param allows more than ptr will do
  689     WARN( 0 )
  690     byval_ci__( ci__ ) '' safe, same type
  691     WARN( 1 )
  692     byval__i_p( ci__ ) '' unsafe, different types
  693     WARN( 1 )
  694     byval__icp( ci__ ) '' unsafe, different types
  695     WARN( 1 )
  696     byval_ci_p( ci__ ) '' unsafe, different types
  697     WARN( 1 )
  698     byval_cicp( ci__ ) '' unsafe, different types
  699 
  700     WARN( 1 )
  701     byval__i__( _i_p ) '' unsafe, different types
  702     WARN( 1 )
  703     byval_ci__( _i_p ) '' unsafe, different types
  704     WARN( 0 )
  705     byval__i_p( _i_p ) '' safe, same type
  706     WARN( 0 )
  707     byval__icp( _i_p ) '' safe, byval makes copy
  708     WARN( 2 )
  709     byval_ci_p( _i_p ) '' unsafe, ptr can do more than param allows
  710     WARN( 2 )
  711     byval_cicp( _i_p ) '' unsafe, ptr can do more than param allows
  712 
  713     WARN( 1 )
  714     byval__i__( _icp ) '' unsafe, different types
  715     WARN( 1 )
  716     byval_ci__( _icp ) '' unsafe, different types
  717     WARN( 0 )
  718     byval__i_p( _icp ) '' safe, param allows more then ptr will do
  719     WARN( 0 )
  720     byval__icp( _icp ) '' safe, same type
  721     WARN( 2 )
  722     byval_ci_p( _icp ) '' unsafe, different types
  723     WARN( 2 )
  724     byval_cicp( _icp ) '' unsafe, ptr can do more than param allows
  725 
  726     WARN( 1 )
  727     byval__i__( ci_p ) '' unsafe, different types
  728     WARN( 1 )
  729     byval_ci__( ci_p ) '' unsafe, different types
  730     WARN( 0 )
  731     byval__i_p( ci_p ) '' safe, param allows more then ptr will do
  732     WARN( 0 )
  733     byval__icp( ci_p ) '' safe, byval makes copy
  734     WARN( 0 )
  735     byval_ci_p( ci_p ) '' safe, same type
  736     WARN( 0 )
  737     byval_cicp( ci_p ) '' safe, byval makes copy
  738 
  739     WARN( 1 )
  740     byval__i__( cicp ) '' unsafe, different types
  741     WARN( 1 )
  742     byval_ci__( cicp ) '' unsafe, different types
  743     WARN( 0 )
  744     byval__i_p( cicp ) '' safe, param allows more then ptr will do
  745     WARN( 0 )
  746     byval__icp( cicp ) '' safe, param allows more then ptr will do
  747     WARN( 0 )
  748     byval_ci_p( cicp ) '' safe, param allows more then ptr will do
  749     WARN( 0 )
  750     byval_cicp( cicp ) '' safe, same type
  751 
  752 end scope
  753 
  754 '' from tests/warnings/ptr-const-param.bas
  755 
  756 declare sub sub_byref__i__  ( byref as       integer           )
  757 declare sub sub_byref_ci__  ( byref as const integer           )
  758 declare sub sub_byref__i_p  ( byref as       integer       ptr )
  759 declare sub sub_byref__icp  ( byref as       integer const ptr )
  760 declare sub sub_byref_ci_p  ( byref as const integer       ptr )
  761 declare sub sub_byref_cicp  ( byref as const integer const ptr )
  762 
  763 scope
  764     #print BYREF ASSIGNMENT
  765 
  766     dim _i__ as sub( byref as       integer           )
  767     dim ci__ as sub( byref as const integer           )
  768     dim _i_p as sub( byref as       integer       ptr )
  769     dim _icp as sub( byref as       integer const ptr )
  770     dim ci_p as sub( byref as const integer       ptr )
  771     dim cicp as sub( byref as const integer const ptr )
  772 
  773     WARN( 0 )
  774     _i__ = @sub_byref__i__ '' safe, same type
  775     WARN( 2 )
  776     ci__ = @sub_byref__i__ '' unsafe, ptr can do more than param allows
  777     WARN( 1 )
  778     _i_p = @sub_byref__i__ '' unsafe, different types
  779     WARN( 2 )
  780     _icp = @sub_byref__i__ '' unsafe, different types
  781     WARN( 1 )
  782     ci_p = @sub_byref__i__ '' unsafe, different types
  783     WARN( 2 )
  784     cicp = @sub_byref__i__ '' unsafe, different types
  785 
  786     WARN( 0 )
  787     _i__ = @sub_byref_ci__ '' safe, param allows more than ptr will do
  788     WARN( 0 )
  789     ci__ = @sub_byref_ci__ '' safe, same type
  790     WARN( 1 )
  791     _i_p = @sub_byref_ci__ '' unsafe, different types
  792     WARN( 1 )
  793     _icp = @sub_byref_ci__ '' unsafe, different types
  794     WARN( 1 )
  795     ci_p = @sub_byref_ci__ '' unsafe, different types
  796     WARN( 1 )
  797     cicp = @sub_byref_ci__ '' unsafe, different types
  798 
  799     WARN( 1 )
  800     _i__ = @sub_byref__i_p '' unsafe, different types
  801     WARN( 2 )
  802     ci__ = @sub_byref__i_p '' unsafe, different types
  803     WARN( 0 )
  804     _i_p = @sub_byref__i_p '' safe, same type
  805     WARN( 2 )
  806     _icp = @sub_byref__i_p '' unsafe, ptr can do more than param allows
  807     WARN( 2 )
  808     ci_p = @sub_byref__i_p '' unsafe, ptr can do more than param allows
  809     WARN( 2 )
  810     cicp = @sub_byref__i_p '' unsafe, ptr can do more than param allows
  811 
  812     WARN( 1 )
  813     _i__ = @sub_byref__icp '' unsafe, different types
  814     WARN( 1 )
  815     ci__ = @sub_byref__icp '' unsafe, different types
  816     WARN( 0 )
  817     _i_p = @sub_byref__icp '' safe, param allows more then ptr will do
  818     WARN( 0 )
  819     _icp = @sub_byref__icp '' safe, same type
  820     WARN( 2 )
  821     ci_p = @sub_byref__icp '' unsafe, different types
  822     WARN( 2 )
  823     cicp = @sub_byref__icp '' unsafe, ptr can do more than param allows
  824 
  825     WARN( 1 )
  826     _i__ = @sub_byref_ci_p '' unsafe, different types
  827     WARN( 2 )
  828     ci__ = @sub_byref_ci_p '' unsafe, different types
  829     WARN( 0 )
  830     _i_p = @sub_byref_ci_p '' safe, param allows more then ptr will do
  831     WARN( 2 )
  832     _icp = @sub_byref_ci_p '' unsafe, different types
  833     WARN( 0 )
  834     ci_p = @sub_byref_ci_p '' safe, same type
  835     WARN( 2 )
  836     cicp = @sub_byref_ci_p '' unsafe, ptr can do more than param allows
  837 
  838     WARN( 1 )
  839     _i__ = @sub_byref_cicp '' unsafe, different types
  840     WARN( 1 )
  841     ci__ = @sub_byref_cicp '' unsafe, different types
  842     WARN( 0 )
  843     _i_p = @sub_byref_cicp '' safe, param allows more then ptr will do
  844     WARN( 0 )
  845     _icp = @sub_byref_cicp '' safe, param allows more then ptr will do
  846     WARN( 0 )
  847     ci_p = @sub_byref_cicp '' safe, param allows more then ptr will do
  848     WARN( 0 )
  849     cicp = @sub_byref_cicp '' safe, same type
  850 
  851 end scope
  852 
  853 '' from tests/warnings/ptr-const-param.bas
  854 
  855 declare sub sub_byval__i__  ( byval as       integer           )
  856 declare sub sub_byval_ci__  ( byval as const integer           )
  857 declare sub sub_byval__i_p  ( byval as       integer       ptr )
  858 declare sub sub_byval__icp  ( byval as       integer const ptr )
  859 declare sub sub_byval_ci_p  ( byval as const integer       ptr )
  860 declare sub sub_byval_cicp  ( byval as const integer const ptr )
  861 
  862 scope
  863     #print BYVAL ASSIGNMENT
  864 
  865     dim _i__ as sub( byval as       integer           )
  866     dim ci__ as sub( byval as const integer           )
  867     dim _i_p as sub( byval as       integer       ptr )
  868     dim _icp as sub( byval as       integer const ptr )
  869     dim ci_p as sub( byval as const integer       ptr )
  870     dim cicp as sub( byval as const integer const ptr )
  871 
  872     WARN( 0 )
  873     _i__ = @sub_byval__i__ '' safe, same type
  874     WARN( 0 )
  875     ci__ = @sub_byval__i__ '' safe, byval makes copy
  876     WARN( 1 )
  877     _i_p = @sub_byval__i__ '' unsafe, different types
  878     WARN( 1 )
  879     _icp = @sub_byval__i__ '' unsafe, different types
  880     WARN( 1 )
  881     ci_p = @sub_byval__i__ '' unsafe, different types
  882     WARN( 1 )
  883     cicp = @sub_byval__i__ '' unsafe, different types
  884 
  885     WARN( 0 )
  886     _i__ = @sub_byval_ci__ '' safe, param allows more than ptr will do
  887     WARN( 0 )
  888     ci__ = @sub_byval_ci__ '' safe, same type
  889     WARN( 1 )
  890     _i_p = @sub_byval_ci__ '' unsafe, different types
  891     WARN( 1 )
  892     _icp = @sub_byval_ci__ '' unsafe, different types
  893     WARN( 1 )
  894     ci_p = @sub_byval_ci__ '' unsafe, different types
  895     WARN( 1 )
  896     cicp = @sub_byval_ci__ '' unsafe, different types
  897 
  898     WARN( 1 )
  899     _i__ = @sub_byval__i_p '' unsafe, different types
  900     WARN( 1 )
  901     ci__ = @sub_byval__i_p '' unsafe, different types
  902     WARN( 0 )
  903     _i_p = @sub_byval__i_p '' safe, same type
  904     WARN( 0 )
  905     _icp = @sub_byval__i_p '' safe, byval makes copy
  906     WARN( 2 )
  907     ci_p = @sub_byval__i_p '' unsafe, ptr can do more than param allows
  908     WARN( 2 )
  909     cicp = @sub_byval__i_p '' unsafe, ptr can do more than param allows
  910 
  911     WARN( 1 )
  912     _i__ = @sub_byval__icp '' unsafe, different types
  913     WARN( 1 )
  914     ci__ = @sub_byval__icp '' unsafe, different types
  915     WARN( 0 )
  916     _i_p = @sub_byval__icp '' safe, param allows more then ptr will do
  917     WARN( 0 )
  918     _icp = @sub_byval__icp '' safe, same type
  919     WARN( 2 )
  920     ci_p = @sub_byval__icp '' unsafe, different types
  921     WARN( 2 )
  922     cicp = @sub_byval__icp '' unsafe, ptr can do more than param allows
  923 
  924     WARN( 1 )
  925     _i__ = @sub_byval_ci_p '' unsafe, different types
  926     WARN( 1 )
  927     ci__ = @sub_byval_ci_p '' unsafe, different types
  928     WARN( 0 )
  929     _i_p = @sub_byval_ci_p '' safe, param allows more then ptr will do
  930     WARN( 0 )
  931     _icp = @sub_byval_ci_p '' safe, byval makes copy
  932     WARN( 0 )
  933     ci_p = @sub_byval_ci_p '' safe, same type
  934     WARN( 0 )
  935     cicp = @sub_byval_ci_p '' safe, byval makes copy
  936 
  937     WARN( 1 )
  938     _i__ = @sub_byval_cicp '' unsafe, different types
  939     WARN( 1 )
  940     ci__ = @sub_byval_cicp '' unsafe, different types
  941     WARN( 0 )
  942     _i_p = @sub_byval_cicp '' safe, param allows more then ptr will do
  943     WARN( 0 )
  944     _icp = @sub_byval_cicp '' safe, param allows more then ptr will do
  945     WARN( 0 )
  946     ci_p = @sub_byval_cicp '' safe, param allows more then ptr will do
  947     WARN( 0 )
  948     cicp = @sub_byval_cicp '' safe, same type
  949 
  950 end scope
  951 
  952 '' --------------------------------------------------------
  953 
  954 #print "---- RTLIB DEALLOCATE"
  955 
  956 scope
  957     type T
  958         zc as const zstring ptr
  959         z as zstring ptr
  960         ic as const integer ptr
  961         i as integer ptr
  962     end type
  963 
  964     dim x as T
  965 
  966     '' should not get warnings when allocating/deallocating
  967     '' a const {datatype} ptr.  We only need to guarantee
  968     '' that the data is not modified when accessed though
  969     '' the const type.  We make no guarantees about the pointer
  970     '' itself; which includes deallocation.
  971 
  972     '' no warnings expected
  973 
  974     WARN( 0 )
  975     x.zc = callocate( 10 )
  976     deallocate( x.zc )
  977 
  978     WARN( 0 )
  979     x.z = callocate( 10 )
  980     deallocate( x.z )
  981 
  982     WARN( 0 )
  983     x.ic = callocate( 10 )
  984     deallocate( x.ic )
  985 
  986     WARN( 0 )
  987     x.i = callocate( 10 )
  988     deallocate( x.i )
  989 
  990 end scope
  991 
  992 '' --------------------------------------------------------
  993 
  994 #print "---- CRT memcpy"
  995 
  996 declare function memcpy (byval as any ptr, byval as const any ptr, byval as integer ) as any ptr
  997 
  998 scope
  999     dim x as byte ptr
 1000     
 1001     dim xc as const byte ptr = allocate(10)
 1002     dim i as integer
 1003 
 1004     '' safe, should not warn
 1005     WARN(0)
 1006     memcpy( x, x, 5 )
 1007 
 1008     WARN(0)
 1009     memcpy( x, xc, 5 )
 1010 
 1011     WARN(0)
 1012     memcpy( x + i, x, 5 )
 1013 
 1014     WARN(0)
 1015     memcpy( x + i, xc, 5 )
 1016 end scope
 1017 
 1018 '' --------------------------------------------------------
 1019 
 1020 '' from https://www.freebasic.net/forum/viewtopic.php?p=254288#p254288
 1021 
 1022 #print "---- Array in Const UDT"
 1023 
 1024 type point
 1025     x as single
 1026     y as single
 1027 end type
 1028 
 1029 type shape
 1030     points(any) as point
 1031 end type
 1032 
 1033 sub const_udt_array( byref s as const shape )
 1034     WARN(0)
 1035     print lbound(s.points)
 1036     WARN(0)
 1037     print ubound(s.points)
 1038     WARN(0)
 1039     print @s.points(0)
 1040     WARN(0)
 1041     print s.points(0).x
 1042 end sub
 1043 
 1044 '' --------------------------------------------------------
 1045 
 1046 '' from https://www.freebasic.net/forum/viewtopic.php?f=17&t=27692
 1047 '' and  https://sourceforge.net/p/fbc/bugs/910/
 1048 
 1049 #print "---- Regression Checks"
 1050 
 1051 sub const_cast_string1( byval s as string )
 1052 end sub 
 1053 sub const_cast_string2( byref s as const string )
 1054     WARN(1)
 1055     const_cast_string1( cast(string, s) )
 1056 end sub
 1057 
 1058 scope
 1059     dim x as const string = "test"
 1060     dim y as string
 1061     WARN(1)
 1062     y = cast(string, x )
 1063     WARN(0)
 1064     y = cast(const string, x )
 1065 end scope
 1066 
 1067 type T_integer
 1068     __ as integer
 1069 end type
 1070 
 1071 sub const_cast_proc1i( byval i as integer ptr )
 1072 end sub 
 1073 sub const_cast_proc2i( byval i as const integer ptr )
 1074 end sub
 1075 
 1076 sub const_cast_proc1t( byval x as T_integer ptr )
 1077 end sub 
 1078 sub const_cast_proc2t( byval x as const T_integer ptr )
 1079 end sub 
 1080 
 1081 scope
 1082     dim i as const integer = 1
 1083     WARN(1)
 1084     const_cast_proc1i( @cast(integer, i) )
 1085     WARN(0)
 1086     const_cast_proc2i( @cast(const integer, i) )
 1087 end scope
 1088 
 1089 scope
 1090     dim i as integer = 1
 1091     WARN(1)
 1092     const_cast_proc2i( @cast(const integer, i) )
 1093     WARN(0)
 1094     const_cast_proc2i( @cast(integer, i) )
 1095 end scope
 1096 
 1097 scope
 1098     dim x as const T_integer = ( 1 )
 1099     WARN(1)
 1100     const_cast_proc1t( @cast(T_integer, x) )
 1101     WARN(0)
 1102     const_cast_proc2t( @cast(const T_integer, x) )
 1103 end scope
 1104 
 1105 scope
 1106     dim x as T_integer = ( 1 )
 1107     WARN(1)
 1108     const_cast_proc2t( @cast(const T_integer,x) )
 1109     WARN(0)
 1110     const_cast_proc2t( @cast(T_integer,x) )
 1111 end scope