Skip to content

Commit

Permalink
Use default implementations for implicit operators.
Browse files Browse the repository at this point in the history
With this, implicit operators are not only faster, but it's also much easier to determine if an implicit operator has been defined (as `DeeType_InheritOperator()` now treats implicit operators just like regular operators)
  • Loading branch information
GrieferAtWork committed Apr 19, 2024
1 parent 12d94d4 commit 2edff53
Show file tree
Hide file tree
Showing 9 changed files with 2,761 additions and 2,096 deletions.
4 changes: 2 additions & 2 deletions cpp.hint
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,8 @@
#define SUPER_PRIVATE_EXPANDARGS(...) (DeeTypeObject *tp_self, __VA_ARGS__)
#define DEFINE_OPERATOR(return,n,args) return DeeObject_T##n SUPER_PRIVATE_EXPANDARGS args{} return DeeObject_##n args
#define DEFINE_INTERNAL_OPERATOR(return,n,args) return DeeObject_T##n SUPER_PRIVATE_EXPANDARGS args{} return DeeObject_##n args
#define DEFINE_DEFAULT_FOO_WITH_INPLACE_FOO_OPERATOR(x,...) DEFINE_INTERNAL_OPERATOR(DeeObject*,Default##x##WithInplace##x,(DeeObject*self,DeeObject*other)){}
#define DEFINE_DEFAULT_INPLACE_FOO_WITH_FOO_OPERATOR(x,...) DEFINE_INTERNAL_OPERATOR(DeeObject*,DefaultInplace##x##With##x,(DeeObject*self,DeeObject*other)){}
#define DEFINE_FILE_OPERATOR(return,n,args) return DeeFile_T##n SUPER_PRIVATE_EXPANDARGS args{} return DeeFile_##n args
#define DEFINE_MATH_INPLACE_INT_OPERATOR(n,a,b,c,...) int n(DeeObject**restrict p_self,c val){}
#define DEFINE_JSON_ITERATOR_COMPARE(n,...) DeeObject*n(DeeJsonIteratorObject*self,DeeJsonIteratorObject*other){}
Expand Down Expand Up @@ -1579,8 +1581,6 @@
#define DEFINE_TUPLEITERATOR_COMPARE(n,...) DeeObject*n(TupleIterator*self,TupleIterator*other){}
#define DEFINE_TYPE_INHERIT_FUNCTION(n,...) bool n(DeeTypeObject*self){}
#define DEFINE_TYPE_INHERIT_FUNCTION1(n,...) bool n(DeeTypeObject*self){}
#define DEFINE_TYPE_INHERIT_FUNCTION2(n,...) bool n(DeeTypeObject*self){}
#define DEFINE_MATH_OPERATOR1(n,...) DEFINE_OPERATOR(DeeObject*,n,(DeeObject*self)){}
#define DEFINE_MATH_OPERATOR2(n,...) DEFINE_OPERATOR(DeeObject*,n,(DeeObject*self,DeeObject*some_object)){}
#define DEFINE_MATH_INPLACE_OPERATOR2(n,...) DEFINE_OPERATOR(int,n,(DeeObject**p_self,DeeObject*some_object)){}
#define DEFINE_OBJECT_COMPARE_OPERATOR(n,...) DEFINE_OPERATOR(DeeObject*,n,(DeeObject*self,DeeObject*some_object)){}
Expand Down
754 changes: 581 additions & 173 deletions include/deemon/class.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions include/deemon/int.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ DFUNDEF WUNUSED DREF /*Int*/ DeeObject *DCALL DeeInt_NewUInt32(uint32_t val);
DFUNDEF WUNUSED DREF /*Int*/ DeeObject *DCALL DeeInt_NewUInt64(uint64_t val);
DFUNDEF WUNUSED DREF /*Int*/ DeeObject *DCALL DeeInt_NewInt128(Dee_int128_t val);
DFUNDEF WUNUSED DREF /*Int*/ DeeObject *DCALL DeeInt_NewUInt128(Dee_uint128_t val);
DFUNDEF WUNUSED DREF /*Int*/ DeeObject *DCALL DeeInt_NewDouble(double val); /* TODO: Rounding? */

/* Create an integer from signed/unsigned LEB data. */
DFUNDEF WUNUSED NONNULL((1)) DREF /*Int*/ DeeObject *DCALL
Expand Down Expand Up @@ -548,6 +549,7 @@ DFUNDEF WUNUSED ATTR_OUT(2) NONNULL((1)) int (DCALL DeeInt_AsUInt16)(/*Int*/ Dee
DFUNDEF WUNUSED ATTR_OUT(2) NONNULL((1)) int (DCALL DeeInt_AsUInt32)(/*Int*/ DeeObject *__restrict self, uint32_t *__restrict value);
DFUNDEF WUNUSED ATTR_OUT(2) NONNULL((1)) int (DCALL DeeInt_AsUInt64)(/*Int*/ DeeObject *__restrict self, uint64_t *__restrict value);
DFUNDEF WUNUSED ATTR_OUT(2) NONNULL((1)) int (DCALL DeeInt_AsUInt128)(/*Int*/ DeeObject *__restrict self, Dee_uint128_t *__restrict value);
DFUNDEF WUNUSED ATTR_OUT(2) NONNULL((1)) int (DCALL DeeInt_AsDouble)(/*Int*/ DeeObject *__restrict self, double *__restrict value);


/* Convert an integer to a binary-encoded data array. */
Expand Down
4 changes: 2 additions & 2 deletions include/deemon/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -3290,7 +3290,6 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritStr(DeeTypeObject *__restrict self
INTDEF NONNULL((1)) bool DCALL DeeType_InheritRepr(DeeTypeObject *__restrict self); /* tp_repr, tp_printrepr */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritBool(DeeTypeObject *__restrict self); /* tp_bool */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritCall(DeeTypeObject *__restrict self); /* tp_call, tp_call_kw */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritHash(DeeTypeObject *__restrict self); /* tp_hash */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritInt(DeeTypeObject *__restrict self); /* tp_int, tp_int32, tp_int64, tp_double */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritInv(DeeTypeObject *__restrict self); /* tp_inv */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritPos(DeeTypeObject *__restrict self); /* tp_pos */
Expand All @@ -3305,6 +3304,7 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritAnd(DeeTypeObject *__restrict self
INTDEF NONNULL((1)) bool DCALL DeeType_InheritOr(DeeTypeObject *__restrict self); /* tp_or, tp_inplace_or */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritXor(DeeTypeObject *__restrict self); /* tp_xor, tp_inplace_xor */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritPow(DeeTypeObject *__restrict self); /* tp_pow, tp_inplace_pow */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritHash(DeeTypeObject *__restrict self); /* tp_hash, tp_eq, tp_ne (in order to inherit "tp_hash", must also inherit "tp_eq" and "tp_ne") */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritCompare(DeeTypeObject *__restrict self); /* tp_eq, tp_ne, tp_lo, tp_le, tp_gr, tp_ge */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritIterNext(DeeTypeObject *__restrict self); /* tp_iter_next */ /* TODO: This should form a group with `tp_nii' */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritIterSelf(DeeTypeObject *__restrict self); /* tp_iter_self */ /* TODO: This should form a group with `tp_nsi' */
Expand All @@ -3326,7 +3326,6 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritBuffer(DeeTypeObject *__restrict s
#define DeeType_InheritRepr(self) DeeType_InheritOperator(self, OPERATOR_REPR)
#define DeeType_InheritBool(self) DeeType_InheritOperator(self, OPERATOR_BOOL)
#define DeeType_InheritCall(self) DeeType_InheritOperator(self, OPERATOR_CALL)
#define DeeType_InheritHash(self) DeeType_InheritOperator(self, OPERATOR_HASH)
#define DeeType_InheritInt(self) DeeType_InheritOperator(self, OPERATOR_INT)
#define DeeType_InheritInv(self) DeeType_InheritOperator(self, OPERATOR_INV)
#define DeeType_InheritPos(self) DeeType_InheritOperator(self, OPERATOR_POS)
Expand All @@ -3341,6 +3340,7 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritBuffer(DeeTypeObject *__restrict s
#define DeeType_InheritOr(self) DeeType_InheritOperator(self, OPERATOR_OR)
#define DeeType_InheritXor(self) DeeType_InheritOperator(self, OPERATOR_XOR)
#define DeeType_InheritPow(self) DeeType_InheritOperator(self, OPERATOR_POW)
#define DeeType_InheritHash(self) DeeType_InheritOperator(self, OPERATOR_HASH)
#define DeeType_InheritCompare(self) DeeType_InheritOperator(self, OPERATOR_EQ)
#define DeeType_InheritIterNext(self) DeeType_InheritOperator(self, OPERATOR_ITERNEXT)
#define DeeType_InheritIterSelf(self) DeeType_InheritOperator(self, OPERATOR_ITERSELF)
Expand Down
20 changes: 20 additions & 0 deletions src/deemon/objects/int.c
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,12 @@ DeeInt_NewInt128(Dee_int128_t val) {
return (DREF DeeObject *)result;
}

PUBLIC WUNUSED DREF /*Int*/ DeeObject *DCALL
DeeInt_NewDouble(double val) {
/* TODO: This is wrong; doubles can approximate values larger than 64-bit! */
return DeeInt_NewInt64((int64_t)val);
}


#if DIGIT_BITS == 30
#define DeeInt_DECIMAL_SHIFT 9 /* max(e such that 10**e fits in a digit) */
Expand Down Expand Up @@ -3170,6 +3176,20 @@ PUBLIC WUNUSED ATTR_OUT(2) NONNULL((1)) int
return err_integer_overflow(self, 128, false);
}

PUBLIC WUNUSED ATTR_OUT(2) NONNULL((1)) int
(DCALL DeeInt_AsDouble)(/*Int*/ DeeObject *__restrict self,
double *__restrict value) {
/* TODO: This is wrong; doubles can approximate values larger than 64-bit! */
int64_t temp;
int error = DeeInt_Get64Bit(self, &temp);
if (error == INT_UNSIGNED) {
*value = (double)(uint64_t)temp;
} else {
*value = (double)temp;
}
return error < 0 ? -1 : 0;
}


/* Convert an integer to a binary-encoded data array. */
PUBLIC WUNUSED NONNULL((1, 2)) int
Expand Down
Loading

0 comments on commit 2edff53

Please sign in to comment.