diff options
author | David Walter Seikel | 2014-01-13 21:08:31 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-01-13 21:08:31 +1000 |
commit | 637177eb1397ef1800027bccd50dbdc1af29a15b (patch) | |
tree | 3670a48303d05fceb8bf3ec4ee2901b72fe62d4d /libraries/luajit-2.0/src/lj_cparse.c | |
parent | Update Irrlicht to 1.8.1. Include actual change markers this time. lol (diff) | |
download | SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.zip SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.gz SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.bz2 SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.xz |
Remove LuaJIT source, we can use packaged LuaJIT 2.0 release now.
Also some cleanups related to the other library removals.
Diffstat (limited to '')
-rw-r--r-- | libraries/luajit-2.0/src/lj_cparse.c | 1839 |
1 files changed, 0 insertions, 1839 deletions
diff --git a/libraries/luajit-2.0/src/lj_cparse.c b/libraries/luajit-2.0/src/lj_cparse.c deleted file mode 100644 index 4f562df..0000000 --- a/libraries/luajit-2.0/src/lj_cparse.c +++ /dev/null | |||
@@ -1,1839 +0,0 @@ | |||
1 | /* | ||
2 | ** C declaration parser. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #include "lj_obj.h" | ||
7 | |||
8 | #if LJ_HASFFI | ||
9 | |||
10 | #include "lj_gc.h" | ||
11 | #include "lj_err.h" | ||
12 | #include "lj_str.h" | ||
13 | #include "lj_ctype.h" | ||
14 | #include "lj_cparse.h" | ||
15 | #include "lj_frame.h" | ||
16 | #include "lj_vm.h" | ||
17 | #include "lj_char.h" | ||
18 | |||
19 | /* | ||
20 | ** Important note: this is NOT a validating C parser! This is a minimal | ||
21 | ** C declaration parser, solely for use by the LuaJIT FFI. | ||
22 | ** | ||
23 | ** It ought to return correct results for properly formed C declarations, | ||
24 | ** but it may accept some invalid declarations, too (and return nonsense). | ||
25 | ** Also, it shows rather generic error messages to avoid unnecessary bloat. | ||
26 | ** If in doubt, please check the input against your favorite C compiler. | ||
27 | */ | ||
28 | |||
29 | /* -- C lexer ------------------------------------------------------------- */ | ||
30 | |||
31 | /* C lexer token names. */ | ||
32 | static const char *const ctoknames[] = { | ||
33 | #define CTOKSTR(name, str) str, | ||
34 | CTOKDEF(CTOKSTR) | ||
35 | #undef CTOKSTR | ||
36 | NULL | ||
37 | }; | ||
38 | |||
39 | /* Forward declaration. */ | ||
40 | LJ_NORET static void cp_err(CPState *cp, ErrMsg em); | ||
41 | |||
42 | static const char *cp_tok2str(CPState *cp, CPToken tok) | ||
43 | { | ||
44 | lua_assert(tok < CTOK_FIRSTDECL); | ||
45 | if (tok > CTOK_OFS) | ||
46 | return ctoknames[tok-CTOK_OFS-1]; | ||
47 | else if (!lj_char_iscntrl(tok)) | ||
48 | return lj_str_pushf(cp->L, "%c", tok); | ||
49 | else | ||
50 | return lj_str_pushf(cp->L, "char(%d)", tok); | ||
51 | } | ||
52 | |||
53 | /* End-of-line? */ | ||
54 | static LJ_AINLINE int cp_iseol(CPChar c) | ||
55 | { | ||
56 | return (c == '\n' || c == '\r'); | ||
57 | } | ||
58 | |||
59 | static LJ_AINLINE CPChar cp_get(CPState *cp); | ||
60 | |||
61 | /* Peek next raw character. */ | ||
62 | static LJ_AINLINE CPChar cp_rawpeek(CPState *cp) | ||
63 | { | ||
64 | return (CPChar)(uint8_t)(*cp->p); | ||
65 | } | ||
66 | |||
67 | /* Transparently skip backslash-escaped line breaks. */ | ||
68 | static LJ_NOINLINE CPChar cp_get_bs(CPState *cp) | ||
69 | { | ||
70 | CPChar c2, c = cp_rawpeek(cp); | ||
71 | if (!cp_iseol(c)) return cp->c; | ||
72 | cp->p++; | ||
73 | c2 = cp_rawpeek(cp); | ||
74 | if (cp_iseol(c2) && c2 != c) cp->p++; | ||
75 | cp->linenumber++; | ||
76 | return cp_get(cp); | ||
77 | } | ||
78 | |||
79 | /* Get next character. */ | ||
80 | static LJ_AINLINE CPChar cp_get(CPState *cp) | ||
81 | { | ||
82 | cp->c = (CPChar)(uint8_t)(*cp->p++); | ||
83 | if (LJ_LIKELY(cp->c != '\\')) return cp->c; | ||
84 | return cp_get_bs(cp); | ||
85 | } | ||
86 | |||
87 | /* Grow save buffer. */ | ||
88 | static LJ_NOINLINE void cp_save_grow(CPState *cp, CPChar c) | ||
89 | { | ||
90 | MSize newsize; | ||
91 | if (cp->sb.sz >= CPARSE_MAX_BUF/2) | ||
92 | cp_err(cp, LJ_ERR_XELEM); | ||
93 | newsize = cp->sb.sz * 2; | ||
94 | lj_str_resizebuf(cp->L, &cp->sb, newsize); | ||
95 | cp->sb.buf[cp->sb.n++] = (char)c; | ||
96 | } | ||
97 | |||
98 | /* Save character in buffer. */ | ||
99 | static LJ_AINLINE void cp_save(CPState *cp, CPChar c) | ||
100 | { | ||
101 | if (LJ_UNLIKELY(cp->sb.n + 1 > cp->sb.sz)) | ||
102 | cp_save_grow(cp, c); | ||
103 | else | ||
104 | cp->sb.buf[cp->sb.n++] = (char)c; | ||
105 | } | ||
106 | |||
107 | /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ | ||
108 | static void cp_newline(CPState *cp) | ||
109 | { | ||
110 | CPChar c = cp_rawpeek(cp); | ||
111 | if (cp_iseol(c) && c != cp->c) cp->p++; | ||
112 | cp->linenumber++; | ||
113 | } | ||
114 | |||
115 | LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...) | ||
116 | { | ||
117 | const char *msg, *tokstr; | ||
118 | lua_State *L; | ||
119 | va_list argp; | ||
120 | if (tok == 0) { | ||
121 | tokstr = NULL; | ||
122 | } else if (tok == CTOK_IDENT || tok == CTOK_INTEGER || tok == CTOK_STRING || | ||
123 | tok >= CTOK_FIRSTDECL) { | ||
124 | cp_save(cp, '\0'); | ||
125 | tokstr = cp->sb.buf; | ||
126 | } else { | ||
127 | tokstr = cp_tok2str(cp, tok); | ||
128 | } | ||
129 | L = cp->L; | ||
130 | va_start(argp, em); | ||
131 | msg = lj_str_pushvf(L, err2msg(em), argp); | ||
132 | va_end(argp); | ||
133 | if (tokstr) | ||
134 | msg = lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr); | ||
135 | if (cp->linenumber > 1) | ||
136 | msg = lj_str_pushf(L, "%s at line %d", msg, cp->linenumber); | ||
137 | lj_err_callermsg(L, msg); | ||
138 | } | ||
139 | |||
140 | LJ_NORET LJ_NOINLINE static void cp_err_token(CPState *cp, CPToken tok) | ||
141 | { | ||
142 | cp_errmsg(cp, cp->tok, LJ_ERR_XTOKEN, cp_tok2str(cp, tok)); | ||
143 | } | ||
144 | |||
145 | LJ_NORET LJ_NOINLINE static void cp_err_badidx(CPState *cp, CType *ct) | ||
146 | { | ||
147 | GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); | ||
148 | cp_errmsg(cp, 0, LJ_ERR_FFI_BADIDX, strdata(s)); | ||
149 | } | ||
150 | |||
151 | LJ_NORET LJ_NOINLINE static void cp_err(CPState *cp, ErrMsg em) | ||
152 | { | ||
153 | cp_errmsg(cp, 0, em); | ||
154 | } | ||
155 | |||
156 | /* -- Main lexical scanner ------------------------------------------------ */ | ||
157 | |||
158 | /* Parse integer literal. */ | ||
159 | static CPToken cp_integer(CPState *cp) | ||
160 | { | ||
161 | uint32_t n = 0; | ||
162 | cp->val.id = CTID_INT32; | ||
163 | if (cp->c != '0') { /* Decimal. */ | ||
164 | do { | ||
165 | n = n*10 + (cp->c - '0'); | ||
166 | } while (lj_char_isdigit(cp_get(cp))); | ||
167 | } else if ((cp_get(cp)& ~0x20) == 'X') { /* Hexadecimal. */ | ||
168 | if (!lj_char_isxdigit(cp_get(cp))) | ||
169 | cp_err(cp, LJ_ERR_XNUMBER); | ||
170 | do { | ||
171 | n = n*16 + (cp->c & 15); | ||
172 | if (!lj_char_isdigit(cp->c)) n += 9; | ||
173 | } while (lj_char_isxdigit(cp_get(cp))); | ||
174 | if (n >= 0x80000000u) cp->val.id = CTID_UINT32; | ||
175 | } else { /* Octal. */ | ||
176 | while (cp->c >= '0' && cp->c <= '7') { | ||
177 | n = n*8 + (cp->c - '0'); | ||
178 | cp_get(cp); | ||
179 | } | ||
180 | if (n >= 0x80000000u) cp->val.id = CTID_UINT32; | ||
181 | } | ||
182 | cp->val.u32 = n; | ||
183 | for (;;) { /* Parse suffixes. */ | ||
184 | if ((cp->c & ~0x20) == 'U') | ||
185 | cp->val.id = CTID_UINT32; | ||
186 | else if ((cp->c & ~0x20) != 'L') | ||
187 | break; | ||
188 | cp_get(cp); | ||
189 | } | ||
190 | if (lj_char_isident(cp->c) && !(cp->mode & CPARSE_MODE_SKIP)) | ||
191 | cp_errmsg(cp, cp->c, LJ_ERR_XNUMBER); | ||
192 | return CTOK_INTEGER; | ||
193 | } | ||
194 | |||
195 | /* Parse identifier or keyword. */ | ||
196 | static CPToken cp_ident(CPState *cp) | ||
197 | { | ||
198 | do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); | ||
199 | cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); | ||
200 | cp->val.id = lj_ctype_getname(cp->cts, &cp->ct, cp->str, cp->tmask); | ||
201 | if (ctype_type(cp->ct->info) == CT_KW) | ||
202 | return ctype_cid(cp->ct->info); | ||
203 | return CTOK_IDENT; | ||
204 | } | ||
205 | |||
206 | /* Parse string or character constant. */ | ||
207 | static CPToken cp_string(CPState *cp) | ||
208 | { | ||
209 | CPChar delim = cp->c; | ||
210 | cp_get(cp); | ||
211 | while (cp->c != delim) { | ||
212 | CPChar c = cp->c; | ||
213 | if (c == '\0') cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); | ||
214 | if (c == '\\') { | ||
215 | c = cp_get(cp); | ||
216 | switch (c) { | ||
217 | case '\0': cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); break; | ||
218 | case 'a': c = '\a'; break; | ||
219 | case 'b': c = '\b'; break; | ||
220 | case 'f': c = '\f'; break; | ||
221 | case 'n': c = '\n'; break; | ||
222 | case 'r': c = '\r'; break; | ||
223 | case 't': c = '\t'; break; | ||
224 | case 'v': c = '\v'; break; | ||
225 | case 'e': c = 27; break; | ||
226 | case 'x': | ||
227 | c = 0; | ||
228 | while (lj_char_isxdigit(cp_get(cp))) | ||
229 | c = (c<<4) + (lj_char_isdigit(cp->c) ? cp->c-'0' : (cp->c&15)+9); | ||
230 | cp_save(cp, (c & 0xff)); | ||
231 | continue; | ||
232 | default: | ||
233 | if (lj_char_isdigit(c)) { | ||
234 | c -= '0'; | ||
235 | if (lj_char_isdigit(cp_get(cp))) { | ||
236 | c = c*8 + (cp->c - '0'); | ||
237 | if (lj_char_isdigit(cp_get(cp))) { | ||
238 | c = c*8 + (cp->c - '0'); | ||
239 | cp_get(cp); | ||
240 | } | ||
241 | } | ||
242 | cp_save(cp, (c & 0xff)); | ||
243 | continue; | ||
244 | } | ||
245 | break; | ||
246 | } | ||
247 | } | ||
248 | cp_save(cp, c); | ||
249 | cp_get(cp); | ||
250 | } | ||
251 | cp_get(cp); | ||
252 | if (delim == '"') { | ||
253 | cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); | ||
254 | return CTOK_STRING; | ||
255 | } else { | ||
256 | if (cp->sb.n != 1) cp_err_token(cp, '\''); | ||
257 | cp->val.i32 = (int32_t)(char)cp->sb.buf[0]; | ||
258 | cp->val.id = CTID_INT32; | ||
259 | return CTOK_INTEGER; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | /* Skip C comment. */ | ||
264 | static void cp_comment_c(CPState *cp) | ||
265 | { | ||
266 | do { | ||
267 | if (cp_get(cp) == '*') { | ||
268 | do { | ||
269 | if (cp_get(cp) == '/') { cp_get(cp); return; } | ||
270 | } while (cp->c == '*'); | ||
271 | } | ||
272 | if (cp_iseol(cp->c)) cp_newline(cp); | ||
273 | } while (cp->c != '\0'); | ||
274 | } | ||
275 | |||
276 | /* Skip C++ comment. */ | ||
277 | static void cp_comment_cpp(CPState *cp) | ||
278 | { | ||
279 | while (!cp_iseol(cp_get(cp)) && cp->c != '\0') | ||
280 | ; | ||
281 | } | ||
282 | |||
283 | /* Lexical scanner for C. Only a minimal subset is implemented. */ | ||
284 | static CPToken cp_next_(CPState *cp) | ||
285 | { | ||
286 | lj_str_resetbuf(&cp->sb); | ||
287 | for (;;) { | ||
288 | if (lj_char_isident(cp->c)) | ||
289 | return lj_char_isdigit(cp->c) ? cp_integer(cp) : cp_ident(cp); | ||
290 | switch (cp->c) { | ||
291 | case '\n': case '\r': cp_newline(cp); /* fallthrough. */ | ||
292 | case ' ': case '\t': case '\v': case '\f': cp_get(cp); break; | ||
293 | case '"': case '\'': return cp_string(cp); | ||
294 | case '/': | ||
295 | cp_get(cp); | ||
296 | if (cp->c == '*') cp_comment_c(cp); | ||
297 | else if (cp->c == '/') cp_comment_cpp(cp); | ||
298 | else return '/'; | ||
299 | break; | ||
300 | case '|': | ||
301 | cp_get(cp); if (cp->c != '|') return '|'; cp_get(cp); return CTOK_OROR; | ||
302 | case '&': | ||
303 | cp_get(cp); if (cp->c != '&') return '&'; cp_get(cp); return CTOK_ANDAND; | ||
304 | case '=': | ||
305 | cp_get(cp); if (cp->c != '=') return '='; cp_get(cp); return CTOK_EQ; | ||
306 | case '!': | ||
307 | cp_get(cp); if (cp->c != '=') return '!'; cp_get(cp); return CTOK_NE; | ||
308 | case '<': | ||
309 | cp_get(cp); | ||
310 | if (cp->c == '=') { cp_get(cp); return CTOK_LE; } | ||
311 | else if (cp->c == '<') { cp_get(cp); return CTOK_SHL; } | ||
312 | return '<'; | ||
313 | case '>': | ||
314 | cp_get(cp); | ||
315 | if (cp->c == '=') { cp_get(cp); return CTOK_GE; } | ||
316 | else if (cp->c == '>') { cp_get(cp); return CTOK_SHR; } | ||
317 | return '>'; | ||
318 | case '-': | ||
319 | cp_get(cp); if (cp->c != '>') return '-'; cp_get(cp); return CTOK_DEREF; | ||
320 | case '\0': return CTOK_EOF; | ||
321 | default: { CPToken c = cp->c; cp_get(cp); return c; } | ||
322 | } | ||
323 | } | ||
324 | } | ||
325 | |||
326 | static LJ_NOINLINE CPToken cp_next(CPState *cp) | ||
327 | { | ||
328 | return (cp->tok = cp_next_(cp)); | ||
329 | } | ||
330 | |||
331 | /* -- C parser ------------------------------------------------------------ */ | ||
332 | |||
333 | /* Namespaces for resolving identifiers. */ | ||
334 | #define CPNS_DEFAULT \ | ||
335 | ((1u<<CT_KW)|(1u<<CT_TYPEDEF)|(1u<<CT_FUNC)|(1u<<CT_EXTERN)|(1u<<CT_CONSTVAL)) | ||
336 | #define CPNS_STRUCT ((1u<<CT_KW)|(1u<<CT_STRUCT)|(1u<<CT_ENUM)) | ||
337 | |||
338 | typedef CTypeID CPDeclIdx; /* Index into declaration stack. */ | ||
339 | typedef uint32_t CPscl; /* Storage class flags. */ | ||
340 | |||
341 | /* Type declaration context. */ | ||
342 | typedef struct CPDecl { | ||
343 | CPDeclIdx top; /* Top of declaration stack. */ | ||
344 | CPDeclIdx pos; /* Insertion position in declaration chain. */ | ||
345 | CPDeclIdx specpos; /* Saved position for declaration specifier. */ | ||
346 | uint32_t mode; /* Declarator mode. */ | ||
347 | CPState *cp; /* C parser state. */ | ||
348 | GCstr *name; /* Name of declared identifier (if direct). */ | ||
349 | GCstr *redir; /* Redirected symbol name. */ | ||
350 | CTypeID nameid; /* Existing typedef for declared identifier. */ | ||
351 | CTInfo attr; /* Attributes. */ | ||
352 | CTInfo fattr; /* Function attributes. */ | ||
353 | CTInfo specattr; /* Saved attributes. */ | ||
354 | CTInfo specfattr; /* Saved function attributes. */ | ||
355 | CTSize bits; /* Field size in bits (if any). */ | ||
356 | CType stack[CPARSE_MAX_DECLSTACK]; /* Type declaration stack. */ | ||
357 | } CPDecl; | ||
358 | |||
359 | /* Forward declarations. */ | ||
360 | static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl); | ||
361 | static void cp_declarator(CPState *cp, CPDecl *decl); | ||
362 | static CTypeID cp_decl_abstract(CPState *cp); | ||
363 | |||
364 | /* Initialize C parser state. Caller must set up: L, p, srcname, mode. */ | ||
365 | static void cp_init(CPState *cp) | ||
366 | { | ||
367 | cp->linenumber = 1; | ||
368 | cp->depth = 0; | ||
369 | cp->curpack = 0; | ||
370 | cp->packstack[0] = 255; | ||
371 | lj_str_initbuf(&cp->sb); | ||
372 | lj_str_resizebuf(cp->L, &cp->sb, LJ_MIN_SBUF); | ||
373 | lua_assert(cp->p != NULL); | ||
374 | cp_get(cp); /* Read-ahead first char. */ | ||
375 | cp->tok = 0; | ||
376 | cp->tmask = CPNS_DEFAULT; | ||
377 | cp_next(cp); /* Read-ahead first token. */ | ||
378 | } | ||
379 | |||
380 | /* Cleanup C parser state. */ | ||
381 | static void cp_cleanup(CPState *cp) | ||
382 | { | ||
383 | global_State *g = G(cp->L); | ||
384 | lj_str_freebuf(g, &cp->sb); | ||
385 | } | ||
386 | |||
387 | /* Check and consume optional token. */ | ||
388 | static int cp_opt(CPState *cp, CPToken tok) | ||
389 | { | ||
390 | if (cp->tok == tok) { cp_next(cp); return 1; } | ||
391 | return 0; | ||
392 | } | ||
393 | |||
394 | /* Check and consume token. */ | ||
395 | static void cp_check(CPState *cp, CPToken tok) | ||
396 | { | ||
397 | if (cp->tok != tok) cp_err_token(cp, tok); | ||
398 | cp_next(cp); | ||
399 | } | ||
400 | |||
401 | /* Check if the next token may start a type declaration. */ | ||
402 | static int cp_istypedecl(CPState *cp) | ||
403 | { | ||
404 | if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECL) return 1; | ||
405 | if (cp->tok == CTOK_IDENT && ctype_istypedef(cp->ct->info)) return 1; | ||
406 | return 0; | ||
407 | } | ||
408 | |||
409 | /* -- Constant expression evaluator --------------------------------------- */ | ||
410 | |||
411 | /* Forward declarations. */ | ||
412 | static void cp_expr_unary(CPState *cp, CPValue *k); | ||
413 | static void cp_expr_sub(CPState *cp, CPValue *k, int pri); | ||
414 | |||
415 | /* Please note that type handling is very weak here. Most ops simply | ||
416 | ** assume integer operands. Accessors are only needed to compute types and | ||
417 | ** return synthetic values. The only purpose of the expression evaluator | ||
418 | ** is to compute the values of constant expressions one would typically | ||
419 | ** find in C header files. And again: this is NOT a validating C parser! | ||
420 | */ | ||
421 | |||
422 | /* Parse comma separated expression and return last result. */ | ||
423 | static void cp_expr_comma(CPState *cp, CPValue *k) | ||
424 | { | ||
425 | do { cp_expr_sub(cp, k, 0); } while (cp_opt(cp, ',')); | ||
426 | } | ||
427 | |||
428 | /* Parse sizeof/alignof operator. */ | ||
429 | static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz) | ||
430 | { | ||
431 | CTSize sz; | ||
432 | CTInfo info; | ||
433 | if (cp_opt(cp, '(')) { | ||
434 | if (cp_istypedecl(cp)) | ||
435 | k->id = cp_decl_abstract(cp); | ||
436 | else | ||
437 | cp_expr_comma(cp, k); | ||
438 | cp_check(cp, ')'); | ||
439 | } else { | ||
440 | cp_expr_unary(cp, k); | ||
441 | } | ||
442 | info = lj_ctype_info(cp->cts, k->id, &sz); | ||
443 | if (wantsz) { | ||
444 | if (sz != CTSIZE_INVALID) | ||
445 | k->u32 = sz; | ||
446 | else if (k->id != CTID_A_CCHAR) /* Special case for sizeof("string"). */ | ||
447 | cp_err(cp, LJ_ERR_FFI_INVSIZE); | ||
448 | } else { | ||
449 | k->u32 = 1u << ctype_align(info); | ||
450 | } | ||
451 | k->id = CTID_UINT32; /* Really size_t. */ | ||
452 | } | ||
453 | |||
454 | /* Parse prefix operators. */ | ||
455 | static void cp_expr_prefix(CPState *cp, CPValue *k) | ||
456 | { | ||
457 | if (cp->tok == CTOK_INTEGER) { | ||
458 | *k = cp->val; cp_next(cp); | ||
459 | } else if (cp_opt(cp, '+')) { | ||
460 | cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */ | ||
461 | } else if (cp_opt(cp, '-')) { | ||
462 | cp_expr_unary(cp, k); k->i32 = -k->i32; | ||
463 | } else if (cp_opt(cp, '~')) { | ||
464 | cp_expr_unary(cp, k); k->i32 = ~k->i32; | ||
465 | } else if (cp_opt(cp, '!')) { | ||
466 | cp_expr_unary(cp, k); k->i32 = !k->i32; k->id = CTID_INT32; | ||
467 | } else if (cp_opt(cp, '(')) { | ||
468 | if (cp_istypedecl(cp)) { /* Cast operator. */ | ||
469 | CTypeID id = cp_decl_abstract(cp); | ||
470 | cp_check(cp, ')'); | ||
471 | cp_expr_unary(cp, k); | ||
472 | k->id = id; /* No conversion performed. */ | ||
473 | } else { /* Sub-expression. */ | ||
474 | cp_expr_comma(cp, k); | ||
475 | cp_check(cp, ')'); | ||
476 | } | ||
477 | } else if (cp_opt(cp, '*')) { /* Indirection. */ | ||
478 | CType *ct; | ||
479 | cp_expr_unary(cp, k); | ||
480 | ct = lj_ctype_rawref(cp->cts, k->id); | ||
481 | if (!ctype_ispointer(ct->info)) | ||
482 | cp_err_badidx(cp, ct); | ||
483 | k->u32 = 0; k->id = ctype_cid(ct->info); | ||
484 | } else if (cp_opt(cp, '&')) { /* Address operator. */ | ||
485 | cp_expr_unary(cp, k); | ||
486 | k->id = lj_ctype_intern(cp->cts, CTINFO(CT_PTR, CTALIGN_PTR+k->id), | ||
487 | CTSIZE_PTR); | ||
488 | } else if (cp_opt(cp, CTOK_SIZEOF)) { | ||
489 | cp_expr_sizeof(cp, k, 1); | ||
490 | } else if (cp_opt(cp, CTOK_ALIGNOF)) { | ||
491 | cp_expr_sizeof(cp, k, 0); | ||
492 | } else if (cp->tok == CTOK_IDENT) { | ||
493 | if (ctype_type(cp->ct->info) == CT_CONSTVAL) { | ||
494 | k->u32 = cp->ct->size; k->id = ctype_cid(cp->ct->info); | ||
495 | } else if (ctype_type(cp->ct->info) == CT_EXTERN) { | ||
496 | k->u32 = cp->val.id; k->id = ctype_cid(cp->ct->info); | ||
497 | } else if (ctype_type(cp->ct->info) == CT_FUNC) { | ||
498 | k->u32 = cp->val.id; k->id = cp->val.id; | ||
499 | } else { | ||
500 | goto err_expr; | ||
501 | } | ||
502 | cp_next(cp); | ||
503 | } else if (cp->tok == CTOK_STRING) { | ||
504 | CTSize sz = cp->str->len; | ||
505 | while (cp_next(cp) == CTOK_STRING) | ||
506 | sz += cp->str->len; | ||
507 | k->u32 = sz + 1; | ||
508 | k->id = CTID_A_CCHAR; | ||
509 | } else { | ||
510 | err_expr: | ||
511 | cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); | ||
512 | } | ||
513 | } | ||
514 | |||
515 | /* Parse postfix operators. */ | ||
516 | static void cp_expr_postfix(CPState *cp, CPValue *k) | ||
517 | { | ||
518 | for (;;) { | ||
519 | CType *ct; | ||
520 | if (cp_opt(cp, '[')) { /* Array/pointer index. */ | ||
521 | CPValue k2; | ||
522 | cp_expr_comma(cp, &k2); | ||
523 | ct = lj_ctype_rawref(cp->cts, k->id); | ||
524 | if (!ctype_ispointer(ct->info)) { | ||
525 | ct = lj_ctype_rawref(cp->cts, k2.id); | ||
526 | if (!ctype_ispointer(ct->info)) | ||
527 | cp_err_badidx(cp, ct); | ||
528 | } | ||
529 | cp_check(cp, ']'); | ||
530 | k->u32 = 0; | ||
531 | } else if (cp->tok == '.' || cp->tok == CTOK_DEREF) { /* Struct deref. */ | ||
532 | CTSize ofs; | ||
533 | CType *fct; | ||
534 | ct = lj_ctype_rawref(cp->cts, k->id); | ||
535 | if (cp->tok == CTOK_DEREF) { | ||
536 | if (!ctype_ispointer(ct->info)) | ||
537 | cp_err_badidx(cp, ct); | ||
538 | ct = lj_ctype_rawref(cp->cts, ctype_cid(ct->info)); | ||
539 | } | ||
540 | cp_next(cp); | ||
541 | if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); | ||
542 | if (!ctype_isstruct(ct->info) || ct->size == CTSIZE_INVALID || | ||
543 | !(fct = lj_ctype_getfield(cp->cts, ct, cp->str, &ofs)) || | ||
544 | ctype_isbitfield(fct->info)) { | ||
545 | GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); | ||
546 | cp_errmsg(cp, 0, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(cp->str)); | ||
547 | } | ||
548 | ct = fct; | ||
549 | k->u32 = ctype_isconstval(ct->info) ? ct->size : 0; | ||
550 | cp_next(cp); | ||
551 | } else { | ||
552 | return; | ||
553 | } | ||
554 | k->id = ctype_cid(ct->info); | ||
555 | } | ||
556 | } | ||
557 | |||
558 | /* Parse infix operators. */ | ||
559 | static void cp_expr_infix(CPState *cp, CPValue *k, int pri) | ||
560 | { | ||
561 | CPValue k2; | ||
562 | k2.u32 = 0; k2.id = 0; /* Silence the compiler. */ | ||
563 | for (;;) { | ||
564 | switch (pri) { | ||
565 | case 0: | ||
566 | if (cp_opt(cp, '?')) { | ||
567 | CPValue k3; | ||
568 | cp_expr_comma(cp, &k2); /* Right-associative. */ | ||
569 | cp_check(cp, ':'); | ||
570 | cp_expr_sub(cp, &k3, 0); | ||
571 | k->u32 = k->u32 ? k2.u32 : k3.u32; | ||
572 | k->id = k2.id > k3.id ? k2.id : k3.id; | ||
573 | continue; | ||
574 | } | ||
575 | case 1: | ||
576 | if (cp_opt(cp, CTOK_OROR)) { | ||
577 | cp_expr_sub(cp, &k2, 2); k->i32 = k->u32 || k2.u32; k->id = CTID_INT32; | ||
578 | continue; | ||
579 | } | ||
580 | case 2: | ||
581 | if (cp_opt(cp, CTOK_ANDAND)) { | ||
582 | cp_expr_sub(cp, &k2, 3); k->i32 = k->u32 && k2.u32; k->id = CTID_INT32; | ||
583 | continue; | ||
584 | } | ||
585 | case 3: | ||
586 | if (cp_opt(cp, '|')) { | ||
587 | cp_expr_sub(cp, &k2, 4); k->u32 = k->u32 | k2.u32; goto arith_result; | ||
588 | } | ||
589 | case 4: | ||
590 | if (cp_opt(cp, '^')) { | ||
591 | cp_expr_sub(cp, &k2, 5); k->u32 = k->u32 ^ k2.u32; goto arith_result; | ||
592 | } | ||
593 | case 5: | ||
594 | if (cp_opt(cp, '&')) { | ||
595 | cp_expr_sub(cp, &k2, 6); k->u32 = k->u32 & k2.u32; goto arith_result; | ||
596 | } | ||
597 | case 6: | ||
598 | if (cp_opt(cp, CTOK_EQ)) { | ||
599 | cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 == k2.u32; k->id = CTID_INT32; | ||
600 | continue; | ||
601 | } else if (cp_opt(cp, CTOK_NE)) { | ||
602 | cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 != k2.u32; k->id = CTID_INT32; | ||
603 | continue; | ||
604 | } | ||
605 | case 7: | ||
606 | if (cp_opt(cp, '<')) { | ||
607 | cp_expr_sub(cp, &k2, 8); | ||
608 | if (k->id == CTID_INT32 && k2.id == CTID_INT32) | ||
609 | k->i32 = k->i32 < k2.i32; | ||
610 | else | ||
611 | k->i32 = k->u32 < k2.u32; | ||
612 | k->id = CTID_INT32; | ||
613 | continue; | ||
614 | } else if (cp_opt(cp, '>')) { | ||
615 | cp_expr_sub(cp, &k2, 8); | ||
616 | if (k->id == CTID_INT32 && k2.id == CTID_INT32) | ||
617 | k->i32 = k->i32 > k2.i32; | ||
618 | else | ||
619 | k->i32 = k->u32 > k2.u32; | ||
620 | k->id = CTID_INT32; | ||
621 | continue; | ||
622 | } else if (cp_opt(cp, CTOK_LE)) { | ||
623 | cp_expr_sub(cp, &k2, 8); | ||
624 | if (k->id == CTID_INT32 && k2.id == CTID_INT32) | ||
625 | k->i32 = k->i32 <= k2.i32; | ||
626 | else | ||
627 | k->i32 = k->u32 <= k2.u32; | ||
628 | k->id = CTID_INT32; | ||
629 | continue; | ||
630 | } else if (cp_opt(cp, CTOK_GE)) { | ||
631 | cp_expr_sub(cp, &k2, 8); | ||
632 | if (k->id == CTID_INT32 && k2.id == CTID_INT32) | ||
633 | k->i32 = k->i32 >= k2.i32; | ||
634 | else | ||
635 | k->i32 = k->u32 >= k2.u32; | ||
636 | k->id = CTID_INT32; | ||
637 | continue; | ||
638 | } | ||
639 | case 8: | ||
640 | if (cp_opt(cp, CTOK_SHL)) { | ||
641 | cp_expr_sub(cp, &k2, 9); k->u32 = k->u32 << k2.u32; | ||
642 | continue; | ||
643 | } else if (cp_opt(cp, CTOK_SHR)) { | ||
644 | cp_expr_sub(cp, &k2, 9); | ||
645 | if (k->id == CTID_INT32) | ||
646 | k->i32 = k->i32 >> k2.i32; | ||
647 | else | ||
648 | k->u32 = k->u32 >> k2.u32; | ||
649 | continue; | ||
650 | } | ||
651 | case 9: | ||
652 | if (cp_opt(cp, '+')) { | ||
653 | cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 + k2.u32; | ||
654 | arith_result: | ||
655 | if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ | ||
656 | continue; | ||
657 | } else if (cp_opt(cp, '-')) { | ||
658 | cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 - k2.u32; goto arith_result; | ||
659 | } | ||
660 | case 10: | ||
661 | if (cp_opt(cp, '*')) { | ||
662 | cp_expr_unary(cp, &k2); k->u32 = k->u32 * k2.u32; goto arith_result; | ||
663 | } else if (cp_opt(cp, '/')) { | ||
664 | cp_expr_unary(cp, &k2); | ||
665 | if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ | ||
666 | if (k2.u32 == 0 || | ||
667 | (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) | ||
668 | cp_err(cp, LJ_ERR_BADVAL); | ||
669 | if (k->id == CTID_INT32) | ||
670 | k->i32 = k->i32 / k2.i32; | ||
671 | else | ||
672 | k->u32 = k->u32 / k2.u32; | ||
673 | continue; | ||
674 | } else if (cp_opt(cp, '%')) { | ||
675 | cp_expr_unary(cp, &k2); | ||
676 | if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ | ||
677 | if (k2.u32 == 0 || | ||
678 | (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) | ||
679 | cp_err(cp, LJ_ERR_BADVAL); | ||
680 | if (k->id == CTID_INT32) | ||
681 | k->i32 = k->i32 % k2.i32; | ||
682 | else | ||
683 | k->u32 = k->u32 % k2.u32; | ||
684 | continue; | ||
685 | } | ||
686 | default: | ||
687 | return; | ||
688 | } | ||
689 | } | ||
690 | } | ||
691 | |||
692 | /* Parse and evaluate unary expression. */ | ||
693 | static void cp_expr_unary(CPState *cp, CPValue *k) | ||
694 | { | ||
695 | if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); | ||
696 | cp_expr_prefix(cp, k); | ||
697 | cp_expr_postfix(cp, k); | ||
698 | cp->depth--; | ||
699 | } | ||
700 | |||
701 | /* Parse and evaluate sub-expression. */ | ||
702 | static void cp_expr_sub(CPState *cp, CPValue *k, int pri) | ||
703 | { | ||
704 | cp_expr_unary(cp, k); | ||
705 | cp_expr_infix(cp, k, pri); | ||
706 | } | ||
707 | |||
708 | /* Parse constant integer expression. */ | ||
709 | static void cp_expr_kint(CPState *cp, CPValue *k) | ||
710 | { | ||
711 | CType *ct; | ||
712 | cp_expr_sub(cp, k, 0); | ||
713 | ct = ctype_raw(cp->cts, k->id); | ||
714 | if (!ctype_isinteger(ct->info)) cp_err(cp, LJ_ERR_BADVAL); | ||
715 | } | ||
716 | |||
717 | /* Parse (non-negative) size expression. */ | ||
718 | static CTSize cp_expr_ksize(CPState *cp) | ||
719 | { | ||
720 | CPValue k; | ||
721 | cp_expr_kint(cp, &k); | ||
722 | if (k.u32 >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); | ||
723 | return k.u32; | ||
724 | } | ||
725 | |||
726 | /* -- Type declaration stack management ----------------------------------- */ | ||
727 | |||
728 | /* Add declaration element behind the insertion position. */ | ||
729 | static CPDeclIdx cp_add(CPDecl *decl, CTInfo info, CTSize size) | ||
730 | { | ||
731 | CPDeclIdx top = decl->top; | ||
732 | if (top >= CPARSE_MAX_DECLSTACK) cp_err(decl->cp, LJ_ERR_XLEVELS); | ||
733 | decl->stack[top].info = info; | ||
734 | decl->stack[top].size = size; | ||
735 | decl->stack[top].sib = 0; | ||
736 | setgcrefnull(decl->stack[top].name); | ||
737 | decl->stack[top].next = decl->stack[decl->pos].next; | ||
738 | decl->stack[decl->pos].next = (CTypeID1)top; | ||
739 | decl->top = top+1; | ||
740 | return top; | ||
741 | } | ||
742 | |||
743 | /* Push declaration element before the insertion position. */ | ||
744 | static CPDeclIdx cp_push(CPDecl *decl, CTInfo info, CTSize size) | ||
745 | { | ||
746 | return (decl->pos = cp_add(decl, info, size)); | ||
747 | } | ||
748 | |||
749 | /* Push or merge attributes. */ | ||
750 | static void cp_push_attributes(CPDecl *decl) | ||
751 | { | ||
752 | CType *ct = &decl->stack[decl->pos]; | ||
753 | if (ctype_isfunc(ct->info)) { /* Ok to modify in-place. */ | ||
754 | #if LJ_TARGET_X86 | ||
755 | if ((decl->fattr & CTFP_CCONV)) | ||
756 | ct->info = (ct->info & (CTMASK_NUM|CTF_VARARG|CTMASK_CID)) + | ||
757 | (decl->fattr & ~CTMASK_CID); | ||
758 | #endif | ||
759 | } else { | ||
760 | if ((decl->attr & CTFP_ALIGNED) && !(decl->mode & CPARSE_MODE_FIELD)) | ||
761 | cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_ALIGN)), | ||
762 | ctype_align(decl->attr)); | ||
763 | } | ||
764 | } | ||
765 | |||
766 | /* Push unrolled type to declaration stack and merge qualifiers. */ | ||
767 | static void cp_push_type(CPDecl *decl, CTypeID id) | ||
768 | { | ||
769 | CType *ct = ctype_get(decl->cp->cts, id); | ||
770 | CTInfo info = ct->info; | ||
771 | CTSize size = ct->size; | ||
772 | switch (ctype_type(info)) { | ||
773 | case CT_STRUCT: case CT_ENUM: | ||
774 | cp_push(decl, CTINFO(CT_TYPEDEF, id), 0); /* Don't copy unique types. */ | ||
775 | if ((decl->attr & CTF_QUAL)) { /* Push unmerged qualifiers. */ | ||
776 | cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_QUAL)), | ||
777 | (decl->attr & CTF_QUAL)); | ||
778 | decl->attr &= ~CTF_QUAL; | ||
779 | } | ||
780 | break; | ||
781 | case CT_ATTRIB: | ||
782 | if (ctype_isxattrib(info, CTA_QUAL)) | ||
783 | decl->attr &= ~size; /* Remove redundant qualifiers. */ | ||
784 | cp_push_type(decl, ctype_cid(info)); /* Unroll. */ | ||
785 | cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ | ||
786 | break; | ||
787 | case CT_ARRAY: | ||
788 | cp_push_type(decl, ctype_cid(info)); /* Unroll. */ | ||
789 | cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ | ||
790 | decl->stack[decl->pos].sib = 1; /* Mark as already checked and sized. */ | ||
791 | /* Note: this is not copied to the ct->sib in the C type table. */ | ||
792 | break; | ||
793 | case CT_FUNC: | ||
794 | /* Copy type, link parameters (shared). */ | ||
795 | decl->stack[cp_push(decl, info, size)].sib = ct->sib; | ||
796 | break; | ||
797 | default: | ||
798 | /* Copy type, merge common qualifiers. */ | ||
799 | cp_push(decl, info|(decl->attr & CTF_QUAL), size); | ||
800 | decl->attr &= ~CTF_QUAL; | ||
801 | break; | ||
802 | } | ||
803 | } | ||
804 | |||
805 | /* Consume the declaration element chain and intern the C type. */ | ||
806 | static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl) | ||
807 | { | ||
808 | CTypeID id = 0; | ||
809 | CPDeclIdx idx = 0; | ||
810 | CTSize csize = CTSIZE_INVALID; | ||
811 | CTSize cinfo = 0; | ||
812 | do { | ||
813 | CType *ct = &decl->stack[idx]; | ||
814 | CTInfo info = ct->info; | ||
815 | CTInfo size = ct->size; | ||
816 | /* The cid is already part of info for copies of pointers/functions. */ | ||
817 | idx = ct->next; | ||
818 | if (ctype_istypedef(info)) { | ||
819 | lua_assert(id == 0); | ||
820 | id = ctype_cid(info); | ||
821 | /* Always refetch info/size, since struct/enum may have been completed. */ | ||
822 | cinfo = ctype_get(cp->cts, id)->info; | ||
823 | csize = ctype_get(cp->cts, id)->size; | ||
824 | lua_assert(ctype_isstruct(cinfo) || ctype_isenum(cinfo)); | ||
825 | } else if (ctype_isfunc(info)) { /* Intern function. */ | ||
826 | CType *fct; | ||
827 | CTypeID fid; | ||
828 | CTypeID sib; | ||
829 | if (id) { | ||
830 | CType *refct = ctype_raw(cp->cts, id); | ||
831 | /* Reject function or refarray return types. */ | ||
832 | if (ctype_isfunc(refct->info) || ctype_isrefarray(refct->info)) | ||
833 | cp_err(cp, LJ_ERR_FFI_INVTYPE); | ||
834 | } | ||
835 | /* No intervening attributes allowed, skip forward. */ | ||
836 | while (idx) { | ||
837 | CType *ctn = &decl->stack[idx]; | ||
838 | if (!ctype_isattrib(ctn->info)) break; | ||
839 | idx = ctn->next; /* Skip attribute. */ | ||
840 | } | ||
841 | sib = ct->sib; /* Next line may reallocate the C type table. */ | ||
842 | fid = lj_ctype_new(cp->cts, &fct); | ||
843 | csize = CTSIZE_INVALID; | ||
844 | fct->info = cinfo = info + id; | ||
845 | fct->size = size; | ||
846 | fct->sib = sib; | ||
847 | id = fid; | ||
848 | } else if (ctype_isattrib(info)) { | ||
849 | if (ctype_isxattrib(info, CTA_QUAL)) | ||
850 | cinfo |= size; | ||
851 | else if (ctype_isxattrib(info, CTA_ALIGN)) | ||
852 | CTF_INSERT(cinfo, ALIGN, size); | ||
853 | id = lj_ctype_intern(cp->cts, info+id, size); | ||
854 | /* Inherit csize/cinfo from original type. */ | ||
855 | } else { | ||
856 | if (ctype_isnum(info)) { /* Handle mode/vector-size attributes. */ | ||
857 | lua_assert(id == 0); | ||
858 | if (!(info & CTF_BOOL)) { | ||
859 | CTSize msize = ctype_msizeP(decl->attr); | ||
860 | CTSize vsize = ctype_vsizeP(decl->attr); | ||
861 | if (msize && (!(info & CTF_FP) || (msize == 4 || msize == 8))) { | ||
862 | CTSize malign = lj_fls(msize); | ||
863 | if (malign > 4) malign = 4; /* Limit alignment. */ | ||
864 | CTF_INSERT(info, ALIGN, malign); | ||
865 | size = msize; /* Override size via mode. */ | ||
866 | } | ||
867 | if (vsize) { /* Vector size set? */ | ||
868 | CTSize esize = lj_fls(size); | ||
869 | if (vsize >= esize) { | ||
870 | /* Intern the element type first. */ | ||
871 | id = lj_ctype_intern(cp->cts, info, size); | ||
872 | /* Then create a vector (array) with vsize alignment. */ | ||
873 | size = (1u << vsize); | ||
874 | if (vsize > 4) vsize = 4; /* Limit alignment. */ | ||
875 | if (ctype_align(info) > vsize) vsize = ctype_align(info); | ||
876 | info = CTINFO(CT_ARRAY, (info & CTF_QUAL) + CTF_VECTOR + | ||
877 | CTALIGN(vsize)); | ||
878 | } | ||
879 | } | ||
880 | } | ||
881 | } else if (ctype_isptr(info)) { | ||
882 | /* Reject pointer/ref to ref. */ | ||
883 | if (id && ctype_isref(ctype_raw(cp->cts, id)->info)) | ||
884 | cp_err(cp, LJ_ERR_FFI_INVTYPE); | ||
885 | if (ctype_isref(info)) { | ||
886 | info &= ~CTF_VOLATILE; /* Refs are always const, never volatile. */ | ||
887 | /* No intervening attributes allowed, skip forward. */ | ||
888 | while (idx) { | ||
889 | CType *ctn = &decl->stack[idx]; | ||
890 | if (!ctype_isattrib(ctn->info)) break; | ||
891 | idx = ctn->next; /* Skip attribute. */ | ||
892 | } | ||
893 | } | ||
894 | } else if (ctype_isarray(info)) { /* Check for valid array size etc. */ | ||
895 | if (ct->sib == 0) { /* Only check/size arrays not copied by unroll. */ | ||
896 | if (ctype_isref(cinfo)) /* Reject arrays of refs. */ | ||
897 | cp_err(cp, LJ_ERR_FFI_INVTYPE); | ||
898 | /* Reject VLS or unknown-sized types. */ | ||
899 | if (ctype_isvltype(cinfo) || csize == CTSIZE_INVALID) | ||
900 | cp_err(cp, LJ_ERR_FFI_INVSIZE); | ||
901 | /* a[] and a[?] keep their invalid size. */ | ||
902 | if (size != CTSIZE_INVALID) { | ||
903 | uint64_t xsz = (uint64_t)size * csize; | ||
904 | if (xsz >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); | ||
905 | size = (CTSize)xsz; | ||
906 | } | ||
907 | } | ||
908 | info |= (cinfo & (CTF_QUAL|CTF_ALIGN)); /* Inherit qual and align. */ | ||
909 | } else { | ||
910 | lua_assert(ctype_isvoid(info)); | ||
911 | } | ||
912 | csize = size; | ||
913 | cinfo = info+id; | ||
914 | id = lj_ctype_intern(cp->cts, info+id, size); | ||
915 | } | ||
916 | } while (idx); | ||
917 | return id; | ||
918 | } | ||
919 | |||
920 | /* -- C declaration parser ------------------------------------------------ */ | ||
921 | |||
922 | #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) | ||
923 | |||
924 | /* Reset declaration state to declaration specifier. */ | ||
925 | static void cp_decl_reset(CPDecl *decl) | ||
926 | { | ||
927 | decl->pos = decl->specpos; | ||
928 | decl->top = decl->specpos+1; | ||
929 | decl->stack[decl->specpos].next = 0; | ||
930 | decl->attr = decl->specattr; | ||
931 | decl->fattr = decl->specfattr; | ||
932 | decl->name = NULL; | ||
933 | decl->redir = NULL; | ||
934 | } | ||
935 | |||
936 | /* Parse constant initializer. */ | ||
937 | /* NYI: FP constants and strings as initializers. */ | ||
938 | static CTypeID cp_decl_constinit(CPState *cp, CType **ctp, CTypeID typeid) | ||
939 | { | ||
940 | CType *ctt = ctype_get(cp->cts, typeid); | ||
941 | CTInfo info; | ||
942 | CTSize size; | ||
943 | CPValue k; | ||
944 | CTypeID constid; | ||
945 | while (ctype_isattrib(ctt->info)) { /* Skip attributes. */ | ||
946 | typeid = ctype_cid(ctt->info); /* Update ID, too. */ | ||
947 | ctt = ctype_get(cp->cts, typeid); | ||
948 | } | ||
949 | info = ctt->info; | ||
950 | size = ctt->size; | ||
951 | if (!ctype_isinteger(info) || !(info & CTF_CONST) || size > 4) | ||
952 | cp_err(cp, LJ_ERR_FFI_INVTYPE); | ||
953 | cp_check(cp, '='); | ||
954 | cp_expr_sub(cp, &k, 0); | ||
955 | constid = lj_ctype_new(cp->cts, ctp); | ||
956 | (*ctp)->info = CTINFO(CT_CONSTVAL, CTF_CONST|typeid); | ||
957 | k.u32 <<= 8*(4-size); | ||
958 | if ((info & CTF_UNSIGNED)) | ||
959 | k.u32 >>= 8*(4-size); | ||
960 | else | ||
961 | k.u32 = (uint32_t)((int32_t)k.u32 >> 8*(4-size)); | ||
962 | (*ctp)->size = k.u32; | ||
963 | return constid; | ||
964 | } | ||
965 | |||
966 | /* Parse size in parentheses as part of attribute. */ | ||
967 | static CTSize cp_decl_sizeattr(CPState *cp) | ||
968 | { | ||
969 | CTSize sz; | ||
970 | uint32_t oldtmask = cp->tmask; | ||
971 | cp->tmask = CPNS_DEFAULT; /* Required for expression evaluator. */ | ||
972 | cp_check(cp, '('); | ||
973 | sz = cp_expr_ksize(cp); | ||
974 | cp->tmask = oldtmask; | ||
975 | cp_check(cp, ')'); | ||
976 | return sz; | ||
977 | } | ||
978 | |||
979 | /* Parse alignment attribute. */ | ||
980 | static void cp_decl_align(CPState *cp, CPDecl *decl) | ||
981 | { | ||
982 | CTSize al = 4; /* Unspecified alignment is 16 bytes. */ | ||
983 | if (cp->tok == '(') { | ||
984 | al = cp_decl_sizeattr(cp); | ||
985 | al = al ? lj_fls(al) : 0; | ||
986 | } | ||
987 | CTF_INSERT(decl->attr, ALIGN, al); | ||
988 | decl->attr |= CTFP_ALIGNED; | ||
989 | } | ||
990 | |||
991 | /* Parse GCC asm("name") redirect. */ | ||
992 | static void cp_decl_asm(CPState *cp, CPDecl *decl) | ||
993 | { | ||
994 | UNUSED(decl); | ||
995 | cp_next(cp); | ||
996 | cp_check(cp, '('); | ||
997 | if (cp->tok == CTOK_STRING) { | ||
998 | GCstr *str = cp->str; | ||
999 | while (cp_next(cp) == CTOK_STRING) { | ||
1000 | lj_str_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str)); | ||
1001 | cp->L->top--; | ||
1002 | str = strV(cp->L->top); | ||
1003 | } | ||
1004 | decl->redir = str; | ||
1005 | } | ||
1006 | cp_check(cp, ')'); | ||
1007 | } | ||
1008 | |||
1009 | /* Parse GCC __attribute__((mode(...))). */ | ||
1010 | static void cp_decl_mode(CPState *cp, CPDecl *decl) | ||
1011 | { | ||
1012 | cp_check(cp, '('); | ||
1013 | if (cp->tok == CTOK_IDENT) { | ||
1014 | const char *s = strdata(cp->str); | ||
1015 | CTSize sz = 0, vlen = 0; | ||
1016 | if (s[0] == '_' && s[1] == '_') s += 2; | ||
1017 | if (*s == 'V') { | ||
1018 | s++; | ||
1019 | vlen = *s++ - '0'; | ||
1020 | if (*s >= '0' && *s <= '9') | ||
1021 | vlen = vlen*10 + (*s++ - '0'); | ||
1022 | } | ||
1023 | switch (*s++) { | ||
1024 | case 'Q': sz = 1; break; | ||
1025 | case 'H': sz = 2; break; | ||
1026 | case 'S': sz = 4; break; | ||
1027 | case 'D': sz = 8; break; | ||
1028 | case 'T': sz = 16; break; | ||
1029 | case 'O': sz = 32; break; | ||
1030 | default: goto bad_size; | ||
1031 | } | ||
1032 | if (*s == 'I' || *s == 'F') { | ||
1033 | CTF_INSERT(decl->attr, MSIZEP, sz); | ||
1034 | if (vlen) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vlen*sz)); | ||
1035 | } | ||
1036 | bad_size: | ||
1037 | cp_next(cp); | ||
1038 | } | ||
1039 | cp_check(cp, ')'); | ||
1040 | } | ||
1041 | |||
1042 | /* Parse GCC __attribute__((...)). */ | ||
1043 | static void cp_decl_gccattribute(CPState *cp, CPDecl *decl) | ||
1044 | { | ||
1045 | cp_next(cp); | ||
1046 | cp_check(cp, '('); | ||
1047 | cp_check(cp, '('); | ||
1048 | while (cp->tok != ')') { | ||
1049 | if (cp->tok == CTOK_IDENT) { | ||
1050 | GCstr *attrstr = cp->str; | ||
1051 | cp_next(cp); | ||
1052 | switch (attrstr->hash) { | ||
1053 | case H_(64a9208e,8ce14319): case H_(8e6331b2,95a282af): /* aligned */ | ||
1054 | cp_decl_align(cp, decl); | ||
1055 | break; | ||
1056 | case H_(42eb47de,f0ede26c): case H_(29f48a09,cf383e0c): /* packed */ | ||
1057 | decl->attr |= CTFP_PACKED; | ||
1058 | break; | ||
1059 | case H_(0a84eef6,8dfab04c): case H_(995cf92c,d5696591): /* mode */ | ||
1060 | cp_decl_mode(cp, decl); | ||
1061 | break; | ||
1062 | case H_(0ab31997,2d5213fa): case H_(bf875611,200e9990): /* vector_size */ | ||
1063 | { | ||
1064 | CTSize vsize = cp_decl_sizeattr(cp); | ||
1065 | if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); | ||
1066 | } | ||
1067 | break; | ||
1068 | #if LJ_TARGET_X86 | ||
1069 | case H_(5ad22db8,c689b848): case H_(439150fa,65ea78cb): /* regparm */ | ||
1070 | CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); | ||
1071 | decl->fattr |= CTFP_CCONV; | ||
1072 | break; | ||
1073 | case H_(18fc0b98,7ff4c074): case H_(4e62abed,0a747424): /* cdecl */ | ||
1074 | CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); | ||
1075 | decl->fattr |= CTFP_CCONV; | ||
1076 | break; | ||
1077 | case H_(72b2e41b,494c5a44): case H_(f2356d59,f25fc9bd): /* thiscall */ | ||
1078 | CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); | ||
1079 | decl->fattr |= CTFP_CCONV; | ||
1080 | break; | ||
1081 | case H_(0d0ffc42,ab746f88): case H_(21c54ba1,7f0ca7e3): /* fastcall */ | ||
1082 | CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); | ||
1083 | decl->fattr |= CTFP_CCONV; | ||
1084 | break; | ||
1085 | case H_(ef76b040,9412e06a): case H_(de56697b,c750e6e1): /* stdcall */ | ||
1086 | CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); | ||
1087 | decl->fattr |= CTFP_CCONV; | ||
1088 | break; | ||
1089 | case H_(ea78b622,f234bd8e): case H_(252ffb06,8d50f34b): /* sseregparm */ | ||
1090 | decl->fattr |= CTF_SSEREGPARM; | ||
1091 | decl->fattr |= CTFP_CCONV; | ||
1092 | break; | ||
1093 | #endif | ||
1094 | default: /* Skip all other attributes. */ | ||
1095 | goto skip_attr; | ||
1096 | } | ||
1097 | } else if (cp->tok >= CTOK_FIRSTDECL) { /* For __attribute((const)) etc. */ | ||
1098 | cp_next(cp); | ||
1099 | skip_attr: | ||
1100 | if (cp_opt(cp, '(')) { | ||
1101 | while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); | ||
1102 | cp_check(cp, ')'); | ||
1103 | } | ||
1104 | } else { | ||
1105 | break; | ||
1106 | } | ||
1107 | if (!cp_opt(cp, ',')) break; | ||
1108 | } | ||
1109 | cp_check(cp, ')'); | ||
1110 | cp_check(cp, ')'); | ||
1111 | } | ||
1112 | |||
1113 | /* Parse MSVC __declspec(...). */ | ||
1114 | static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl) | ||
1115 | { | ||
1116 | cp_next(cp); | ||
1117 | cp_check(cp, '('); | ||
1118 | while (cp->tok == CTOK_IDENT) { | ||
1119 | GCstr *attrstr = cp->str; | ||
1120 | cp_next(cp); | ||
1121 | switch (attrstr->hash) { | ||
1122 | case H_(bc2395fa,98f267f8): /* align */ | ||
1123 | cp_decl_align(cp, decl); | ||
1124 | break; | ||
1125 | default: /* Ignore all other attributes. */ | ||
1126 | if (cp_opt(cp, '(')) { | ||
1127 | while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); | ||
1128 | cp_check(cp, ')'); | ||
1129 | } | ||
1130 | break; | ||
1131 | } | ||
1132 | } | ||
1133 | cp_check(cp, ')'); | ||
1134 | } | ||
1135 | |||
1136 | /* Parse declaration attributes (and common qualifiers). */ | ||
1137 | static void cp_decl_attributes(CPState *cp, CPDecl *decl) | ||
1138 | { | ||
1139 | for (;;) { | ||
1140 | switch (cp->tok) { | ||
1141 | case CTOK_CONST: decl->attr |= CTF_CONST; break; | ||
1142 | case CTOK_VOLATILE: decl->attr |= CTF_VOLATILE; break; | ||
1143 | case CTOK_RESTRICT: break; /* Ignore. */ | ||
1144 | case CTOK_EXTENSION: break; /* Ignore. */ | ||
1145 | case CTOK_ATTRIBUTE: cp_decl_gccattribute(cp, decl); continue; | ||
1146 | case CTOK_ASM: cp_decl_asm(cp, decl); continue; | ||
1147 | case CTOK_DECLSPEC: cp_decl_msvcattribute(cp, decl); continue; | ||
1148 | case CTOK_CCDECL: | ||
1149 | #if LJ_TARGET_X86 | ||
1150 | CTF_INSERT(decl->fattr, CCONV, cp->ct->size); | ||
1151 | decl->fattr |= CTFP_CCONV; | ||
1152 | #endif | ||
1153 | break; | ||
1154 | case CTOK_PTRSZ: | ||
1155 | #if LJ_64 | ||
1156 | CTF_INSERT(decl->attr, MSIZEP, cp->ct->size); | ||
1157 | #endif | ||
1158 | break; | ||
1159 | default: return; | ||
1160 | } | ||
1161 | cp_next(cp); | ||
1162 | } | ||
1163 | } | ||
1164 | |||
1165 | /* Parse struct/union/enum name. */ | ||
1166 | static CTypeID cp_struct_name(CPState *cp, CPDecl *sdecl, CTInfo info) | ||
1167 | { | ||
1168 | CTypeID sid; | ||
1169 | CType *ct; | ||
1170 | cp->tmask = CPNS_STRUCT; | ||
1171 | cp_next(cp); | ||
1172 | cp_decl_attributes(cp, sdecl); | ||
1173 | cp->tmask = CPNS_DEFAULT; | ||
1174 | if (cp->tok != '{') { | ||
1175 | if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); | ||
1176 | if (cp->val.id) { /* Name of existing struct/union/enum. */ | ||
1177 | sid = cp->val.id; | ||
1178 | ct = cp->ct; | ||
1179 | if ((ct->info ^ info) & (CTMASK_NUM|CTF_UNION)) /* Wrong type. */ | ||
1180 | cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); | ||
1181 | } else { /* Create named, incomplete struct/union/enum. */ | ||
1182 | if ((cp->mode & CPARSE_MODE_NOIMPLICIT)) | ||
1183 | cp_errmsg(cp, 0, LJ_ERR_FFI_BADTAG, strdata(cp->str)); | ||
1184 | sid = lj_ctype_new(cp->cts, &ct); | ||
1185 | ct->info = info; | ||
1186 | ct->size = CTSIZE_INVALID; | ||
1187 | ctype_setname(ct, cp->str); | ||
1188 | lj_ctype_addname(cp->cts, ct, sid); | ||
1189 | } | ||
1190 | cp_next(cp); | ||
1191 | } else { /* Create anonymous, incomplete struct/union/enum. */ | ||
1192 | sid = lj_ctype_new(cp->cts, &ct); | ||
1193 | ct->info = info; | ||
1194 | ct->size = CTSIZE_INVALID; | ||
1195 | } | ||
1196 | if (cp->tok == '{') { | ||
1197 | if (ct->size != CTSIZE_INVALID || ct->sib) | ||
1198 | cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); | ||
1199 | ct->sib = 1; /* Indicate the type is currently being defined. */ | ||
1200 | } | ||
1201 | return sid; | ||
1202 | } | ||
1203 | |||
1204 | /* Determine field alignment. */ | ||
1205 | static CTSize cp_field_align(CPState *cp, CType *ct, CTInfo info) | ||
1206 | { | ||
1207 | CTSize align = ctype_align(info); | ||
1208 | UNUSED(cp); UNUSED(ct); | ||
1209 | #if (LJ_TARGET_X86 && !LJ_ABI_WIN) || (LJ_TARGET_ARM && __APPLE__) | ||
1210 | /* The SYSV i386 and iOS ABIs limit alignment of non-vector fields to 2^2. */ | ||
1211 | if (align > 2 && !(info & CTFP_ALIGNED)) { | ||
1212 | if (ctype_isarray(info) && !(info & CTF_VECTOR)) { | ||
1213 | do { | ||
1214 | ct = ctype_rawchild(cp->cts, ct); | ||
1215 | info = ct->info; | ||
1216 | } while (ctype_isarray(info) && !(info & CTF_VECTOR)); | ||
1217 | } | ||
1218 | if (ctype_isnum(info) || ctype_isenum(info)) | ||
1219 | align = 2; | ||
1220 | } | ||
1221 | #endif | ||
1222 | return align; | ||
1223 | } | ||
1224 | |||
1225 | /* Layout struct/union fields. */ | ||
1226 | static void cp_struct_layout(CPState *cp, CTypeID sid, CTInfo sattr) | ||
1227 | { | ||
1228 | CTSize bofs = 0, bmaxofs = 0; /* Bit offset and max. bit offset. */ | ||
1229 | CTSize maxalign = ctype_align(sattr); | ||
1230 | CType *sct = ctype_get(cp->cts, sid); | ||
1231 | CTInfo sinfo = sct->info; | ||
1232 | CTypeID fieldid = sct->sib; | ||
1233 | while (fieldid) { | ||
1234 | CType *ct = ctype_get(cp->cts, fieldid); | ||
1235 | CTInfo attr = ct->size; /* Field declaration attributes (temp.). */ | ||
1236 | |||
1237 | if (ctype_isfield(ct->info) || | ||
1238 | (ctype_isxattrib(ct->info, CTA_SUBTYPE) && attr)) { | ||
1239 | CTSize align, amask; /* Alignment (pow2) and alignment mask (bits). */ | ||
1240 | CTSize sz; | ||
1241 | CTInfo info = lj_ctype_info(cp->cts, ctype_cid(ct->info), &sz); | ||
1242 | CTSize bsz, csz = 8*sz; /* Field size and container size (in bits). */ | ||
1243 | sinfo |= (info & (CTF_QUAL|CTF_VLA)); /* Merge pseudo-qualifiers. */ | ||
1244 | |||
1245 | /* Check for size overflow and determine alignment. */ | ||
1246 | if (sz >= 0x20000000u || bofs + csz < bofs) { | ||
1247 | if (!(sz == CTSIZE_INVALID && ctype_isarray(info) && | ||
1248 | !(sinfo & CTF_UNION))) | ||
1249 | cp_err(cp, LJ_ERR_FFI_INVSIZE); | ||
1250 | csz = sz = 0; /* Treat a[] and a[?] as zero-sized. */ | ||
1251 | } | ||
1252 | align = cp_field_align(cp, ct, info); | ||
1253 | if (((attr|sattr) & CTFP_PACKED) || | ||
1254 | ((attr & CTFP_ALIGNED) && ctype_align(attr) > align)) | ||
1255 | align = ctype_align(attr); | ||
1256 | if (cp->packstack[cp->curpack] < align) | ||
1257 | align = cp->packstack[cp->curpack]; | ||
1258 | if (align > maxalign) maxalign = align; | ||
1259 | amask = (8u << align) - 1; | ||
1260 | |||
1261 | bsz = ctype_bitcsz(ct->info); /* Bitfield size (temp.). */ | ||
1262 | if (bsz == CTBSZ_FIELD || !ctype_isfield(ct->info)) { | ||
1263 | bsz = csz; /* Regular fields or subtypes always fill the container. */ | ||
1264 | bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ | ||
1265 | ct->size = (bofs >> 3); /* Store field offset. */ | ||
1266 | } else { /* Bitfield. */ | ||
1267 | if (bsz == 0 || (attr & CTFP_ALIGNED) || | ||
1268 | (!((attr|sattr) & CTFP_PACKED) && (bofs & amask) + bsz > csz)) | ||
1269 | bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ | ||
1270 | |||
1271 | /* Prefer regular field over bitfield. */ | ||
1272 | if (bsz == csz && (bofs & amask) == 0) { | ||
1273 | ct->info = CTINFO(CT_FIELD, ctype_cid(ct->info)); | ||
1274 | ct->size = (bofs >> 3); /* Store field offset. */ | ||
1275 | } else { | ||
1276 | ct->info = CTINFO(CT_BITFIELD, | ||
1277 | (info & (CTF_QUAL|CTF_UNSIGNED|CTF_BOOL)) + | ||
1278 | (csz << (CTSHIFT_BITCSZ-3)) + (bsz << CTSHIFT_BITBSZ)); | ||
1279 | #if LJ_BE | ||
1280 | ct->info += ((csz - (bofs & (csz-1)) - bsz) << CTSHIFT_BITPOS); | ||
1281 | #else | ||
1282 | ct->info += ((bofs & (csz-1)) << CTSHIFT_BITPOS); | ||
1283 | #endif | ||
1284 | ct->size = ((bofs & ~(csz-1)) >> 3); /* Store container offset. */ | ||
1285 | } | ||
1286 | } | ||
1287 | |||
1288 | /* Determine next offset or max. offset. */ | ||
1289 | if ((sinfo & CTF_UNION)) { | ||
1290 | if (bsz > bmaxofs) bmaxofs = bsz; | ||
1291 | } else { | ||
1292 | bofs += bsz; | ||
1293 | } | ||
1294 | } /* All other fields in the chain are already set up. */ | ||
1295 | |||
1296 | fieldid = ct->sib; | ||
1297 | } | ||
1298 | |||
1299 | /* Complete struct/union. */ | ||
1300 | sct->info = sinfo + CTALIGN(maxalign); | ||
1301 | bofs = (sinfo & CTF_UNION) ? bmaxofs : bofs; | ||
1302 | maxalign = (8u << maxalign) - 1; | ||
1303 | sct->size = (((bofs + maxalign) & ~maxalign) >> 3); | ||
1304 | } | ||
1305 | |||
1306 | /* Parse struct/union declaration. */ | ||
1307 | static CTypeID cp_decl_struct(CPState *cp, CPDecl *sdecl, CTInfo sinfo) | ||
1308 | { | ||
1309 | CTypeID sid = cp_struct_name(cp, sdecl, sinfo); | ||
1310 | if (cp_opt(cp, '{')) { /* Struct/union definition. */ | ||
1311 | CTypeID lastid = sid; | ||
1312 | int lastdecl = 0; | ||
1313 | while (cp->tok != '}') { | ||
1314 | CPDecl decl; | ||
1315 | CPscl scl = cp_decl_spec(cp, &decl, CDF_STATIC); | ||
1316 | decl.mode = scl ? CPARSE_MODE_DIRECT : | ||
1317 | CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT|CPARSE_MODE_FIELD; | ||
1318 | |||
1319 | for (;;) { | ||
1320 | CTypeID typeid; | ||
1321 | |||
1322 | if (lastdecl) cp_err_token(cp, '}'); | ||
1323 | |||
1324 | /* Parse field declarator. */ | ||
1325 | decl.bits = CTSIZE_INVALID; | ||
1326 | cp_declarator(cp, &decl); | ||
1327 | typeid = cp_decl_intern(cp, &decl); | ||
1328 | |||
1329 | if ((scl & CDF_STATIC)) { /* Static constant in struct namespace. */ | ||
1330 | CType *ct; | ||
1331 | CTypeID fieldid = cp_decl_constinit(cp, &ct, typeid); | ||
1332 | ctype_get(cp->cts, lastid)->sib = fieldid; | ||
1333 | lastid = fieldid; | ||
1334 | ctype_setname(ct, decl.name); | ||
1335 | } else { | ||
1336 | CTSize bsz = CTBSZ_FIELD; /* Temp. for layout phase. */ | ||
1337 | CType *ct; | ||
1338 | CTypeID fieldid = lj_ctype_new(cp->cts, &ct); /* Do this first. */ | ||
1339 | CType *tct = ctype_raw(cp->cts, typeid); | ||
1340 | |||
1341 | if (decl.bits == CTSIZE_INVALID) { /* Regular field. */ | ||
1342 | if (ctype_isarray(tct->info) && tct->size == CTSIZE_INVALID) | ||
1343 | lastdecl = 1; /* a[] or a[?] must be the last declared field. */ | ||
1344 | |||
1345 | /* Accept transparent struct/union/enum. */ | ||
1346 | if (!decl.name) { | ||
1347 | if (!((ctype_isstruct(tct->info) && !(tct->info & CTF_VLA)) || | ||
1348 | ctype_isenum(tct->info))) | ||
1349 | cp_err_token(cp, CTOK_IDENT); | ||
1350 | ct->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_SUBTYPE) + typeid); | ||
1351 | ct->size = ctype_isstruct(tct->info) ? | ||
1352 | (decl.attr|0x80000000u) : 0; /* For layout phase. */ | ||
1353 | goto add_field; | ||
1354 | } | ||
1355 | } else { /* Bitfield. */ | ||
1356 | bsz = decl.bits; | ||
1357 | if (!ctype_isinteger_or_bool(tct->info) || | ||
1358 | (bsz == 0 && decl.name) || 8*tct->size > CTBSZ_MAX || | ||
1359 | bsz > ((tct->info & CTF_BOOL) ? 1 : 8*tct->size)) | ||
1360 | cp_errmsg(cp, ':', LJ_ERR_BADVAL); | ||
1361 | } | ||
1362 | |||
1363 | /* Create temporary field for layout phase. */ | ||
1364 | ct->info = CTINFO(CT_FIELD, typeid + (bsz << CTSHIFT_BITCSZ)); | ||
1365 | ct->size = decl.attr; | ||
1366 | if (decl.name) ctype_setname(ct, decl.name); | ||
1367 | |||
1368 | add_field: | ||
1369 | ctype_get(cp->cts, lastid)->sib = fieldid; | ||
1370 | lastid = fieldid; | ||
1371 | } | ||
1372 | if (!cp_opt(cp, ',')) break; | ||
1373 | cp_decl_reset(&decl); | ||
1374 | } | ||
1375 | cp_check(cp, ';'); | ||
1376 | } | ||
1377 | cp_check(cp, '}'); | ||
1378 | ctype_get(cp->cts, lastid)->sib = 0; /* Drop sib = 1 for empty structs. */ | ||
1379 | cp_decl_attributes(cp, sdecl); /* Layout phase needs postfix attributes. */ | ||
1380 | cp_struct_layout(cp, sid, sdecl->attr); | ||
1381 | } | ||
1382 | return sid; | ||
1383 | } | ||
1384 | |||
1385 | /* Parse enum declaration. */ | ||
1386 | static CTypeID cp_decl_enum(CPState *cp, CPDecl *sdecl) | ||
1387 | { | ||
1388 | CTypeID eid = cp_struct_name(cp, sdecl, CTINFO(CT_ENUM, CTID_VOID)); | ||
1389 | CTInfo einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_UINT32); | ||
1390 | CTSize esize = 4; /* Only 32 bit enums are supported. */ | ||
1391 | if (cp_opt(cp, '{')) { /* Enum definition. */ | ||
1392 | CPValue k; | ||
1393 | CTypeID lastid = eid; | ||
1394 | k.u32 = 0; | ||
1395 | k.id = CTID_INT32; | ||
1396 | do { | ||
1397 | GCstr *name = cp->str; | ||
1398 | if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); | ||
1399 | if (cp->val.id) cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(name)); | ||
1400 | cp_next(cp); | ||
1401 | if (cp_opt(cp, '=')) { | ||
1402 | cp_expr_kint(cp, &k); | ||
1403 | if (k.id == CTID_UINT32) { | ||
1404 | /* C99 says that enum constants are always (signed) integers. | ||
1405 | ** But since unsigned constants like 0x80000000 are quite common, | ||
1406 | ** those are left as uint32_t. | ||
1407 | */ | ||
1408 | if (k.i32 >= 0) k.id = CTID_INT32; | ||
1409 | } else { | ||
1410 | /* OTOH it's common practice and even mandated by some ABIs | ||
1411 | ** that the enum type itself is unsigned, unless there are any | ||
1412 | ** negative constants. | ||
1413 | */ | ||
1414 | k.id = CTID_INT32; | ||
1415 | if (k.i32 < 0) einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_INT32); | ||
1416 | } | ||
1417 | } | ||
1418 | /* Add named enum constant. */ | ||
1419 | { | ||
1420 | CType *ct; | ||
1421 | CTypeID constid = lj_ctype_new(cp->cts, &ct); | ||
1422 | ctype_get(cp->cts, lastid)->sib = constid; | ||
1423 | lastid = constid; | ||
1424 | ctype_setname(ct, name); | ||
1425 | ct->info = CTINFO(CT_CONSTVAL, CTF_CONST|k.id); | ||
1426 | ct->size = k.u32++; | ||
1427 | if (k.u32 == 0x80000000u) k.id = CTID_UINT32; | ||
1428 | lj_ctype_addname(cp->cts, ct, constid); | ||
1429 | } | ||
1430 | if (!cp_opt(cp, ',')) break; | ||
1431 | } while (cp->tok != '}'); /* Trailing ',' is ok. */ | ||
1432 | cp_check(cp, '}'); | ||
1433 | /* Complete enum. */ | ||
1434 | ctype_get(cp->cts, eid)->info = einfo; | ||
1435 | ctype_get(cp->cts, eid)->size = esize; | ||
1436 | } | ||
1437 | return eid; | ||
1438 | } | ||
1439 | |||
1440 | /* Parse declaration specifiers. */ | ||
1441 | static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl) | ||
1442 | { | ||
1443 | uint32_t cds = 0, sz = 0; | ||
1444 | CTInfo tdef = 0; | ||
1445 | |||
1446 | decl->cp = cp; | ||
1447 | decl->mode = cp->mode; | ||
1448 | decl->name = NULL; | ||
1449 | decl->redir = NULL; | ||
1450 | decl->attr = 0; | ||
1451 | decl->fattr = 0; | ||
1452 | decl->pos = decl->top = 0; | ||
1453 | decl->stack[0].next = 0; | ||
1454 | |||
1455 | for (;;) { /* Parse basic types. */ | ||
1456 | cp_decl_attributes(cp, decl); | ||
1457 | switch (cp->tok) { | ||
1458 | case CTOK_STRUCT: | ||
1459 | tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, 0)); | ||
1460 | break; | ||
1461 | case CTOK_UNION: | ||
1462 | tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, CTF_UNION)); | ||
1463 | break; | ||
1464 | case CTOK_ENUM: | ||
1465 | tdef = cp_decl_enum(cp, decl); | ||
1466 | break; | ||
1467 | case CTOK_IDENT: | ||
1468 | if (!ctype_istypedef(cp->ct->info) || sz || tdef || | ||
1469 | (cds & (CDF_SHORT|CDF_LONG|CDF_SIGNED|CDF_UNSIGNED|CDF_COMPLEX))) | ||
1470 | goto end_decl; | ||
1471 | tdef = ctype_cid(cp->ct->info); /* Get typedef. */ | ||
1472 | cp_next(cp); | ||
1473 | break; | ||
1474 | default: | ||
1475 | if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECLFLAG) { | ||
1476 | uint32_t cbit; | ||
1477 | if (cp->ct->size) { | ||
1478 | if (sz) goto end_decl; | ||
1479 | sz = cp->ct->size; | ||
1480 | } | ||
1481 | cbit = (1u << (cp->tok - CTOK_FIRSTDECL)); | ||
1482 | cds = cds | cbit | ((cbit & cds & CDF_LONG) << 1); | ||
1483 | if (cp->tok >= CTOK_FIRSTSCL && !(scl & cbit)) | ||
1484 | cp_errmsg(cp, cp->tok, LJ_ERR_FFI_BADSCL); | ||
1485 | cp_next(cp); | ||
1486 | break; | ||
1487 | } | ||
1488 | goto end_decl; | ||
1489 | } | ||
1490 | } | ||
1491 | end_decl: | ||
1492 | |||
1493 | if ((cds & CDF_COMPLEX)) /* Use predefined complex types. */ | ||
1494 | tdef = sz == 4 ? CTID_COMPLEX_FLOAT : CTID_COMPLEX_DOUBLE; | ||
1495 | |||
1496 | if (tdef) { | ||
1497 | cp_push_type(decl, tdef); | ||
1498 | } else if ((cds & CDF_VOID)) { | ||
1499 | cp_push(decl, CTINFO(CT_VOID, (decl->attr & CTF_QUAL)), CTSIZE_INVALID); | ||
1500 | decl->attr &= ~CTF_QUAL; | ||
1501 | } else { | ||
1502 | /* Determine type info and size. */ | ||
1503 | CTInfo info = CTINFO(CT_NUM, (cds & CDF_UNSIGNED) ? CTF_UNSIGNED : 0); | ||
1504 | if ((cds & CDF_BOOL)) { | ||
1505 | info = CTINFO(CT_NUM, CTF_UNSIGNED|CTF_BOOL); | ||
1506 | lua_assert(sz == 1); | ||
1507 | } else if ((cds & CDF_FP)) { | ||
1508 | info = CTINFO(CT_NUM, CTF_FP); | ||
1509 | if ((cds & CDF_LONG)) sz = sizeof(long double); | ||
1510 | } else if ((cds & CDF_CHAR)) { | ||
1511 | if ((cds & (CDF_CHAR|CDF_SIGNED|CDF_UNSIGNED)) == CDF_CHAR) | ||
1512 | info |= CTF_UCHAR; /* Handle platforms where char is unsigned. */ | ||
1513 | } else if ((cds & CDF_SHORT)) { | ||
1514 | sz = sizeof(short); | ||
1515 | } else if ((cds & CDF_LONGLONG)) { | ||
1516 | sz = 8; | ||
1517 | } else if ((cds & CDF_LONG)) { | ||
1518 | info |= CTF_LONG; | ||
1519 | sz = sizeof(long); | ||
1520 | } else if (!sz) { | ||
1521 | if (!(cds & (CDF_SIGNED|CDF_UNSIGNED))) | ||
1522 | cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC); | ||
1523 | sz = sizeof(int); | ||
1524 | } | ||
1525 | lua_assert(sz != 0); | ||
1526 | info += CTALIGN(lj_fls(sz)); /* Use natural alignment. */ | ||
1527 | info += (decl->attr & CTF_QUAL); /* Merge qualifiers. */ | ||
1528 | cp_push(decl, info, sz); | ||
1529 | decl->attr &= ~CTF_QUAL; | ||
1530 | } | ||
1531 | decl->specpos = decl->pos; | ||
1532 | decl->specattr = decl->attr; | ||
1533 | decl->specfattr = decl->fattr; | ||
1534 | return (cds & CDF_SCL); /* Return storage class. */ | ||
1535 | } | ||
1536 | |||
1537 | /* Parse array declaration. */ | ||
1538 | static void cp_decl_array(CPState *cp, CPDecl *decl) | ||
1539 | { | ||
1540 | CTInfo info = CTINFO(CT_ARRAY, 0); | ||
1541 | CTSize nelem = CTSIZE_INVALID; /* Default size for a[] or a[?]. */ | ||
1542 | cp_decl_attributes(cp, decl); | ||
1543 | if (cp_opt(cp, '?')) | ||
1544 | info |= CTF_VLA; /* Create variable-length array a[?]. */ | ||
1545 | else if (cp->tok != ']') | ||
1546 | nelem = cp_expr_ksize(cp); | ||
1547 | cp_check(cp, ']'); | ||
1548 | cp_add(decl, info, nelem); | ||
1549 | } | ||
1550 | |||
1551 | /* Parse function declaration. */ | ||
1552 | static void cp_decl_func(CPState *cp, CPDecl *fdecl) | ||
1553 | { | ||
1554 | CTSize nargs = 0; | ||
1555 | CTInfo info = CTINFO(CT_FUNC, 0); | ||
1556 | CTypeID lastid = 0, anchor = 0; | ||
1557 | if (cp->tok != ')') { | ||
1558 | do { | ||
1559 | CPDecl decl; | ||
1560 | CTypeID typeid, fieldid; | ||
1561 | CType *ct; | ||
1562 | if (cp_opt(cp, '.')) { /* Vararg function. */ | ||
1563 | cp_check(cp, '.'); /* Workaround for the minimalistic lexer. */ | ||
1564 | cp_check(cp, '.'); | ||
1565 | info |= CTF_VARARG; | ||
1566 | break; | ||
1567 | } | ||
1568 | cp_decl_spec(cp, &decl, CDF_REGISTER); | ||
1569 | decl.mode = CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT; | ||
1570 | cp_declarator(cp, &decl); | ||
1571 | typeid = cp_decl_intern(cp, &decl); | ||
1572 | ct = ctype_raw(cp->cts, typeid); | ||
1573 | if (ctype_isvoid(ct->info)) | ||
1574 | break; | ||
1575 | else if (ctype_isrefarray(ct->info)) | ||
1576 | typeid = lj_ctype_intern(cp->cts, | ||
1577 | CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ct->info)), CTSIZE_PTR); | ||
1578 | else if (ctype_isfunc(ct->info)) | ||
1579 | typeid = lj_ctype_intern(cp->cts, | ||
1580 | CTINFO(CT_PTR, CTALIGN_PTR|typeid), CTSIZE_PTR); | ||
1581 | /* Add new parameter. */ | ||
1582 | fieldid = lj_ctype_new(cp->cts, &ct); | ||
1583 | if (anchor) | ||
1584 | ctype_get(cp->cts, lastid)->sib = fieldid; | ||
1585 | else | ||
1586 | anchor = fieldid; | ||
1587 | lastid = fieldid; | ||
1588 | if (decl.name) ctype_setname(ct, decl.name); | ||
1589 | ct->info = CTINFO(CT_FIELD, typeid); | ||
1590 | ct->size = nargs++; | ||
1591 | } while (cp_opt(cp, ',')); | ||
1592 | } | ||
1593 | cp_check(cp, ')'); | ||
1594 | if (cp_opt(cp, '{')) { /* Skip function definition. */ | ||
1595 | int level = 1; | ||
1596 | cp->mode |= CPARSE_MODE_SKIP; | ||
1597 | for (;;) { | ||
1598 | if (cp->tok == '{') level++; | ||
1599 | else if (cp->tok == '}' && --level == 0) break; | ||
1600 | else if (cp->tok == CTOK_EOF) cp_err_token(cp, '}'); | ||
1601 | cp_next(cp); | ||
1602 | } | ||
1603 | cp->mode &= ~CPARSE_MODE_SKIP; | ||
1604 | cp->tok = ';'; /* Ok for cp_decl_multi(), error in cp_decl_single(). */ | ||
1605 | } | ||
1606 | info |= (fdecl->fattr & ~CTMASK_CID); | ||
1607 | fdecl->fattr = 0; | ||
1608 | fdecl->stack[cp_add(fdecl, info, nargs)].sib = anchor; | ||
1609 | } | ||
1610 | |||
1611 | /* Parse declarator. */ | ||
1612 | static void cp_declarator(CPState *cp, CPDecl *decl) | ||
1613 | { | ||
1614 | if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); | ||
1615 | |||
1616 | for (;;) { /* Head of declarator. */ | ||
1617 | if (cp_opt(cp, '*')) { /* Pointer. */ | ||
1618 | CTSize sz; | ||
1619 | CTInfo info; | ||
1620 | cp_decl_attributes(cp, decl); | ||
1621 | sz = CTSIZE_PTR; | ||
1622 | info = CTINFO(CT_PTR, CTALIGN_PTR); | ||
1623 | #if LJ_64 | ||
1624 | if (ctype_msizeP(decl->attr) == 4) { | ||
1625 | sz = 4; | ||
1626 | info = CTINFO(CT_PTR, CTALIGN(2)); | ||
1627 | } | ||
1628 | #endif | ||
1629 | info += (decl->attr & (CTF_QUAL|CTF_REF)); | ||
1630 | decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<<CTSHIFT_MSIZEP)); | ||
1631 | cp_push(decl, info, sz); | ||
1632 | } else if (cp_opt(cp, '&') || cp_opt(cp, CTOK_ANDAND)) { /* Reference. */ | ||
1633 | decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<<CTSHIFT_MSIZEP)); | ||
1634 | cp_push(decl, CTINFO_REF(0), CTSIZE_PTR); | ||
1635 | } else { | ||
1636 | break; | ||
1637 | } | ||
1638 | } | ||
1639 | |||
1640 | if (cp_opt(cp, '(')) { /* Inner declarator. */ | ||
1641 | CPDeclIdx pos; | ||
1642 | cp_decl_attributes(cp, decl); | ||
1643 | /* Resolve ambiguity between inner declarator and 1st function parameter. */ | ||
1644 | if ((decl->mode & CPARSE_MODE_ABSTRACT) && | ||
1645 | (cp->tok == ')' || cp_istypedecl(cp))) goto func_decl; | ||
1646 | pos = decl->pos; | ||
1647 | cp_declarator(cp, decl); | ||
1648 | cp_check(cp, ')'); | ||
1649 | decl->pos = pos; | ||
1650 | } else if (cp->tok == CTOK_IDENT) { /* Direct declarator. */ | ||
1651 | if (!(decl->mode & CPARSE_MODE_DIRECT)) cp_err_token(cp, CTOK_EOF); | ||
1652 | decl->name = cp->str; | ||
1653 | decl->nameid = cp->val.id; | ||
1654 | cp_next(cp); | ||
1655 | } else { /* Abstract declarator. */ | ||
1656 | if (!(decl->mode & CPARSE_MODE_ABSTRACT)) cp_err_token(cp, CTOK_IDENT); | ||
1657 | } | ||
1658 | |||
1659 | for (;;) { /* Tail of declarator. */ | ||
1660 | if (cp_opt(cp, '[')) { /* Array. */ | ||
1661 | cp_decl_array(cp, decl); | ||
1662 | } else if (cp_opt(cp, '(')) { /* Function. */ | ||
1663 | func_decl: | ||
1664 | cp_decl_func(cp, decl); | ||
1665 | } else { | ||
1666 | break; | ||
1667 | } | ||
1668 | } | ||
1669 | |||
1670 | if ((decl->mode & CPARSE_MODE_FIELD) && cp_opt(cp, ':')) /* Field width. */ | ||
1671 | decl->bits = cp_expr_ksize(cp); | ||
1672 | |||
1673 | /* Process postfix attributes. */ | ||
1674 | cp_decl_attributes(cp, decl); | ||
1675 | cp_push_attributes(decl); | ||
1676 | |||
1677 | cp->depth--; | ||
1678 | } | ||
1679 | |||
1680 | /* Parse an abstract type declaration and return it's C type ID. */ | ||
1681 | static CTypeID cp_decl_abstract(CPState *cp) | ||
1682 | { | ||
1683 | CPDecl decl; | ||
1684 | cp_decl_spec(cp, &decl, 0); | ||
1685 | decl.mode = CPARSE_MODE_ABSTRACT; | ||
1686 | cp_declarator(cp, &decl); | ||
1687 | return cp_decl_intern(cp, &decl); | ||
1688 | } | ||
1689 | |||
1690 | /* Handle pragmas. */ | ||
1691 | static void cp_pragma(CPState *cp, BCLine pragmaline) | ||
1692 | { | ||
1693 | cp_next(cp); | ||
1694 | if (cp->tok == CTOK_IDENT && | ||
1695 | cp->str->hash == H_(e79b999f,42ca3e85)) { /* pack */ | ||
1696 | cp_next(cp); | ||
1697 | cp_check(cp, '('); | ||
1698 | if (cp->tok == CTOK_IDENT) { | ||
1699 | if (cp->str->hash == H_(738e923c,a1b65954)) { /* push */ | ||
1700 | if (cp->curpack < CPARSE_MAX_PACKSTACK) { | ||
1701 | cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; | ||
1702 | cp->curpack++; | ||
1703 | } | ||
1704 | } else if (cp->str->hash == H_(6c71cf27,6c71cf27)) { /* pop */ | ||
1705 | if (cp->curpack > 0) cp->curpack--; | ||
1706 | } else { | ||
1707 | cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); | ||
1708 | } | ||
1709 | cp_next(cp); | ||
1710 | if (!cp_opt(cp, ',')) goto end_pack; | ||
1711 | } | ||
1712 | if (cp->tok == CTOK_INTEGER) { | ||
1713 | cp->packstack[cp->curpack] = cp->val.u32 ? lj_fls(cp->val.u32) : 0; | ||
1714 | cp_next(cp); | ||
1715 | } else { | ||
1716 | cp->packstack[cp->curpack] = 255; | ||
1717 | } | ||
1718 | end_pack: | ||
1719 | cp_check(cp, ')'); | ||
1720 | } else { /* Ignore all other pragmas. */ | ||
1721 | while (cp->tok != CTOK_EOF && cp->linenumber == pragmaline) | ||
1722 | cp_next(cp); | ||
1723 | } | ||
1724 | } | ||
1725 | |||
1726 | /* Parse multiple C declarations of types or extern identifiers. */ | ||
1727 | static void cp_decl_multi(CPState *cp) | ||
1728 | { | ||
1729 | int first = 1; | ||
1730 | while (cp->tok != CTOK_EOF) { | ||
1731 | CPDecl decl; | ||
1732 | CPscl scl; | ||
1733 | if (cp_opt(cp, ';')) { /* Skip empty statements. */ | ||
1734 | first = 0; | ||
1735 | continue; | ||
1736 | } | ||
1737 | if (cp->tok == '#') { /* Workaround, since we have no preprocessor, yet. */ | ||
1738 | BCLine pragmaline = cp->linenumber; | ||
1739 | if (!(cp_next(cp) == CTOK_IDENT && | ||
1740 | cp->str->hash == H_(f5e6b4f8,1d509107))) /* pragma */ | ||
1741 | cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); | ||
1742 | cp_pragma(cp, pragmaline); | ||
1743 | continue; | ||
1744 | } | ||
1745 | scl = cp_decl_spec(cp, &decl, CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC); | ||
1746 | if ((cp->tok == ';' || cp->tok == CTOK_EOF) && | ||
1747 | ctype_istypedef(decl.stack[0].info)) { | ||
1748 | CTInfo info = ctype_rawchild(cp->cts, &decl.stack[0])->info; | ||
1749 | if (ctype_isstruct(info) || ctype_isenum(info)) | ||
1750 | goto decl_end; /* Accept empty declaration of struct/union/enum. */ | ||
1751 | } | ||
1752 | for (;;) { | ||
1753 | CTypeID typeid; | ||
1754 | cp_declarator(cp, &decl); | ||
1755 | typeid = cp_decl_intern(cp, &decl); | ||
1756 | if (decl.name && !decl.nameid) { /* NYI: redeclarations are ignored. */ | ||
1757 | CType *ct; | ||
1758 | CTypeID id; | ||
1759 | if ((scl & CDF_TYPEDEF)) { /* Create new typedef. */ | ||
1760 | id = lj_ctype_new(cp->cts, &ct); | ||
1761 | ct->info = CTINFO(CT_TYPEDEF, typeid); | ||
1762 | goto noredir; | ||
1763 | } else if (ctype_isfunc(ctype_get(cp->cts, typeid)->info)) { | ||
1764 | /* Treat both static and extern function declarations as extern. */ | ||
1765 | ct = ctype_get(cp->cts, typeid); | ||
1766 | /* We always get new anonymous functions (typedefs are copied). */ | ||
1767 | lua_assert(gcref(ct->name) == NULL); | ||
1768 | id = typeid; /* Just name it. */ | ||
1769 | } else if ((scl & CDF_STATIC)) { /* Accept static constants. */ | ||
1770 | id = cp_decl_constinit(cp, &ct, typeid); | ||
1771 | goto noredir; | ||
1772 | } else { /* External references have extern or no storage class. */ | ||
1773 | id = lj_ctype_new(cp->cts, &ct); | ||
1774 | ct->info = CTINFO(CT_EXTERN, typeid); | ||
1775 | } | ||
1776 | if (decl.redir) { /* Add attribute for redirected symbol name. */ | ||
1777 | CType *cta; | ||
1778 | CTypeID aid = lj_ctype_new(cp->cts, &cta); | ||
1779 | ct = ctype_get(cp->cts, id); /* Table may have been reallocated. */ | ||
1780 | cta->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_REDIR)); | ||
1781 | cta->sib = ct->sib; | ||
1782 | ct->sib = aid; | ||
1783 | ctype_setname(cta, decl.redir); | ||
1784 | } | ||
1785 | noredir: | ||
1786 | ctype_setname(ct, decl.name); | ||
1787 | lj_ctype_addname(cp->cts, ct, id); | ||
1788 | } | ||
1789 | if (!cp_opt(cp, ',')) break; | ||
1790 | cp_decl_reset(&decl); | ||
1791 | } | ||
1792 | decl_end: | ||
1793 | if (cp->tok == CTOK_EOF && first) break; /* May omit ';' for 1 decl. */ | ||
1794 | first = 0; | ||
1795 | cp_check(cp, ';'); | ||
1796 | } | ||
1797 | } | ||
1798 | |||
1799 | /* Parse a single C type declaration. */ | ||
1800 | static void cp_decl_single(CPState *cp) | ||
1801 | { | ||
1802 | CPDecl decl; | ||
1803 | cp_decl_spec(cp, &decl, 0); | ||
1804 | cp_declarator(cp, &decl); | ||
1805 | cp->val.id = cp_decl_intern(cp, &decl); | ||
1806 | if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); | ||
1807 | } | ||
1808 | |||
1809 | #undef H_ | ||
1810 | |||
1811 | /* ------------------------------------------------------------------------ */ | ||
1812 | |||
1813 | /* Protected callback for C parser. */ | ||
1814 | static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud) | ||
1815 | { | ||
1816 | CPState *cp = (CPState *)ud; | ||
1817 | UNUSED(dummy); | ||
1818 | cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ | ||
1819 | cp_init(cp); | ||
1820 | if ((cp->mode & CPARSE_MODE_MULTI)) | ||
1821 | cp_decl_multi(cp); | ||
1822 | else | ||
1823 | cp_decl_single(cp); | ||
1824 | lua_assert(cp->depth == 0); | ||
1825 | return NULL; | ||
1826 | } | ||
1827 | |||
1828 | /* C parser. */ | ||
1829 | int lj_cparse(CPState *cp) | ||
1830 | { | ||
1831 | LJ_CTYPE_SAVE(cp->cts); | ||
1832 | int errcode = lj_vm_cpcall(cp->L, NULL, cp, cpcparser); | ||
1833 | if (errcode) | ||
1834 | LJ_CTYPE_RESTORE(cp->cts); | ||
1835 | cp_cleanup(cp); | ||
1836 | return errcode; | ||
1837 | } | ||
1838 | |||
1839 | #endif | ||