1 /*
2 ** C type management.
3 ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
4 */
5 
6 #ifndef _LJ_CTYPE_H
7 #define _LJ_CTYPE_H
8 
9 #include "lj_obj.h"
10 #include "lj_gc.h"
11 
12 #if LJ_HASFFI
13 
14 /* -- C type definitions -------------------------------------------------- */
15 
16 /* C type numbers. Highest 4 bits of C type info. ORDER CT. */
17 enum {
18   /* Externally visible types. */
19   CT_NUM,		/* Integer or floating-point numbers. */
20   CT_STRUCT,		/* Struct or union. */
21   CT_PTR,		/* Pointer or reference. */
22   CT_ARRAY,		/* Array or complex type. */
23   CT_MAYCONVERT = CT_ARRAY,
24   CT_VOID,		/* Void type. */
25   CT_ENUM,		/* Enumeration. */
26   CT_HASSIZE = CT_ENUM,  /* Last type where ct->size holds the actual size. */
27   CT_FUNC,		/* Function. */
28   CT_TYPEDEF,		/* Typedef. */
29   CT_ATTRIB,		/* Miscellaneous attributes. */
30   /* Internal element types. */
31   CT_FIELD,		/* Struct/union field or function parameter. */
32   CT_BITFIELD,		/* Struct/union bitfield. */
33   CT_CONSTVAL,		/* Constant value. */
34   CT_EXTERN,		/* External reference. */
35   CT_KW			/* Keyword. */
36 };
37 
38 LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR);
39 LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT);
40 
41 /*
42 **  ---------- info ------------
43 ** |type      flags...  A   cid | size   |  sib  | next  | name  |
44 ** +----------------------------+--------+-------+-------+-------+--
45 ** |NUM       BFcvUL..  A       | size   |       | type  |       |
46 ** |STRUCT    ..cvU..V  A       | size   | field | name? | name? |
47 ** |PTR       ..cvR...  A   cid | size   |       | type  |       |
48 ** |ARRAY     VCcv...V  A   cid | size   |       | type  |       |
49 ** |VOID      ..cv....  A       | size   |       | type  |       |
50 ** |ENUM                A   cid | size   | const | name? | name? |
51 ** |FUNC      ....VS.. cc   cid | nargs  | field | name? | name? |
52 ** |TYPEDEF                 cid |        |       | name  | name  |
53 ** |ATTRIB        attrnum   cid | attr   | sib?  | type? |       |
54 ** |FIELD                   cid | offset | field |       | name? |
55 ** |BITFIELD  B.cvU csz bsz pos | offset | field |       | name? |
56 ** |CONSTVAL    c           cid | value  | const | name  | name  |
57 ** |EXTERN                  cid |        | sib?  | name  | name  |
58 ** |KW                      tok | size   |       | name  | name  |
59 ** +----------------------------+--------+-------+-------+-------+--
60 **        ^^  ^^--- bits used for C type conversion dispatch
61 */
62 
63 /* C type info flags.     TFFArrrr  */
64 #define CTF_BOOL	0x08000000u	/* Boolean: NUM, BITFIELD. */
65 #define CTF_FP		0x04000000u	/* Floating-point: NUM. */
66 #define CTF_CONST	0x02000000u	/* Const qualifier. */
67 #define CTF_VOLATILE	0x01000000u	/* Volatile qualifier. */
68 #define CTF_UNSIGNED	0x00800000u	/* Unsigned: NUM, BITFIELD. */
69 #define CTF_LONG	0x00400000u	/* Long: NUM. */
70 #define CTF_VLA		0x00100000u	/* Variable-length: ARRAY, STRUCT. */
71 #define CTF_REF		0x00800000u	/* Reference: PTR. */
72 #define CTF_VECTOR	0x08000000u	/* Vector: ARRAY. */
73 #define CTF_COMPLEX	0x04000000u	/* Complex: ARRAY. */
74 #define CTF_UNION	0x00800000u	/* Union: STRUCT. */
75 #define CTF_VARARG	0x00800000u	/* Vararg: FUNC. */
76 #define CTF_SSEREGPARM	0x00400000u	/* SSE register parameters: FUNC. */
77 
78 #define CTF_QUAL	(CTF_CONST|CTF_VOLATILE)
79 #define CTF_ALIGN	(CTMASK_ALIGN<<CTSHIFT_ALIGN)
80 #define CTF_UCHAR	((char)-1 > 0 ? CTF_UNSIGNED : 0)
81 
82 /* Flags used in parser.  .F.Ammvf   cp->attr  */
83 #define CTFP_ALIGNED	0x00000001u	/* cp->attr + ALIGN */
84 #define CTFP_PACKED	0x00000002u	/* cp->attr */
85 /*                        ...C...f   cp->fattr */
86 #define CTFP_CCONV	0x00000001u	/* cp->fattr + CCONV/[SSE]REGPARM */
87 
88 /* C type info bitfields. */
89 #define CTMASK_CID	0x0000ffffu	/* Max. 65536 type IDs. */
90 #define CTMASK_NUM	0xf0000000u	/* Max. 16 type numbers. */
91 #define CTSHIFT_NUM	28
92 #define CTMASK_ALIGN	15		/* Max. alignment is 2^15. */
93 #define CTSHIFT_ALIGN	16
94 #define CTMASK_ATTRIB	255		/* Max. 256 attributes. */
95 #define CTSHIFT_ATTRIB	16
96 #define CTMASK_CCONV	3		/* Max. 4 calling conventions. */
97 #define CTSHIFT_CCONV	16
98 #define CTMASK_REGPARM	3		/* Max. 0-3 regparms. */
99 #define CTSHIFT_REGPARM	18
100 /* Bitfields only used in parser. */
101 #define CTMASK_VSIZEP	15		/* Max. vector size is 2^15. */
102 #define CTSHIFT_VSIZEP	4
103 #define CTMASK_MSIZEP	255		/* Max. type size (via mode) is 128. */
104 #define CTSHIFT_MSIZEP	8
105 
106 /* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */
107 #define CTBSZ_MAX	32		/* Max. size of bitfield is 32 bit. */
108 #define CTBSZ_FIELD	127		/* Temp. marker for regular field. */
109 #define CTMASK_BITPOS	127
110 #define CTMASK_BITBSZ	127
111 #define CTMASK_BITCSZ	127
112 #define CTSHIFT_BITPOS	0
113 #define CTSHIFT_BITBSZ	8
114 #define CTSHIFT_BITCSZ	16
115 
116 #define CTF_INSERT(info, field, val) \
117   info = (info & ~(CTMASK_##field<<CTSHIFT_##field)) | \
118 	  (((CTSize)(val) & CTMASK_##field) << CTSHIFT_##field)
119 
120 /* Calling conventions. ORDER CC */
121 enum { CTCC_CDECL, CTCC_THISCALL, CTCC_FASTCALL, CTCC_STDCALL };
122 
123 /* Attribute numbers. */
124 enum {
125   CTA_NONE,		/* Ignored attribute. Must be zero. */
126   CTA_QUAL,		/* Unmerged qualifiers. */
127   CTA_ALIGN,		/* Alignment override. */
128   CTA_SUBTYPE,		/* Transparent sub-type. */
129   CTA_REDIR,		/* Redirected symbol name. */
130   CTA_BAD,		/* To catch bad IDs. */
131   CTA__MAX
132 };
133 
134 /* Special sizes. */
135 #define CTSIZE_INVALID	0xffffffffu
136 
137 typedef uint32_t CTInfo;	/* Type info. */
138 typedef uint32_t CTSize;	/* Type size. */
139 typedef uint32_t CTypeID;	/* Type ID. */
140 typedef uint16_t CTypeID1;	/* Minimum-sized type ID. */
141 
142 /* C type table element. */
143 typedef struct CType {
144   CTInfo info;		/* Type info. */
145   CTSize size;		/* Type size or other info. */
146   CTypeID1 sib;		/* Sibling element. */
147   CTypeID1 next;	/* Next element in hash chain. */
148   GCRef name;		/* Element name (GCstr). */
149 } CType;
150 
151 #define CTHASH_SIZE	128	/* Number of hash anchors. */
152 #define CTHASH_MASK	(CTHASH_SIZE-1)
153 
154 /* Simplify target-specific configuration. Checked in lj_ccall.h. */
155 #define CCALL_MAX_GPR		8
156 #define CCALL_MAX_FPR		14
157 
158 typedef LJ_ALIGN(8) union FPRCBArg { double d; float f[2]; } FPRCBArg;
159 
160 /* C callback state. Defined here, to avoid dragging in lj_ccall.h. */
161 
162 typedef LJ_ALIGN(8) struct CCallback {
163   FPRCBArg fpr[CCALL_MAX_FPR];	/* Arguments/results in FPRs. */
164   intptr_t gpr[CCALL_MAX_GPR];	/* Arguments/results in GPRs. */
165   intptr_t *stack;		/* Pointer to arguments on stack. */
166   void *mcode;			/* Machine code for callback func. pointers. */
167   CTypeID1 *cbid;		/* Callback type table. */
168   MSize sizeid;			/* Size of callback type table. */
169   MSize topid;			/* Highest unused callback type table slot. */
170   MSize slot;			/* Current callback slot. */
171 } CCallback;
172 
173 /* C type state. */
174 typedef struct CTState {
175   CType *tab;		/* C type table. */
176   CTypeID top;		/* Current top of C type table. */
177   MSize sizetab;	/* Size of C type table. */
178   lua_State *L;		/* Lua state (needed for errors and allocations). */
179   global_State *g;	/* Global state. */
180   GCtab *finalizer;	/* Map of cdata to finalizer. */
181   GCtab *miscmap;	/* Map of -CTypeID to metatable and cb slot to func. */
182   CCallback cb;		/* Temporary callback state. */
183   CTypeID1 hash[CTHASH_SIZE];  /* Hash anchors for C type table. */
184 } CTState;
185 
186 #define CTINFO(ct, flags)	(((CTInfo)(ct) << CTSHIFT_NUM) + (flags))
187 #define CTALIGN(al)		((CTSize)(al) << CTSHIFT_ALIGN)
188 #define CTATTRIB(at)		((CTInfo)(at) << CTSHIFT_ATTRIB)
189 
190 #define ctype_type(info)	((info) >> CTSHIFT_NUM)
191 #define ctype_cid(info)		((CTypeID)((info) & CTMASK_CID))
192 #define ctype_align(info)	(((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN)
193 #define ctype_attrib(info)	(((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB)
194 #define ctype_bitpos(info)	(((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS)
195 #define ctype_bitbsz(info)	(((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ)
196 #define ctype_bitcsz(info)	(((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ)
197 #define ctype_vsizeP(info)	(((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP)
198 #define ctype_msizeP(info)	(((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP)
199 #define ctype_cconv(info)	(((info) >> CTSHIFT_CCONV) & CTMASK_CCONV)
200 
201 /* Simple type checks. */
202 #define ctype_isnum(info)	(ctype_type((info)) == CT_NUM)
203 #define ctype_isvoid(info)	(ctype_type((info)) == CT_VOID)
204 #define ctype_isptr(info)	(ctype_type((info)) == CT_PTR)
205 #define ctype_isarray(info)	(ctype_type((info)) == CT_ARRAY)
206 #define ctype_isstruct(info)	(ctype_type((info)) == CT_STRUCT)
207 #define ctype_isfunc(info)	(ctype_type((info)) == CT_FUNC)
208 #define ctype_isenum(info)	(ctype_type((info)) == CT_ENUM)
209 #define ctype_istypedef(info)	(ctype_type((info)) == CT_TYPEDEF)
210 #define ctype_isattrib(info)	(ctype_type((info)) == CT_ATTRIB)
211 #define ctype_isfield(info)	(ctype_type((info)) == CT_FIELD)
212 #define ctype_isbitfield(info)	(ctype_type((info)) == CT_BITFIELD)
213 #define ctype_isconstval(info)	(ctype_type((info)) == CT_CONSTVAL)
214 #define ctype_isextern(info)	(ctype_type((info)) == CT_EXTERN)
215 #define ctype_hassize(info)	(ctype_type((info)) <= CT_HASSIZE)
216 
217 /* Combined type and flag checks. */
218 #define ctype_isinteger(info) \
219   (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0))
220 #define ctype_isinteger_or_bool(info) \
221   (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0))
222 #define ctype_isbool(info) \
223   (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL))
224 #define ctype_isfp(info) \
225   (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP))
226 
227 #define ctype_ispointer(info) \
228   ((ctype_type(info) >> 1) == (CT_PTR >> 1))  /* Pointer or array. */
229 #define ctype_isref(info) \
230   (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF))
231 
232 #define ctype_isrefarray(info) \
233   (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0))
234 #define ctype_isvector(info) \
235   (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR))
236 #define ctype_iscomplex(info) \
237   (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX))
238 
239 #define ctype_isvltype(info) \
240   (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \
241    CTINFO(CT_STRUCT, CTF_VLA))  /* VL array or VL struct. */
242 #define ctype_isvlarray(info) \
243   (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA))
244 
245 #define ctype_isxattrib(info, at) \
246   (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \
247    CTINFO(CT_ATTRIB, CTATTRIB(at)))
248 
249 /* Target-dependent sizes and alignments. */
250 #if LJ_64
251 #define CTSIZE_PTR	8
252 #define CTALIGN_PTR	CTALIGN(3)
253 #else
254 #define CTSIZE_PTR	4
255 #define CTALIGN_PTR	CTALIGN(2)
256 #endif
257 
258 #define CTINFO_REF(ref) \
259   CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref))
260 
261 #define CT_MEMALIGN	3	/* Alignment guaranteed by memory allocator. */
262 
263 #ifdef LUA_USE_ASSERT
264 #define lj_assertCTS(c, ...)	(lj_assertG_(cts->g, (c), __VA_ARGS__))
265 #else
266 #define lj_assertCTS(c, ...)	((void)cts)
267 #endif
268 
269 /* -- Predefined types ---------------------------------------------------- */
270 
271 /* Target-dependent types. */
272 #if LJ_TARGET_PPC
273 #define CTTYDEFP(_) \
274   _(LINT32,		4,	CT_NUM, CTF_LONG|CTALIGN(2))
275 #else
276 #define CTTYDEFP(_)
277 #endif
278 
279 /* Common types. */
280 #define CTTYDEF(_) \
281   _(NONE,		0,	CT_ATTRIB, CTATTRIB(CTA_BAD)) \
282   _(VOID,		-1,	CT_VOID, CTALIGN(0)) \
283   _(CVOID,		-1,	CT_VOID, CTF_CONST|CTALIGN(0)) \
284   _(BOOL,		1,	CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \
285   _(CCHAR,		1,	CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \
286   _(INT8,		1,	CT_NUM, CTALIGN(0)) \
287   _(UINT8,		1,	CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \
288   _(INT16,		2,	CT_NUM, CTALIGN(1)) \
289   _(UINT16,		2,	CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \
290   _(INT32,		4,	CT_NUM, CTALIGN(2)) \
291   _(UINT32,		4,	CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \
292   _(INT64,		8,	CT_NUM, CTF_LONG|CTALIGN(3)) \
293   _(UINT64,		8,	CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \
294   _(FLOAT,		4,	CT_NUM, CTF_FP|CTALIGN(2)) \
295   _(DOUBLE,		8,	CT_NUM, CTF_FP|CTALIGN(3)) \
296   _(COMPLEX_FLOAT,	8,	CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \
297   _(COMPLEX_DOUBLE,	16,	CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \
298   _(P_VOID,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_VOID) \
299   _(P_CVOID,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_CVOID) \
300   _(P_CCHAR,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_CCHAR) \
301   _(P_UINT8,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_UINT8) \
302   _(A_CCHAR,		-1,	CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \
303   _(CTYPEID,		4,	CT_ENUM, CTALIGN(2)|CTID_INT32) \
304   CTTYDEFP(_) \
305   /* End of type list. */
306 
307 /* Public predefined type IDs. */
308 enum {
309 #define CTTYIDDEF(id, sz, ct, info)	CTID_##id,
310 CTTYDEF(CTTYIDDEF)
311 #undef CTTYIDDEF
312   /* Predefined typedefs and keywords follow. */
313   CTID_MAX = 65536
314 };
315 
316 /* Target-dependent type IDs. */
317 #if LJ_64
318 #define CTID_INT_PSZ	CTID_INT64
319 #define CTID_UINT_PSZ	CTID_UINT64
320 #else
321 #define CTID_INT_PSZ	CTID_INT32
322 #define CTID_UINT_PSZ	CTID_UINT32
323 #endif
324 
325 #if LJ_ABI_WIN
326 #define CTID_WCHAR	CTID_UINT16
327 #elif LJ_TARGET_PPC
328 #define CTID_WCHAR	CTID_LINT32
329 #else
330 #define CTID_WCHAR	CTID_INT32
331 #endif
332 
333 /* -- C tokens and keywords ----------------------------------------------- */
334 
335 /* C lexer keywords. */
336 #define CTOKDEF(_) \
337   _(IDENT, "<identifier>") _(STRING, "<string>") \
338   _(INTEGER, "<integer>") _(EOF, "<eof>") \
339   _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \
340   _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->")
341 
342 /* Simple declaration specifiers. */
343 #define CDSDEF(_) \
344   _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \
345   _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \
346   _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \
347   _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER)
348 
349 /* C keywords. */
350 #define CKWDEF(_) \
351   CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \
352   _(DECLSPEC) _(CCDECL) _(PTRSZ) \
353   _(STRUCT) _(UNION) _(ENUM) \
354   _(SIZEOF) _(ALIGNOF)
355 
356 /* C token numbers. */
357 enum {
358   CTOK_OFS = 255,
359 #define CTOKNUM(name, sym)	CTOK_##name,
360 #define CKWNUM(name)		CTOK_##name,
361 CTOKDEF(CTOKNUM)
362 CKWDEF(CKWNUM)
363 #undef CTOKNUM
364 #undef CKWNUM
365   CTOK_FIRSTDECL = CTOK_VOID,
366   CTOK_FIRSTSCL = CTOK_TYPEDEF,
367   CTOK_LASTDECLFLAG = CTOK_REGISTER,
368   CTOK_LASTDECL = CTOK_ENUM
369 };
370 
371 /* Declaration specifier flags. */
372 enum {
373 #define CDSFLAG(name)	CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)),
374 CDSDEF(CDSFLAG)
375 #undef CDSFLAG
376   CDF__END
377 };
378 
379 #define CDF_SCL  (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER)
380 
381 /* -- C type management --------------------------------------------------- */
382 
383 #define ctype_ctsG(g)		(mref((g)->ctype_state, CTState))
384 
385 /* Get C type state. */
ctype_cts(lua_State * L)386 static LJ_AINLINE CTState *ctype_cts(lua_State *L)
387 {
388   CTState *cts = ctype_ctsG(G(L));
389   cts->L = L;  /* Save L for errors and allocations. */
390   return cts;
391 }
392 
393 /* Load FFI library on-demand. */
394 #define ctype_loadffi(L) \
395   do { \
396     if (!ctype_ctsG(G(L))) { \
397       ptrdiff_t oldtop = (char *)L->top - mref(L->stack, char); \
398       luaopen_ffi(L); \
399       L->top = (TValue *)(mref(L->stack, char) + oldtop); \
400     } \
401   } while (0)
402 
403 /* Save and restore state of C type table. */
404 #define LJ_CTYPE_SAVE(cts)	CTState savects_ = *(cts)
405 #define LJ_CTYPE_RESTORE(cts) \
406   ((cts)->top = savects_.top, \
407    memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash)))
408 
409 /* Check C type ID for validity when assertions are enabled. */
ctype_check(CTState * cts,CTypeID id)410 static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id)
411 {
412   UNUSED(cts);
413   lj_assertCTS(id > 0 && id < cts->top, "bad CTID %d", id);
414   return id;
415 }
416 
417 /* Get C type for C type ID. */
ctype_get(CTState * cts,CTypeID id)418 static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id)
419 {
420   return &cts->tab[ctype_check(cts, id)];
421 }
422 
423 /* Get C type ID for a C type. */
424 #define ctype_typeid(cts, ct)	((CTypeID)((ct) - (cts)->tab))
425 
426 /* Get child C type. */
ctype_child(CTState * cts,CType * ct)427 static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct)
428 {
429   lj_assertCTS(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) ||
430 	       ctype_isbitfield(ct->info)),
431 	       "ctype %08x has no children", ct->info);
432   return ctype_get(cts, ctype_cid(ct->info));
433 }
434 
435 /* Get raw type for a C type ID. */
ctype_raw(CTState * cts,CTypeID id)436 static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id)
437 {
438   CType *ct = ctype_get(cts, id);
439   while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct);
440   return ct;
441 }
442 
443 /* Get raw type of the child of a C type. */
ctype_rawchild(CTState * cts,CType * ct)444 static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct)
445 {
446   do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info));
447   return ct;
448 }
449 
450 /* Set the name of a C type table element. */
ctype_setname(CType * ct,GCstr * s)451 static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s)
452 {
453   /* NOBARRIER: mark string as fixed -- the C type table is never collected. */
454   fixstring(s);
455   setgcref(ct->name, obj2gco(s));
456 }
457 
458 LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp);
459 LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size);
460 LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id);
461 LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name,
462 				 uint32_t tmask);
463 LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name,
464 				  CTSize *ofs, CTInfo *qual);
465 #define lj_ctype_getfield(cts, ct, name, ofs) \
466   lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL)
467 LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id);
468 LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id);
469 LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem);
470 LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp);
471 LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm);
472 LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name);
473 LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned);
474 LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size);
475 LJ_FUNC CTState *lj_ctype_init(lua_State *L);
476 LJ_FUNC void lj_ctype_freestate(global_State *g);
477 
478 #endif
479 
480 #endif
481