aboutsummaryrefslogtreecommitdiff
path: root/builtins/pointer.c
blob: bc35848813b20662459838af4e4974928fa8f75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <gc.h>
#include <gc/cord.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/param.h>
#include <err.h>

#include "../util.h"
#include "../SipHash/halfsiphash.h"
#include "functions.h"
#include "types.h"

typedef struct recursion_s {
    const void *ptr;
    struct recursion_s *next;
} recursion_t;

public CORD Pointer__cord(const void *x, bool colorize, const TypeInfo *type) {
    auto ptr_info = type->PointerInfo;
    const void *ptr = *(const void**)x;
    if (!ptr) {
        CORD typename = generic_as_str(NULL, false, ptr_info.pointed);
        return colorize ? CORD_asprintf("\x1b[34;1m!%s\x1b[m", typename) : CORD_cat(ptr_info.sigil, typename);
    }

    // Check for recursive references, so if `x.foo = x`, then it prints as
    // `@Foo{foo=@..1}` instead of overflowing the stack:
    static recursion_t *recursion = NULL;
    int32_t depth = 0;
    for (recursion_t *r = recursion; r; r = r->next) {
        ++depth;
        if (r->ptr == ptr)
            return CORD_asprintf(colorize ? "\x1b[34;1m%s..%d\x1b[m" : "%s..%d", ptr_info.sigil, depth);
    }

    CORD pointed;
    { // Stringify with this pointer flagged as a recursive one:
        recursion_t my_recursion = {.ptr=ptr, .next=recursion};
        recursion = &my_recursion;
        pointed = generic_as_str(ptr, colorize, ptr_info.pointed);
        recursion = recursion->next;
    }
    return colorize ? CORD_asprintf("\x1b[34;1m%s%r\x1b[m", ptr_info.sigil, pointed) : CORD_cat(ptr_info.sigil, pointed);
}

public int32_t Pointer__compare(const void *x, const void *y, const TypeInfo *type) {
    (void)type;
    const void *xp = *(const void**)x, *yp = *(const void**)y;
    return (xp > yp) - (xp < yp);
}

public bool Pointer__equal(const void *x, const void *y, const TypeInfo *type) {
    (void)type;
    const void *xp = *(const void**)x, *yp = *(const void**)y;
    return xp == yp;
}

public uint32_t Pointer__hash(const void *x, const TypeInfo *type) {
    (void)type;
    uint32_t hash;
    halfsiphash(x, sizeof(void*), SSS_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));
    return hash;
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0