mescc: Tinycc support: sizeof struct field.

* module/language/c99/compiler.mes (expr->accu): Sizeof struct field.
* scaffold/tests/72-typedef-struct-def.c (test): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-17 07:54:48 +02:00
parent d393785381
commit 8ec7e1225b
2 changed files with 14 additions and 0 deletions

View file

@ -511,6 +511,11 @@
(size (ast-type->size info type)))
(append-text info (wrap-as (i386:value->accu size)))))
((sizeof-expr (i-sel (ident ,field) (p-expr (ident ,array))))
(let* ((type (ident->type info array))
(size (field-size info type field)))
(append-text info (wrap-as (i386:value->accu size)))))
((sizeof-type (type-name (decl-spec-list (type-spec (fixed-type ,name)))))
(let* ((type name)
(size (ast-type->size info type)))
@ -1040,6 +1045,13 @@
(prefix (and=> (member field (reverse fields) (lambda (a b) (equal? a (car b)))) cdr)))
(apply + (map field:size prefix))))))
(define (field-size info struct field)
(let ((xtype (ast-type->type info struct)))
(if (eq? (type:type xtype) 'union) 0
(let* ((fields (type:description xtype))
(field (and=> (member field fields (lambda (a b) (equal? a (car b)))) car)))
(field:size field)))))
(define (ast->type o)
(pmatch o
((fixed-type ,type)

View file

@ -54,5 +54,7 @@ test ()
(*pp)->i = 3;
printf ("(*pp)->i=%d\n", b.i);
printf ("sizeof i:%d\n", sizeof (p->i));
return 0;
}