ae2f_docs
Loading...
Searching...
No Matches
type_unique.h
Go to the documentation of this file.
1/** @file type_unique.h */
2
3#ifndef util_type_unique_h
4#define util_type_unique_h
5
6#include <ae2f/Keys.h>
7#include <clang-c/Index.h>
8#include <aclspv/spvty.h>
9#include "./vec.auto.h"
10#include <assert.h>
11
12/**
13 * @class util_type_unique
14 * @brief unique types when `ID_DEFAULT_` and `constant` is not enough
15 * */
16typedef struct {
17 aclspv_id_t m_id;
18 CXType m_type;
19} util_type_unique;
20
21/**
22 * @returns the index for `c_type`
23 * @returns `c_num_uniques` when not found
24 * @see util_type_unique
25 * */
26ae2f_inline static aclspv_wrd_t find_type_unique(
27 const aclspv_wrd_t c_num_uniques,
28 const util_type_unique* ae2f_restrict rd_uniques,
29 const CXType c_type
30 )
31{
32 aclspv_wrd_t IDX = c_num_uniques;
33 const CXType CANONICAL = clang_getCanonicalType(c_type);
34 if(rd_uniques) while(IDX--) {
35 if(clang_equalTypes(CANONICAL, rd_uniques[IDX].m_type))
36 break;
37 }
38
39 if(IDX < c_num_uniques)
40 return IDX;
41
42 return c_num_uniques;
43}
44
45/**
46 * @returns the index for `c_type`
47 * @returns `c_num_uniques` when not found so made new room and it succeed.
48 * @returns `c_num_uniques + 1` when error occurred.
49 * @see util_type_unique
50 * */
51ae2f_inline static aclspv_wrd_t mk_type_unique(
52 const aclspv_wrd_t c_num_uniques,
53 x_aclspv_vec* ae2f_restrict const h_uniques,
54 const CXType c_type
55 )
56{
57 aclspv_wrd_t IDX = 0;
58
59 ae2f_expected_but_else(h_uniques) {
60 assert(0 && "h_uniques is null");
61 return c_num_uniques + 1;
62 }
63
64 ae2f_unexpected_but_if(h_uniques->m_sz < (size_t)(sizeof(util_type_unique) * c_num_uniques)) {
65 assert(0 && "vector is too small - invalid c_num_uniques");
66 return c_num_uniques + 1;
67 }
68
69 if((IDX = find_type_unique(c_num_uniques, h_uniques->m_p, c_type)) != c_num_uniques)
70 return IDX;
71
72 _aclspv_grow_vec_with_copy(_aclspv_malloc, _aclspv_free, _aclspv_memcpy, L_new, *h_uniques
73 , (size_t)(c_num_uniques + 1) * (size_t)sizeof(c_num_uniques));
74
75 ae2f_expected_but_else(h_uniques->m_p) {
76 return c_num_uniques + 1;
77 }
78
79 c_num_uniques[(util_type_unique* ae2f_restrict)h_uniques->m_p].m_type = c_type;
80 c_num_uniques[(util_type_unique* ae2f_restrict)h_uniques->m_p].m_id = 0;
81 return c_num_uniques;
82}
83
84#endif
#define ae2f_unexpected_but_if(a)
Definition cc.h:192
#define ae2f_expected_but_else(a)
Definition cc.h:201
#define ae2f_restrict
Keyword as restrict on C99.
Definition cc.h:81
#define ae2f_inline
inline
Definition cc.h:149
#define aclspv_wrd_t
integer as word
Definition spvty.h:16