M: array c-type-unboxer-quot drop [ >c-ptr ] ;
- M: value-type c-type-rep drop int-rep ;
-
- M: value-type c-type-getter
- drop [ swap <displaced-alien> ] ;
-
- M: value-type c-type-setter ( type -- quot )
- [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
- '[ @ swap @ _ memcpy ] ;
-
PREDICATE: string-type < pair
- first2 [ "char*" = ] [ word? ] bi* and ;
+ first2 [ char* = ] [ word? ] bi* and ;
M: string-type c-type ;
: parse-array-type ( name -- array )
"[" split unclip
- [ [ "]" ?tail drop string>number ] map ] dip prefix ;
+ [ [ "]" ?tail drop string>number ] map ] dip
+ parse-c-type-name prefix ;
+
+: parse-c-type ( string -- array )
+ {
+ { [ CHAR: ] over member? ] [ parse-array-type ] }
+ { [ dup search c-type-word? ] [ parse-c-type-name resolve-typedef ] }
+ { [ dup c-types get at ] [ c-types get at resolve-typedef ] }
+ { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] }
+ [ no-c-type ]
+ } cond ;
M: string c-type ( name -- type )
- CHAR: ] over member? [
- parse-array-type
- ] [
- dup c-types get at [
- resolve-typedef
- ] [
- "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if
- ] ?if
- ] if ;
+ parse-c-type ;
+
+M: word c-type
+ "c-type" word-prop resolve-typedef ;
+ GENERIC: c-struct? ( type -- ? )
+
+ M: object c-struct?
+ drop f ;
+ M: string c-struct?
+ dup "void" = [ drop f ] [ c-type c-struct? ] if ;
+
! These words being foldable means that words need to be
! recompiled if a C type is redefined. Even so, folding the
! size facilitates some optimizations.
: flatten-large-struct ( c-type -- seq )
heap-size cell align
- cell /i "__stack_value" c-type <repetition> ;
+ cell /i \ (stack-value) c-type <repetition> ;
- M: struct-type flatten-value-type ( type -- seq )
+ : flatten-struct ( c-type -- seq )
dup heap-size 16 > [
flatten-large-struct
] [