Use shape_id for determining "too complex"
[ruby.git] / internal / struct.h
bloba8c773b730300edecb46f97bda4e3a8748c07f0b
1 #ifndef INTERNAL_STRUCT_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_STRUCT_H
3 /**
4 * @author Ruby developers <ruby-core@ruby-lang.org>
5 * @copyright This file is a part of the programming language Ruby.
6 * Permission is hereby granted, to either redistribute and/or
7 * modify this file, provided that the conditions mentioned in the
8 * file COPYING are met. Consult the file for details.
9 * @brief Internal header for Struct.
11 #include "ruby/internal/stdbool.h" /* for bool */
12 #include "ruby/ruby.h" /* for struct RBasic */
14 enum {
15 RSTRUCT_EMBED_LEN_MASK = RUBY_FL_USER7 | RUBY_FL_USER6 | RUBY_FL_USER5 | RUBY_FL_USER4 |
16 RUBY_FL_USER3 | RUBY_FL_USER2 | RUBY_FL_USER1,
17 RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
20 struct RStruct {
21 struct RBasic basic;
22 union {
23 struct {
24 long len;
25 const VALUE *ptr;
26 } heap;
27 /* This is a length 1 array because:
28 * 1. GCC has a bug that does not optimize C flexible array members
29 * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102452)
30 * 2. Zero length arrays are not supported by all compilers
32 const VALUE ary[1];
33 } as;
36 #define RSTRUCT(obj) ((struct RStruct *)(obj))
38 #ifdef RSTRUCT_LEN
39 # undef RSTRUCT_LEN
40 #endif
42 #ifdef RSTRUCT_PTR
43 # undef RSTRUCT_PTR
44 #endif
46 #ifdef RSTRUCT_SET
47 # undef RSTRUCT_SET
48 #endif
50 #ifdef RSTRUCT_GET
51 # undef RSTRUCT_GET
52 #endif
54 #define RSTRUCT_LEN internal_RSTRUCT_LEN
55 #define RSTRUCT_SET internal_RSTRUCT_SET
56 #define RSTRUCT_GET internal_RSTRUCT_GET
58 /* struct.c */
59 VALUE rb_struct_init_copy(VALUE copy, VALUE s);
60 VALUE rb_struct_lookup(VALUE s, VALUE idx);
61 VALUE rb_struct_s_keyword_init(VALUE klass);
62 static inline long RSTRUCT_EMBED_LEN(VALUE st);
63 static inline long RSTRUCT_LEN(VALUE st);
64 static inline int RSTRUCT_LENINT(VALUE st);
65 static inline const VALUE *RSTRUCT_CONST_PTR(VALUE st);
66 static inline void RSTRUCT_SET(VALUE st, long k, VALUE v);
67 static inline VALUE RSTRUCT_GET(VALUE st, long k);
69 static inline long
70 RSTRUCT_EMBED_LEN(VALUE st)
72 long ret = FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK);
73 ret >>= RSTRUCT_EMBED_LEN_SHIFT;
74 return ret;
77 static inline long
78 RSTRUCT_LEN(VALUE st)
80 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
81 return RSTRUCT_EMBED_LEN(st);
83 else {
84 return RSTRUCT(st)->as.heap.len;
88 static inline int
89 RSTRUCT_LENINT(VALUE st)
91 return rb_long2int(RSTRUCT_LEN(st));
94 static inline const VALUE *
95 RSTRUCT_CONST_PTR(VALUE st)
97 const struct RStruct *p = RSTRUCT(st);
99 if (FL_TEST_RAW(st, RSTRUCT_EMBED_LEN_MASK)) {
100 return p->as.ary;
102 else {
103 return p->as.heap.ptr;
107 static inline void
108 RSTRUCT_SET(VALUE st, long k, VALUE v)
110 RB_OBJ_WRITE(st, &RSTRUCT_CONST_PTR(st)[k], v);
113 static inline VALUE
114 RSTRUCT_GET(VALUE st, long k)
116 return RSTRUCT_CONST_PTR(st)[k];
119 #endif /* INTERNAL_STRUCT_H */