Skip to content

Commit

Permalink
kernel: introduce BOOL, TRUE, FALSE
Browse files Browse the repository at this point in the history
This adds `typedef Int BOOL`; the resulting C type `BOOL` can be used to
indicate that the return type of a function is supposed to be a boolean.
Matching integer constants TRUE=1 and FALSE=0 are also added.

The reason we are not using type `bool` from stdbool.h and the constants
`true` and `false`, all available in C99, is the following: in C99, a pointer
is automatically converted to type bool if necessary. That means that if one
by accident writes `return False` instead of `return false` in a function with
return type `BOOL`, then no compiler warning is issued. Instead, a bug is
introduced, as `False` is a non-NULL pointer, and thus is cast to the `bool`
value `true`.

With this commit, we get warnings similar to the following if we mix up
TRUE/True or FALSE/False; while if using `bool`, we'd only get the latter two
warnings, but not the first one.

    src/objects.c:897:12: error: incompatible pointer to integer conversion
          returning 'Obj' (aka 'unsigned long **') from a function with result
          type 'BOOL' (aka 'unsigned char') [-Werror,-Wint-conversion]
        return False;
               ^~~~~

    src/objects.c:1185:39: error: pointer/integer type mismatch in conditional
          expression ('int' and 'Obj' (aka 'unsigned long **'))
          [-Werror,-Wconditional-type-mismatch]
        return (TNUM_OBJ(obj) == T_COMOBJ ? TRUE : False);
                                          ^ ~~~~   ~~~~~

    src/objects.c:1337:16: error: incompatible integer to pointer conversion
          returning 'int' from a function with result type 'Obj' (aka
          'unsigned long **') [-Werror,-Wint-conversion]
            return TRUE;
                   ^~~~

Note that on the long run, it would be better to switch to something like
`typedef char BOOL`, but that breaks at least one package (NormalizInterface),
so let's postpone that until all affected packages with kernel extensions have
switched to using BOOL.
  • Loading branch information
fingolfin committed Jan 10, 2020
1 parent 26bf15a commit 10d082e
Show file tree
Hide file tree
Showing 64 changed files with 379 additions and 389 deletions.
30 changes: 15 additions & 15 deletions src/blister.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static Int LenBlist(Obj list)
**
** 'IsbBlist' is the function in 'IsbListFuncs' for boolean lists.
*/
static Int IsbBlist(Obj list, Int pos)
static BOOL IsbBlist(Obj list, Int pos)
{
return (pos <= LEN_BLIST(list));
}
Expand Down Expand Up @@ -756,7 +756,7 @@ static void PlainBlist(Obj list)
** 'IsPossBlist' returns 1 if <list> is empty, and 0 otherwise, since a
** boolean list is a positions list if and only if it is empty.
*/
static Int IsPossBlist(Obj list)
static BOOL IsPossBlist(Obj list)
{
return LEN_BLIST(list) == 0;
}
Expand All @@ -766,7 +766,7 @@ static Int IsPossBlist(Obj list)
**
*F IsHomogBlist( <list> ) . . . . . . . . . . check if <list> is homogenous
*/
static Int IsHomogBlist(Obj list)
static BOOL IsHomogBlist(Obj list)
{
return (0 < LEN_BLIST(list));
}
Expand All @@ -776,18 +776,18 @@ static Int IsHomogBlist(Obj list)
**
*F IsSSortBlist( <list> ) . . . . . . . check if <list> is strictly sorted
*/
static Int IsSSortBlist(Obj list)
static BOOL IsSSortBlist(Obj list)
{
Int isSort;
BOOL isSort;

if ( LEN_BLIST(list) <= 1 ) {
isSort = 1;
isSort = TRUE;
}
else if ( LEN_BLIST(list) == 2 ) {
isSort = (TEST_BIT_BLIST(list, 1) && !TEST_BIT_BLIST(list, 2));
}
else {
isSort = 0;
isSort = FALSE;
}
SET_FILT_LIST( list, (isSort ? FN_IS_SSORT : FN_IS_NSORT) );

Expand Down Expand Up @@ -894,20 +894,20 @@ UInt COUNT_TRUES_BLOCKS(const UInt * ptr, UInt nblocks)
** list that has no holes and contains only 'true' and 'false', and 0
** otherwise.
*/
static Int IsBlist(Obj list)
static BOOL IsBlist(Obj list)
{
UInt isBlist; /* result of the test */
BOOL isBlist; /* result of the test */
Int len; /* logical length of the list */
UInt i; /* loop variable */

/* if <list> is known to be a boolean list, it is very easy */
if ( IS_BLIST_REP(list) ) {
isBlist = 1;
isBlist = TRUE;
}

/* if <list> is not a small list, it isn't a boolean list (convert to list) */
else if ( ! IS_SMALL_LIST( list ) ) {
isBlist = 0;
isBlist = FALSE;
}

/* otherwise test if there are holes and if all elements are boolean */
Expand Down Expand Up @@ -940,20 +940,20 @@ static Int IsBlist(Obj list)
** boolean lists into the compact representation of type 'T_BLIST' described
** above.
*/
static Int IsBlistConv(Obj list)
static BOOL IsBlistConv(Obj list)
{
UInt isBlist; /* result of the test */
BOOL isBlist; /* result of the test */
Int len; /* logical length of the list */
UInt i; /* loop variable */

/* if <list> is known to be a boolean list, it is very easy */
if ( IS_BLIST_REP(list) ) {
isBlist = 1;
isBlist = TRUE;
}

/* if <list> is not a list, it isn't a boolean list (convert to list) */
else if ( ! IS_SMALL_LIST(list) ) {
isBlist = 0;
isBlist = FALSE;
}

/* otherwise test if there are holes and if all elements are boolean */
Expand Down
3 changes: 2 additions & 1 deletion src/blister.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
**
*F IS_BLIST_REP( <list> ) . . . . . check if <list> is in boolean list rep
*/
EXPORT_INLINE Int IS_BLIST_REP(Obj list)
EXPORT_INLINE BOOL IS_BLIST_REP(Obj list)
{
return T_BLIST <= TNUM_OBJ(list) &&
TNUM_OBJ(list) <= T_BLIST_SSORT + IMMUTABLE;
Expand All @@ -48,6 +48,7 @@ EXPORT_INLINE Int SIZE_PLEN_BLIST(Int plen)
return sizeof(Obj) + (plen + BIPEB - 1) / BIPEB * sizeof(UInt);
}


/****************************************************************************
**
*F LEN_BLIST( <list> ) . . . . . . . . . . . . . . length of a boolean list
Expand Down
8 changes: 4 additions & 4 deletions src/calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,9 @@ void PrintFunction (
Int narg; /* number of arguments */
Int nloc; /* number of locals */
UInt i; /* loop variable */
UInt isvarg; /* does function have varargs? */
BOOL isvarg; /* does function have varargs? */

isvarg = 0;
isvarg = FALSE;

if ( IS_OPERATION(func) ) {
CALL_1ARGS( PrintOperation, func );
Expand All @@ -1014,7 +1014,7 @@ void PrintFunction (
/* print the arguments */
narg = NARG_FUNC(func);
if (narg < 0) {
isvarg = 1;
isvarg = TRUE;
narg = -narg;
}

Expand Down Expand Up @@ -1527,7 +1527,7 @@ static Obj FuncIsKernelFunction(Obj self, Obj func)
return IsKernelFunction(func) ? True : False;
}

Int IsKernelFunction(Obj func)
BOOL IsKernelFunction(Obj func)
{
GAP_ASSERT(IS_FUNC(func));
return (BODY_FUNC(func) == 0) ||
Expand Down
4 changes: 2 additions & 2 deletions src/calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ EXPORT_INLINE void SET_LCKS_FUNC(Obj func, Obj locks)
** 'IsKernelFunction' returns 1 if <func> is a kernel function (i.e.
** compiled from C code), and 0 otherwise.
*/
Int IsKernelFunction(Obj func);
BOOL IsKernelFunction(Obj func);


EXPORT_INLINE ObjFunc_0ARGS HDLR_0ARGS(Obj func)
Expand Down Expand Up @@ -271,7 +271,7 @@ EXPORT_INLINE ObjFunc_1ARGS HDLR_XARGS(Obj func)
**
*F IS_FUNC( <obj> ) . . . . . . . . . . . . . check if object is a function
*/
EXPORT_INLINE int IS_FUNC(Obj obj)
EXPORT_INLINE BOOL IS_FUNC(Obj obj)
{
return TNUM_OBJ(obj) == T_FUNCTION;
}
Expand Down
4 changes: 2 additions & 2 deletions src/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void SET_VISITED_STAT(Stat stat);
** 'LVAR_REF_LVAR' returns the local variable (by its index) to which <expr>
** is a (immediate) reference.
*/
EXPORT_INLINE Int IS_REF_LVAR(Expr expr)
EXPORT_INLINE BOOL IS_REF_LVAR(Expr expr)
{
return ((Int)expr & 0x03) == 0x03;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ EXPORT_INLINE Int LVAR_REF_LVAR(Expr expr)
** 'INT_INTEXPR' converts the (immediate) integer expression <expr> to a C
** integer.
*/
EXPORT_INLINE Int IS_INTEXPR(Expr expr)
EXPORT_INLINE BOOL IS_INTEXPR(Expr expr)
{
return ((Int)expr & 0x03) == 0x01;
}
Expand Down
5 changes: 5 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ typedef uintptr_t UInt;
GAP_STATIC_ASSERT(sizeof(void *) == sizeof(Int), "sizeof(Int) is wrong");
GAP_STATIC_ASSERT(sizeof(void *) == sizeof(UInt), "sizeof(UInt) is wrong");

// FIXME: workaround a conflict with the Semigroups package
#undef BOOL
typedef Int BOOL; // TODO: should be changed to `char` once packages adapted
enum { FALSE = 0, TRUE = 1 };


/****************************************************************************
**
Expand Down
8 changes: 4 additions & 4 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,22 @@ static void MergeInfoCVars(Bag dst, Bag src)
}
}

static Int IsEqInfoCVars(Bag dst, Bag src)
static BOOL IsEqInfoCVars(Bag dst, Bag src)
{
Int i;
if ( SIZE_BAG(dst) < SIZE_BAG(src) ) ResizeBag( dst, SIZE_BAG(src) );
if ( SIZE_BAG(src) < SIZE_BAG(dst) ) ResizeBag( src, SIZE_BAG(dst) );
for ( i = 1; i <= NLVAR_INFO(src); i++ ) {
if ( TNUM_LVAR_INFO(dst,i) != TNUM_LVAR_INFO(src,i) ) {
return 0;
return FALSE;
}
}
for ( i = 1; i <= NTEMP_INFO(dst) && i <= NTEMP_INFO(src); i++ ) {
if ( TNUM_TEMP_INFO(dst,i) != TNUM_TEMP_INFO(src,i) ) {
return 0;
return FALSE;
}
}
return 1;
return TRUE;
}


Expand Down
7 changes: 4 additions & 3 deletions src/cyclotom.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ static Obj Cyclotomic(UInt n, UInt m)
UInt p; /* prime factor */
static UInt lastN; /* rember last n, dont recompute: */
static UInt phi; /* Euler phi(n) */
static UInt isSqfree; /* is n squarefree? */
static BOOL isSqfree; /* is n squarefree? */
static UInt nrp; /* number of its prime factors */

/* get a pointer to the cyclotomic and a copy of n to factor */
Expand Down Expand Up @@ -672,12 +672,13 @@ static Obj Cyclotomic(UInt n, UInt m)
if ( n != lastN ) {
lastN = n;
phi = n; k = n;
isSqfree = 1;
isSqfree = TRUE;
nrp = 0;
for ( p = 2; p <= k; p++ ) {
if ( k % p == 0 ) {
phi = phi * (p-1) / p;
if ( k % (p*p) == 0 ) isSqfree = 0;
if (k % (p * p) == 0)
isSqfree = FALSE;
nrp++;
while ( k % p == 0 ) k = k / p;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cyclotom.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
** 'IS_CYC' returns 1 if the argument object's tnum indicates that it is an
** internal integer, rational or (proper) cyclotomic object, otherwise 0.
*/
EXPORT_INLINE Int IS_CYC(Obj o)
EXPORT_INLINE BOOL IS_CYC(Obj o)
{
return TNUM_OBJ(o) <= T_CYC;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ enum {
**
*F IsUsingLibGap() . . . . . . . . 1 if GAP is being used a library, else 0
*/
int IsUsingLibGap(void);
BOOL IsUsingLibGap(void);

/****************************************************************************
**
Expand Down
11 changes: 9 additions & 2 deletions src/gaputils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@
#define ARRAY_SIZE(arr) ( sizeof(arr) / sizeof((arr)[0]) )


EXPORT_INLINE Int AlwaysYes(Obj obj) { return 1; }
EXPORT_INLINE Int AlwaysNo(Obj obj) { return 0; }
EXPORT_INLINE BOOL AlwaysYes(Obj obj)
{
return TRUE;
}

EXPORT_INLINE BOOL AlwaysNo(Obj obj)
{
return FALSE;
}

#endif // GAP_UTILS_H
20 changes: 10 additions & 10 deletions src/gasman.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ UInt NrHalfDeadBags;
**
*F IS_BAG_ID -- check if a value looks like a masterpointer id
*/
static inline UInt IS_BAG_ID(void * ptr)
static inline BOOL IS_BAG_ID(void * ptr)
{
return (((void *)MptrBags <= ptr) && (ptr < (void *)MptrEndBags) &&
((UInt)ptr & (sizeof(Bag) - 1)) == 0);
Expand All @@ -454,7 +454,7 @@ static inline UInt IS_BAG_ID(void * ptr)
**
*F IS_BAG_BODY -- check if value like a pointer to a bag body
*/
static inline UInt IS_BAG_BODY(void * ptr)
static inline BOOL IS_BAG_BODY(void * ptr)
{
return (((void *)OldBags <= ptr) && (ptr < (void *)AllocBags) &&
((UInt)ptr & (sizeof(Bag) - 1)) == 0);
Expand Down Expand Up @@ -608,17 +608,17 @@ static inline Bag MARKED_HALFDEAD(Bag x)
return (Bag)((UInt)x | HALFDEAD);
}

static inline Int IS_MARKED_DEAD(Bag x)
static inline BOOL IS_MARKED_DEAD(Bag x)
{
return LINK_BAG(x) == MARKED_DEAD(x);
}

// static inline Int IS_MARKED_ALIVE(Bag x)
// static inline BOOL IS_MARKED_ALIVE(Bag x)
// {
// return LINK_BAG(x) == MARKED_ALIVE(x);
// }

static inline Int IS_MARKED_HALFDEAD(Bag x)
static inline BOOL IS_MARKED_HALFDEAD(Bag x)
{
return LINK_BAG(x) == MARKED_HALFDEAD(x);
}
Expand Down Expand Up @@ -687,12 +687,12 @@ void MarkBagWeakly(Bag bag)
}
}

Int IsWeakDeadBag(Bag bag)
BOOL IsWeakDeadBag(Bag bag)
{
CANARY_DISABLE_VALGRIND();
Int isWeakDeadBag = (((UInt)bag & (sizeof(Bag) - 1)) == 0) &&
(Bag)MptrBags <= bag && bag < (Bag)MptrEndBags &&
(((UInt)*bag) & (sizeof(Bag) - 1)) == 1;
BOOL isWeakDeadBag = (((UInt)bag & (sizeof(Bag) - 1)) == 0) &&
(Bag)MptrBags <= bag && bag < (Bag)MptrEndBags &&
(((UInt)*bag) & (sizeof(Bag) - 1)) == 1;
CANARY_ENABLE_VALGRIND();
return isWeakDeadBag;
}
Expand All @@ -710,7 +710,7 @@ void CallbackForAllBags(void (*func)(Bag))
{
for (Bag bag = (Bag)MptrBags; bag < (Bag)MptrEndBags; bag++) {
CANARY_DISABLE_VALGRIND();
Int is_bag = IS_BAG_BODY(*bag);
BOOL is_bag = IS_BAG_BODY(*bag);
CANARY_ENABLE_VALGRIND();
if (is_bag) {
(*func)(bag);
Expand Down
4 changes: 2 additions & 2 deletions src/gasman.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ EXPORT_INLINE void CLEAR_BAG_FLAG(Bag bag, uint8_t flag)
**
** See also 'IS_INTOBJ' and 'IS_FFE'.
*/
EXPORT_INLINE Int IS_BAG_REF(Obj bag)
EXPORT_INLINE BOOL IS_BAG_REF(Obj bag)
{
return bag && !((Int)bag & 0x03);
}
Expand Down Expand Up @@ -358,7 +358,7 @@ EXPORT_INLINE void CHANGED_BAG(Bag bag)

void CHANGED_BAG(Bag bag);

int IsGapObj(void *);
BOOL IsGapObj(void *);

#elif defined(GAP_MEMORY_CANARY)

Expand Down
2 changes: 1 addition & 1 deletion src/gasman_intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void MarkBagWeakly(Bag bag);
** an object which was freed as the only references to it were weak.
** This is used for implement weak pointer references.
*/
Int IsWeakDeadBag(Bag bag);
BOOL IsWeakDeadBag(Bag bag);

/****************************************************************************
**
Expand Down
Loading

0 comments on commit 10d082e

Please sign in to comment.