Changeset 0c327ce for libcfa


Ignore:
Timestamp:
Jul 12, 2024, 3:30:06 PM (5 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
76b507d
Parents:
9c447e2
Message:
  1. Add bound check to Serial function: now compiler generates the unchecked functions in ImplementEnumFunc?, and enum.hfa implements the bound check on top. Todo: Wrapped the checked version into a trait; 2. countof is now works on any type that implement Countof(). Because Countof() is implemented in enum.hfa for all CfaEnum?, we can call countof on { T | CfaEnum?(T) }
Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/enum.cfa

    r9c447e2 r0c327ce  
    55
    66#pragma GCC visibility push(default)
     7
     8forall( E | Serial( E ) ) {
     9    E fromInt( unsigned i ) {
     10        E upper = upperBound();
     11        E lower = lowerBound();
     12        // It is okay to overflow as overflow will be theoretically caught by the other bound
     13        assert( i <= fromInstance(upper) && i >= fromInstance(lower)
     14            &&  "Not a valid value");
     15        return fromInt_unsafe( i );
     16    }
     17
     18    E succ( E e ) {
     19        E upper = upperBound();
     20        assert( (fromInstance(e) < fromInstance(upper))
     21            && "Calling succ() on the last" );
     22        return succ_unsafe(e);
     23    }
     24
     25    E pred( E e ) {
     26        E lower = lowerBound();
     27        assert( (fromInstance(e) > fromInstance(lower))
     28            && "Calling pred() on the first" );
     29        return pred_unsafe(e);
     30    }
     31
     32    int Countof( __attribute__((unused)) E e ) {
     33        E upper = upperBound();
     34        E lower = lowerBound();
     35                return fromInstance( upper ) + fromInstance( lower ) + 1;
     36    }
     37}
    738
    839int scmp( const void * str1, const void * str2 ) {
  • libcfa/src/enum.hfa

    r9c447e2 r0c327ce  
    11#pragma once
    22
     3#include <assert.h>
    34#include "iostream.hfa"
    45
     
    1011forall( E | Bounded( E ) ) trait Serial {
    1112    unsigned fromInstance( E e );
     13    E fromInt_unsafe( unsigned i );
     14    E succ_unsafe( E e );
     15    E pred_unsafe( E e );
     16};
     17
     18forall( E | Serial( E ) ) {
    1219    E fromInt( unsigned i );
    1320    E succ( E e );
    1421    E pred( E e );
    15 };
     22    int Countof( E e );
     23}
     24
     25// forall( E | Bounded(E) ) trait SafeSerial {
     26//     // unsigned fromInstance( E e );
     27//     E fromInt_unsafe( unsigned i );
     28//     // E succ_unsafe( E e );
     29//     //E pred_unsafe( E e );
     30
     31//     unsigned fromInstance( E e );
     32//     E fromInt( unsigned i );
     33//     E succ( E e );
     34//     E pred( E e );
     35// };
    1636
    1737forall( E | Serial( E ) ) trait CfaEnum {
     
    3353        OSTYPE_VOID( E );
    3454}
    35 
    36 // forall( ostype & | ostream( ostype ), E | CfaEnum( E, quasi_void ) ) {
    37 //      ostype & ?|?( ostype &, E );
    38 //      OSTYPE_VOID( E );
    39 // }
    40 
    41 // Design two <- should go for this if we have change the cost model
    42 // forall( E | Serial( E ) ) trait CfaEnum {
    43 //     char * label( E e );
    44 //     unsigned int posn( E e );
    45 // };
    46 
    47 // forall( E, V| CfaEnum( E)) trait TypedEnum {
    48 //     V value( E e);
    49 // };
    5055
    5156static inline
Note: See TracChangeset for help on using the changeset viewer.