keep an array literal a global pointer is set to
At file scope every compound literal was cut down to its braces, right for a value and not for an array a pointer points at: the address of a static object is a constant, and its braces alone the pointer's own.
This commit is contained in:
61
semen.scm
61
semen.scm
@@ -2042,7 +2042,13 @@
|
||||
(walk-operands core (drop core 3) env))
|
||||
core))
|
||||
env)))
|
||||
(copy-form-source! solved (map bare-braces solved)))))
|
||||
(if (> (length solved) 3)
|
||||
(copy-form-source!
|
||||
solved
|
||||
(append (take solved 3)
|
||||
(map (cut bare-braces <> (third solved))
|
||||
(drop solved 3))))
|
||||
solved))))
|
||||
(form (if qualifier
|
||||
(copy-form-source! resolved (cons qualifier core))
|
||||
core)))
|
||||
@@ -2052,14 +2058,53 @@
|
||||
|
||||
;;; A compound literal is not a constant, though cc lets one pass at file
|
||||
;;; scope unless asked to be pedantic; where it initializes, its braces
|
||||
;;; alone say the same:
|
||||
;;; alone say the same. Not where it is an array a pointer is set to:
|
||||
;;; that is the address of a static object, which is a constant, and its
|
||||
;;; braces alone would be the pointer's own. TARGET is what FORM
|
||||
;;; initializes, #f where nothing says.
|
||||
;;;
|
||||
;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, .u = {.a = {1}}}
|
||||
(define (bare-braces form)
|
||||
(if (vector? form)
|
||||
(let ((parts (list-split (vector->list form) ':)))
|
||||
(list->vector (map bare-braces (last parts))))
|
||||
form))
|
||||
;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, ...}
|
||||
;;; (var p (* int) #([int 2] : 1 2)) -> static int * p = (int [2]){1, 2}
|
||||
(define (bare-braces form target)
|
||||
(if (not (vector? form))
|
||||
form
|
||||
(let* ((parts (list-split (vector->list form) ':))
|
||||
(written (and (pair? (cdr parts)) (unwrap-type (car parts)))))
|
||||
(if (and written (array-form? written) target (pointer-target target))
|
||||
form
|
||||
(let ((elements (last parts)))
|
||||
(list->vector
|
||||
(map bare-braces elements
|
||||
(element-targets (or written target) elements))))))))
|
||||
|
||||
;;; What each of ELEMENTS, in braces for TYPE, initializes: an element
|
||||
;;; of an array, the fields of an aggregate in turn, or the one a `.name'
|
||||
;;; before it names -- and #f where TYPE does not say
|
||||
(define (element-targets type elements)
|
||||
(let ((element (and type (array-form? type) (array-element-type type)))
|
||||
(fields (and type (not (array-form? type)) (type-fields type))))
|
||||
(let loop ((es elements) (next (or fields (list))) (acc (list)))
|
||||
(cond
|
||||
((null? es) (reverse acc))
|
||||
(element (loop (cdr es) next (cons element acc)))
|
||||
((designator-name (car es))
|
||||
=> (lambda (name)
|
||||
(let ((at (and fields
|
||||
(find-tail (lambda (f) (eq? (car f) name)) fields))))
|
||||
(if (and at (pair? (cdr es)))
|
||||
(loop (cddr es) (cdr at) (cons (second (car at)) (cons #f acc)))
|
||||
(loop (cdr es) (list) (cons #f acc))))))
|
||||
(else (loop (cdr es)
|
||||
(if (pair? next) (cdr next) next)
|
||||
(cons (and (pair? next) (second (car next))) acc)))))))
|
||||
|
||||
;;; .x -> x, as a designator names a field; #f for anything else
|
||||
(define (designator-name x)
|
||||
(and (symbol? x)
|
||||
(let ((s (symbol->string x)))
|
||||
(and (> (string-length s) 1)
|
||||
(char=? #\. (string-ref s 0))
|
||||
(string->symbol (substring s 1))))))
|
||||
|
||||
;;; Utils
|
||||
|
||||
|
||||
Reference in New Issue
Block a user