mescc: Support anonymous enums.

* module/language/c99/compiler.mes (enum-def-list->constants): New function.
  (ast->info): Support anonymous enum.
This commit is contained in:
Jan Nieuwenhuizen 2017-05-25 07:57:26 +02:00
parent 6af7556d51
commit 92a330ff07

View file

@ -1776,24 +1776,20 @@
(int->bv32 (+ d (data-offset ,value g))) (int->bv32 (+ d (data-offset ,value g)))
(list-tail data (+ here 4)))))))))))) (list-tail data (+ here 4))))))))))))
;; enum ;; enum foo { };
((decl (decl-spec-list (type-spec (enum-def (ident ,name) (enum-def-list . ,fields))))) ((decl (decl-spec-list (type-spec (enum-def (ident ,name) (enum-def-list . ,fields)))))
(let ((type (enum->type name fields)) (let ((type (enum->type name fields))
(constants (let loop ((fields fields) (i 0) (constants constants)) (constants (enum-def-list->constants constants fields)))
(if (null? fields) constants
(let* ((field (car fields))
(name (pmatch field
((enum-defn (ident ,name) . _) name)))
(i (pmatch field
((enum-defn ,name (p-expr (fixed ,value))) (cstring->number value))
((enum-defn ,name) i))))
(loop (cdr fields)
(1+ i)
(append constants (list (ident->constant name i)))))))))
(clone info (clone info
#:types (append (.types info) (list type)) #:types (append (.types info) (list type))
#:constants (append constants (.constants info))))) #:constants (append constants (.constants info)))))
;; enum {};
((decl (decl-spec-list (type-spec (enum-def (enum-def-list . ,fields)))))
(let ((constants (enum-def-list->constants constants fields)))
(clone info
#:constants (append constants (.constants info)))))
;; struct ;; struct
((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields))))) ((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields)))))
(let ((type (struct->type (list "struct" name) (map struct-field fields)))) (let ((type (struct->type (list "struct" name) (map struct-field fields))))
@ -2035,6 +2031,19 @@
(_ (let ((info ((expr->accu info) o))) (_ (let ((info ((expr->accu info) o)))
(append-text info (wrap-as (i386:accu-zero?))))))))) (append-text info (wrap-as (i386:accu-zero?)))))))))
(define (enum-def-list->constants constants fields)
(let loop ((fields fields) (i 0) (constants constants))
(if (null? fields) constants
(let* ((field (car fields))
(name (pmatch field
((enum-defn (ident ,name) . _) name)))
(i (pmatch field
((enum-defn ,name (p-expr (fixed ,value))) (cstring->number value))
((enum-defn ,name) i))))
(loop (cdr fields)
(1+ i)
(append constants (list (ident->constant name i))))))))
(define (initzer->non-const info) (define (initzer->non-const info)
(lambda (o) (lambda (o)
(pmatch o (pmatch o