<commit_before>#include <stdarg.h>
#include <float.h>

#define NB(n) n$builtins$##n

#ifdef NLANG_DEFINE_TYPES

struct n$builtins$cleaner {
  void *p;
  size_t sz;
};

typedef struct _$Ngen_n$builtins$Slice_impl$$n$builtins$U8_genN$_ NB(byteslice);

union NB(Varargintunion) {
  struct NB(Varargint) *ref;
  va_list valist;
  NB(byteslice) *s;
};

struct NB(Varargint) {
  NB(Int) n; // Must be first, see Count_left below.
  NB(I32) vacount; // As passed in.
  union NB(Varargintunion) u;
};

#endif

#ifdef NLANG_DECLARE_FUNCTIONS

#define NLANG_DEFER(dtor) __attribute__((__cleanup__(dtor)))

#define NLANG_CLEANUP_ZERO(x) \
  __attribute__((__cleanup__(n$builtins$clean_zero))) \
  struct n$builtins$cleaner __Ncleaner_##x = { .p = (x), .sz = sizeof(*(x)) }

static inline void n$builtins$clean_zero(struct n$builtins$cleaner *c);

#define NLANG_STRING_LITERAL(s) \
{ .bytes = { \
  .dat = (n$builtins$U8 *)s, \
  .cnt = sizeof(s)-1, \
  .cap = sizeof(s) } }

#define NLANG_BYTE_SLICE(b, cnt) \
{ .dat = (n$builtins$U8 *)b, \
  .cnt = cnt, \
  .cap = cnt }

#define NLANG_BUILTINS_VARARG_SLICE_CNT(ap) ( (ap)->u.s->cnt )
#define NLANG_BUILTINS_VARARG_SLICE_NTH(ap, t, nth) \
  ({ \
   n$builtins$Assert__((ap)->n != 0, NULL); \
   __attribute__((__unused__)) static t __dummy; \
   (void *) &(ap)->u.s->dat[(nth) * sizeof(*__dummy)]; })

#define NLANG_BUILTINS_VACOUNT_SLICE (-1)
#define NLANG_BUILTINS_VACOUNT_VARARGREF (-2)
#define NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE (0x10000000)
#define NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF (0x20000000)

#define NLANG_BUILTINS_VARARG_FWD_SLICE(va, _ap) do { \
  /* If forwarding the last vararg, we're changing mode. */ \
  void *_Ns = va_arg((_ap)->u.valist, void *); \
  va_end((_ap)->u.valist); \
  (va).ap.vacount = NLANG_BUILTINS_VACOUNT_SLICE; \
  (va).ap.u.s = _Ns; \
  (va).ap.n = NLANG_BUILTINS_VARARG_SLICE_CNT(&(va).ap); \
} while (0)

#define NLANG_BUILTINS_VARARG_FWD_REF(va, _ap) do { \
  struct n$builtins$Varargint *_Nref = va_arg((_ap)->u.valist, struct n$builtins$Varargint *); \
  va_end((_ap)->u.valist); \
  if (_Nref->vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) { \
    _Nref = _Nref->u.ref; \
  } \
  /* If forwarding the last vararg, we're changing mode. */ \
  (va).ap.vacount = NLANG_BUILTINS_VACOUNT_VARARGREF; \
  (va).ap.u.ref = _Nref; \
} while (0)

#define NLANG_BUILTINS_VARARG_LAST_IS_FWD(ap) \
  ( (ap)->vacount > 0 \
    && ((ap)->vacount \
        & (NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE | NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF)) )

#define NLANG_BUILTINS_VARARG_START(va) do { \
  va_start((va).ap.u.valist, _$Nvacount); \
  (va).ap.vacount = _$Nvacount; \
  if (_$Nvacount >= 0) { \
    (va).ap.n = _$Nvacount \
      & ~(NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE | NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF); \
  } else if (_$Nvacount == NLANG_BUILTINS_VACOUNT_SLICE) { \
    NLANG_BUILTINS_VARARG_FWD_SLICE(va, &(va).ap); \
  } else { \
    NLANG_BUILTINS_VARARG_FWD_REF(va, &(va).ap); \
  } \
} while (0)

#define NLANG_BUILTINS_VARARG_AP(va) \
  (((va).ap.vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) \
   ? (va).ap.u.ref : &(va).ap)

#define NLANG_BUILTINS_VARARG_END(va) do { \
  struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (_Nap->vacount >= 0) { \
    va_end(_Nap->u.valist); \
  } \
} while (0)

#define NLANG_BUILTINS_VARARG_NEXT_GET(t, ap) \
  ({ \
   (ap)->n -= 1; \
   va_arg((ap)->u.valist, t); \
   })

#define NLANG_BUILTINS_VARARG_NEXT_(t, rt, ap) \
  ({ n$builtins$Varargint$Count_left(ap) == 0 ? ( (rt){ 0 } ) : \
  ( (rt){ .X = ({ \
   (ap->vacount == NLANG_BUILTINS_VACOUNT_SLICE) ? \
   NLANG_BUILTINS_VARARG_SLICE_NTH(ap, t, \
                                   NLANG_BUILTINS_VARARG_SLICE_CNT(ap) \
                                   - ap->n--) \
   : NLANG_BUILTINS_VARARG_NEXT_GET(t, ap); \
   }), .Nonnil = 1 } ); })

#define NLANG_BUILTINS_VARARG_NEXT(t, rt, va) \
  ({ struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (n$builtins$Varargint$Count_left(_Nap) == 1 && NLANG_BUILTINS_VARARG_LAST_IS_FWD(_Nap)) { \
    if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE) { \
      NLANG_BUILTINS_VARARG_FWD_SLICE(va, _Nap); \
    } else if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF) { \
      NLANG_BUILTINS_VARARG_FWD_REF(va, _Nap); \
    } \
    _Nap = NLANG_BUILTINS_VARARG_AP(va); \
  } \
  NLANG_BUILTINS_VARARG_NEXT_(t, rt, _Nap); })

// FIXME: dyn slices support is missing
#define NLANG_BUILTINS_VARARG_NEXT_DYN_(t, rt, ap) \
  ({ n$builtins$Varargint$Count_left(ap) == 0 ? ( (rt){ 0 } ) : \
  ( (rt){ .X = ({ \
  NLANG_BUILTINS_VARARG_NEXT_GET(t, ap); \
  }), .Nonnil = 1 } ); })

// FIXME: dyn slices support is missing
#define NLANG_BUILTINS_VARARG_NEXT_DYN(t, rt, va) \
  ({ struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (n$builtins$Varargint$Count_left(_Nap) == 1 && NLANG_BUILTINS_VARARG_LAST_IS_FWD(_Nap)) { \
    if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF) { \
      NLANG_BUILTINS_VARARG_FWD_REF(va, _Nap); \
    } \
    _Nap = NLANG_BUILTINS_VARARG_AP(va); \
  } \
  NLANG_BUILTINS_VARARG_NEXT_DYN_(t, rt, _Nap); })

#define NLANG_MKDYN(dyn_type, _dyntable, _obj) \
  (dyn_type){ .obj = (_obj), .dyntable = (void *)(_dyntable) }

struct n$reflect$Type;
n$builtins$Bool n$reflect$Isa(void *dyntable, struct n$reflect$Type *i);

struct n$bench$Example;
n$builtins$Void n$bench$Example$Dtor(struct n$bench$Example *);

#endif

#ifdef NLANG_DEFINE_FUNCTIONS

static inline void n$builtins$clean_zero(struct n$builtins$cleaner *c) {
  memset(c->p, 0, c->sz);
}

static inline NB(Uint) NB(Varargint$Count_left)(struct NB(Varargint) *self) {
  if (self->vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) {
    return self->u.ref->n;
  } else {
    return self->n;<commit_msg>n.builtins: NLANG_MKDYN: for dyncast, zero obj pointer if dyntable is nil<commit_after>#include <stdarg.h>
#include <float.h>

#define NB(n) n$builtins$##n

#ifdef NLANG_DEFINE_TYPES

struct n$builtins$cleaner {
  void *p;
  size_t sz;
};

typedef struct _$Ngen_n$builtins$Slice_impl$$n$builtins$U8_genN$_ NB(byteslice);

union NB(Varargintunion) {
  struct NB(Varargint) *ref;
  va_list valist;
  NB(byteslice) *s;
};

struct NB(Varargint) {
  NB(Int) n; // Must be first, see Count_left below.
  NB(I32) vacount; // As passed in.
  union NB(Varargintunion) u;
};

#endif

#ifdef NLANG_DECLARE_FUNCTIONS

#define NLANG_DEFER(dtor) __attribute__((__cleanup__(dtor)))

#define NLANG_CLEANUP_ZERO(x) \
  __attribute__((__cleanup__(n$builtins$clean_zero))) \
  struct n$builtins$cleaner __Ncleaner_##x = { .p = (x), .sz = sizeof(*(x)) }

static inline void n$builtins$clean_zero(struct n$builtins$cleaner *c);

#define NLANG_STRING_LITERAL(s) \
{ .bytes = { \
  .dat = (n$builtins$U8 *)s, \
  .cnt = sizeof(s)-1, \
  .cap = sizeof(s) } }

#define NLANG_BYTE_SLICE(b, cnt) \
{ .dat = (n$builtins$U8 *)b, \
  .cnt = cnt, \
  .cap = cnt }

#define NLANG_BUILTINS_VARARG_SLICE_CNT(ap) ( (ap)->u.s->cnt )
#define NLANG_BUILTINS_VARARG_SLICE_NTH(ap, t, nth) \
  ({ \
   n$builtins$Assert__((ap)->n != 0, NULL); \
   __attribute__((__unused__)) static t __dummy; \
   (void *) &(ap)->u.s->dat[(nth) * sizeof(*__dummy)]; })

#define NLANG_BUILTINS_VACOUNT_SLICE (-1)
#define NLANG_BUILTINS_VACOUNT_VARARGREF (-2)
#define NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE (0x10000000)
#define NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF (0x20000000)

#define NLANG_BUILTINS_VARARG_FWD_SLICE(va, _ap) do { \
  /* If forwarding the last vararg, we're changing mode. */ \
  void *_Ns = va_arg((_ap)->u.valist, void *); \
  va_end((_ap)->u.valist); \
  (va).ap.vacount = NLANG_BUILTINS_VACOUNT_SLICE; \
  (va).ap.u.s = _Ns; \
  (va).ap.n = NLANG_BUILTINS_VARARG_SLICE_CNT(&(va).ap); \
} while (0)

#define NLANG_BUILTINS_VARARG_FWD_REF(va, _ap) do { \
  struct n$builtins$Varargint *_Nref = va_arg((_ap)->u.valist, struct n$builtins$Varargint *); \
  va_end((_ap)->u.valist); \
  if (_Nref->vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) { \
    _Nref = _Nref->u.ref; \
  } \
  /* If forwarding the last vararg, we're changing mode. */ \
  (va).ap.vacount = NLANG_BUILTINS_VACOUNT_VARARGREF; \
  (va).ap.u.ref = _Nref; \
} while (0)

#define NLANG_BUILTINS_VARARG_LAST_IS_FWD(ap) \
  ( (ap)->vacount > 0 \
    && ((ap)->vacount \
        & (NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE | NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF)) )

#define NLANG_BUILTINS_VARARG_START(va) do { \
  va_start((va).ap.u.valist, _$Nvacount); \
  (va).ap.vacount = _$Nvacount; \
  if (_$Nvacount >= 0) { \
    (va).ap.n = _$Nvacount \
      & ~(NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE | NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF); \
  } else if (_$Nvacount == NLANG_BUILTINS_VACOUNT_SLICE) { \
    NLANG_BUILTINS_VARARG_FWD_SLICE(va, &(va).ap); \
  } else { \
    NLANG_BUILTINS_VARARG_FWD_REF(va, &(va).ap); \
  } \
} while (0)

#define NLANG_BUILTINS_VARARG_AP(va) \
  (((va).ap.vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) \
   ? (va).ap.u.ref : &(va).ap)

#define NLANG_BUILTINS_VARARG_END(va) do { \
  struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (_Nap->vacount >= 0) { \
    va_end(_Nap->u.valist); \
  } \
} while (0)

#define NLANG_BUILTINS_VARARG_NEXT_GET(t, ap) \
  ({ \
   (ap)->n -= 1; \
   va_arg((ap)->u.valist, t); \
   })

#define NLANG_BUILTINS_VARARG_NEXT_(t, rt, ap) \
  ({ n$builtins$Varargint$Count_left(ap) == 0 ? ( (rt){ 0 } ) : \
  ( (rt){ .X = ({ \
   (ap->vacount == NLANG_BUILTINS_VACOUNT_SLICE) ? \
   NLANG_BUILTINS_VARARG_SLICE_NTH(ap, t, \
                                   NLANG_BUILTINS_VARARG_SLICE_CNT(ap) \
                                   - ap->n--) \
   : NLANG_BUILTINS_VARARG_NEXT_GET(t, ap); \
   }), .Nonnil = 1 } ); })

#define NLANG_BUILTINS_VARARG_NEXT(t, rt, va) \
  ({ struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (n$builtins$Varargint$Count_left(_Nap) == 1 && NLANG_BUILTINS_VARARG_LAST_IS_FWD(_Nap)) { \
    if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_SLICE) { \
      NLANG_BUILTINS_VARARG_FWD_SLICE(va, _Nap); \
    } else if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF) { \
      NLANG_BUILTINS_VARARG_FWD_REF(va, _Nap); \
    } \
    _Nap = NLANG_BUILTINS_VARARG_AP(va); \
  } \
  NLANG_BUILTINS_VARARG_NEXT_(t, rt, _Nap); })

// FIXME: dyn slices support is missing
#define NLANG_BUILTINS_VARARG_NEXT_DYN_(t, rt, ap) \
  ({ n$builtins$Varargint$Count_left(ap) == 0 ? ( (rt){ 0 } ) : \
  ( (rt){ .X = ({ \
  NLANG_BUILTINS_VARARG_NEXT_GET(t, ap); \
  }), .Nonnil = 1 } ); })

// FIXME: dyn slices support is missing
#define NLANG_BUILTINS_VARARG_NEXT_DYN(t, rt, va) \
  ({ struct n$builtins$Varargint *_Nap = NLANG_BUILTINS_VARARG_AP(va); \
  if (n$builtins$Varargint$Count_left(_Nap) == 1 && NLANG_BUILTINS_VARARG_LAST_IS_FWD(_Nap)) { \
    if (_Nap->vacount & NLANG_BUILTINS_VACOUNT_LAST_IS_FWD_REF) { \
      NLANG_BUILTINS_VARARG_FWD_REF(va, _Nap); \
    } \
    _Nap = NLANG_BUILTINS_VARARG_AP(va); \
  } \
  NLANG_BUILTINS_VARARG_NEXT_DYN_(t, rt, _Nap); })

#define NLANG_MKDYN(dyn_type, _dyntable, _obj) \
  (dyn_type){ .obj = (_dyntable) ? (_obj) : NULL, .dyntable = (void *)(_dyntable) }

struct n$reflect$Type;
n$builtins$Bool n$reflect$Isa(void *dyntable, struct n$reflect$Type *i);

struct n$bench$Example;
n$builtins$Void n$bench$Example$Dtor(struct n$bench$Example *);

#endif

#ifdef NLANG_DEFINE_FUNCTIONS

static inline void n$builtins$clean_zero(struct n$builtins$cleaner *c) {
  memset(c->p, 0, c->sz);
}

static inline NB(Uint) NB(Varargint$Count_left)(struct NB(Varargint) *self) {
  if (self->vacount == NLANG_BUILTINS_VACOUNT_VARARGREF) {
    return self->u.ref->n;
  } else {
    return self->n;
{'fraction_non_alphanumeric': 0.16144187238404373, 'fraction_numerical': 0.007516870248569232, 'mean_word_length': 4.037865748709122, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315367', 'n_tokens_mistral': 5494, 'n_tokens_neox': 5304, 'n_words': 761}