1 ! Copyright (C) 2022 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.enums
4 alien.strings ascii assocs byte-arrays classes classes.struct
5 combinators combinators.extras combinators.short-circuit
6 combinators.smart discord io io.backend io.directories
7 io.encodings.utf8 io.files.info kernel layouts libc libclang.ffi
8 make math math.parser multiline namespaces prettyprint sequences
9 sequences.private sets sorting splitting strings ;
13 : clang-state> ( -- clang-state ) clang-state get-global ;
17 defs-counter c-defs-by-name c-defs-by-order
19 unnamed-counter unnamed-table
21 out-forms-counter out-forms out-forms-by-name
22 out-forms-written out-form-names-written ;
24 : <libclang-state> ( -- state )
27 H{ } clone >>c-defs-by-name
28 H{ } clone >>c-defs-by-order
30 H{ } clone >>child-forms
32 H{ } clone >>unnamed-table
35 H{ } clone >>out-forms
36 H{ } clone >>out-forms-by-name
37 HS{ } clone >>out-forms-written
38 HS{ } clone >>out-form-names-written ;
40 : next-defs-counter ( libclang-state -- n ) [ dup 1 + ] change-defs-counter drop ;
41 : next-unnamed-counter ( libclang-state -- n ) [ dup 1 + ] change-unnamed-counter drop ;
42 : next-out-forms-counter ( libclang-state -- n ) [ dup 1 + ] change-out-forms-counter drop ;
44 GENERIC: def>out-form ( obj -- string )
46 : out-form-written? ( string -- ? )
47 clang-state> out-forms-written>> in? ; inline
49 : out-form-name-written? ( string -- ? )
50 clang-state> out-form-names-written>> in? ; inline
52 : save-out-form ( string def -- )
56 over out-form-written? [
57 ! dup name>> out-form-name-written? [
64 [ next-out-forms-counter ]
65 [ out-forms>> set-at ] bi
67 [ nipd [ name>> ] dip out-form-names-written>> adjoin ]
68 [ nip out-forms-written>> adjoin ]
69 [ [ name>> ] dip out-forms-by-name>> push-at ]
74 ! some forms must be defined out of order, e.g. anonymous unions/structs
75 : def>out-forms ( obj -- )
76 [ def>out-form ] keep save-out-form ;
78 : peek-current-form ( -- n )
79 clang-state> c-forms>> ?last ; inline
84 : push-child-form ( form -- )
85 ! dup order>> c-defs-by-order get-global set-at ; inline
86 dup parent-order>> clang-state> child-forms>> push-at ; inline
88 : with-new-form ( quot -- n )
89 clang-state> [ next-defs-counter ] [ c-forms>> ] bi push
91 clang-state> c-forms>> pop ; inline
93 ERROR: unknown-form name ;
94 GENERIC: print-deferred ( obj -- )
96 ! foo*** -> foo, todo: other cases?
97 : factor-type-name ( type -- type' ) [ CHAR: * = ] trim-tail ;
99 : ?lookup-type ( type -- obj/f )
101 clang-state> c-defs-by-name>> ?at [ drop f ] unless ;
103 : lookup-order ( obj -- order/f ) type>> ?lookup-type [ order>> ] ?call -1 or ;
105 M: object print-deferred
106 type>> ?lookup-type [ def>out-forms ] when* ;
108 : unnamed? ( string -- ? ) "(unnamed" swap subseq? ; inline
109 : unnamed-exists? ( string -- value/key ? ) clang-state> unnamed-table>> ?at ; inline
110 : lookup-unnamed ( type string -- type-name )
114 [ clang-state> next-unnamed-counter number>string append ] dip
116 ! "RECORDING: " gwrite dup g... gflush
117 [ clang-state> unnamed-table>> set-at ] keepd
120 : ?unnamed ( string type -- string' ? )
122 swap lookup-unnamed t
128 { return-type string }
133 : <c-function> ( return-type name args -- c-function )
138 clang-state> next-defs-counter >>order ;
144 : <c-struct> ( name order -- c-struct )
153 : <c-union> ( name order -- c-union )
164 : <c-enum> ( name order -- c-enum )
175 : <c-arg> ( name type -- c-arg )
179 peek-current-form >>parent-order
180 clang-state> next-defs-counter >>order ;
188 : <c-field> ( name type -- c-field )
192 peek-current-form >>parent-order
193 clang-state> next-defs-counter >>order ;
200 : <c-typedef> ( type name -- c-typedef )
204 clang-state> next-defs-counter >>order ;
206 M: c-function def>out-form
209 [ drop "FUNCTION: " ]
210 [ return-type>> " " ]
212 [ args>> dup empty? ")\n" " )\n" ? ]
214 ] "" append-outputs-as ;
216 : ignore-typedef? ( typedef -- ? )
217 [ type>> ] [ name>> ] bi
218 { [ = ] [ [ empty? ] either? ] } 2|| ;
220 M: c-typedef def>out-form
221 dup ignore-typedef? [
230 ] "" append-outputs-as
233 ERROR: unknown-child-forms order ;
234 M: c-field def>out-form
241 ] "" append-outputs-as ;
243 : print-defers ( current-order slots -- )
245 tuck lookup-order < [
252 : empty-struct? ( c-struct -- ? )
253 order>> clang-state> child-forms>> key? not ;
255 M: c-struct def>out-form
257 name>> "C-TYPE: " prepend
264 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
266 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
269 ] "" append-outputs-as
272 M: c-enum def>out-form
278 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
280 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
283 ] "" append-outputs-as ;
285 M: c-union def>out-form
288 [ drop "UNION-STRUCT: " ]
291 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
293 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
296 ] "" append-outputs-as ;
298 M: object def>out-form
299 class-of name>> "unknown object: " prepend ;
301 : set-definition ( named -- )
302 [ dup name>> clang-state> c-defs-by-name>> set-at ]
303 [ dup order>> clang-state> c-defs-by-order>> set-at ] bi ;
305 : set-typedef ( typedef -- )
306 dup ignore-typedef? [
309 [ type>> ] [ name>> ] bi clang-state> typedefs>> set-at
312 : clang-get-cstring ( CXString -- string )
313 clang_getCString [ utf8 alien>string ] [ clang_disposeString ] bi ;
315 : trim-blanks ( string -- string' )
316 [ blank? ] trim ; inline
318 : cut-tail ( string quot -- before after ) (trim-tail) cut ; inline
320 : cell-bytes ( -- n )
321 cell-bits 8 /i ; inline
323 : get-tokens ( tokens ntokens -- tokens )
325 _ * swap <displaced-alien>
329 : clang-get-file-max-range ( CXTranslationUnit path -- CXSourceRange )
330 [ dupd clang_getFile 0 clang_getLocationForOffset ]
331 [ dupd [ clang_getFile ] [ nip file-info size>> ] 2bi clang_getLocationForOffset ] 2bi
334 : clang-tokenize ( CXTranslationUnit CXSourceRange -- tokens ntokens )
337 [ clang_tokenize ] 2keep
341 : tokenize-path ( tu path -- tokens ntokens )
342 [ drop ] [ clang-get-file-max-range ] 2bi
345 :: with-cursor-tokens ( cursor quot: ( tu token -- obj ) -- )
346 cursor clang_Cursor_getTranslationUnit :> tu
347 tu cursor clang_getCursorExtent clang-tokenize :> ( tokens ntokens )
348 tu tokens ntokens <iota>
349 CXToken heap-size :> bytesize
352 bytesize * swap <displaced-alien> @
353 ] with with { } map-as
354 tu tokens ntokens clang_disposeTokens ; inline
356 DEFER: cursor>c-struct
357 DEFER: cursor>c-union
359 :: cursor-type ( cursor -- string )
360 cursor clang_getCursorType clang_getTypeSpelling clang-get-cstring
364 [ CHAR: * = ] cut-tail
365 [ [ trim-blanks ] dip append ] when*
369 { [ dup "struct " head? ] [
371 clang-state> unnamed-table>> ?at or
374 ! libclang uses two forms for unnamed union (why!?)
375 ! union (unnamed at /Users/erg/factor/elf2.h:39:3)
376 ! union (unnamed union at /Users/erg/factor/elf2.h:39:3)
377 { [ dup "union " head? ] [
379 clang-state> unnamed-table>> ?at or
381 { [ dup "_Bool" = ] [ drop "bool" ] }
382 { [ "int8_t" ?head ] [ trim-blanks "char" prepend ] }
383 { [ "int16_t" ?head ] [ trim-blanks "short" prepend ] }
384 { [ "int32_t" ?head ] [ trim-blanks "int" prepend ] }
385 { [ "int64_t" ?head ] [ trim-blanks "longlong" prepend ] }
386 { [ "uint8_t" ?head ] [ trim-blanks "uchar" prepend ] }
387 { [ "uint16_t" ?head ] [ trim-blanks "ushort" prepend ] }
388 { [ "uint32_t" ?head ] [ trim-blanks "uint" prepend ] }
389 { [ "uint64_t" ?head ] [ trim-blanks "ulonglong" prepend ] }
390 { [ "signed char" ?head ] [ trim-blanks "char" prepend ] }
391 { [ "signed short" ?head ] [ trim-blanks "short" prepend ] }
392 { [ "signed int" ?head ] [ trim-blanks "int" prepend ] }
393 { [ "signed long" ?head ] [ trim-blanks "long" prepend ] }
394 { [ "unsigned char" ?head ] [ trim-blanks "uchar" prepend ] }
395 { [ "unsigned short" ?head ] [ trim-blanks "ushort" prepend ] }
396 { [ "unsigned int" ?head ] [ trim-blanks "uint" prepend ] }
397 { [ "unsigned long" ?head ] [ trim-blanks "ulong" prepend ] }
398 { [ dup "(*)" swap subseq? ] [ drop "void*" ] }
402 : cursor-name ( cursor -- string )
403 clang_getCursorSpelling clang-get-cstring "Enum" ?unnamed drop ;
405 : ?cursor-name ( cursor unnamed-type -- string )
406 [ clang_getCursorSpelling clang-get-cstring ] dip ?unnamed drop ;
408 : arg-info ( cursor -- string )
409 [ cursor-type ] [ cursor-name [ "dummy" ] when-empty ] bi " " glue ;
411 : cursor>args ( CXCursor -- args/f )
412 dup clang_Cursor_getNumArguments dup -1 = [
416 clang_Cursor_getArgument
420 : cxprimitive-type>factor ( CXType -- string )
422 { CXType_Bool [ "bool" ] }
423 { CXType_Char_S [ "char" ] }
424 { CXType_Char_U [ "uchar" ] }
425 { CXType_SChar [ "char" ] }
426 { CXType_UChar [ "uchar" ] }
427 { CXType_Short [ "short" ] }
428 { CXType_UShort [ "ushort" ] }
429 { CXType_Int [ "int" ] }
430 { CXType_UInt [ "uint" ] }
431 { CXType_Long [ "long" ] }
432 { CXType_ULong [ "ulong" ] }
433 { CXType_LongLong [ "longlong" ] }
434 { CXType_ULongLong [ "ulonglong" ] }
435 { CXType_Float [ "float" ] }
436 { CXType_Double [ "double" ] }
437 { CXType_Void [ "void" ] }
441 : cxreturn-type>factor ( CXType -- string )
443 { [ dup kind>> CXType_Pointer = ] [
444 clang_getPointeeType cxreturn-type>factor "*" append
446 { [ dup kind>> CXType_Elaborated = ] [
447 clang_getCanonicalType cxreturn-type>factor
449 { [ dup kind>> CXType_Record = ] [
450 clang_getTypeDeclaration cursor-name
452 { [ dup kind>> CXType_FunctionProto = ] [
453 ! inside a CXType_Pointer, so we get `void*` from that case
456 [ kind>> cxprimitive-type>factor ]
459 : cursor>args-info ( CXCursor -- args-info )
460 cursor>args [ arg-info ] map ", " join ;
462 : cursor>c-function ( CXCursor -- )
463 [ clang_getCursorResultType cxreturn-type>factor ]
465 [ cursor>args-info ] tri <c-function> set-definition ;
467 : cursor>c-typedef ( CXCursor -- )
468 [ clang_getTypedefDeclUnderlyingType cxreturn-type>factor ]
469 [ cursor-name ] bi <c-typedef> [ set-definition ] [ set-typedef ] bi ;
471 : cursor>c-field ( CXCursor -- )
472 [ cursor-name ] [ cursor-type ] bi <c-field> push-child-form ;
474 DEFER: cursor-visitor
476 : cursor>enum ( CXCursor -- )
478 [ cursor-name ] [ cursor-visitor ] bi
479 f clang_visitChildren drop
480 ] with-new-form <c-enum> set-definition ;
482 : cursor>c-union ( CXCursor -- )
484 [ "Union" ?cursor-name ] keep
485 cursor-visitor f clang_visitChildren drop
487 <c-union> set-definition ;
489 : cursor>c-struct ( CXCursor -- )
491 [ "Struct" ?cursor-name ] keep
492 cursor-visitor f clang_visitChildren drop
494 <c-struct> set-definition ;
496 : cursor-visitor ( -- callback )
499 dup clang_getCursorKind
500 ! dup "cursor-visitor got: " gwrite g... gflush
502 { CXCursor_Namespace [ drop CXChildVisit_Recurse ] }
503 { CXCursor_FunctionDecl [ cursor>c-function CXChildVisit_Continue ] }
504 { CXCursor_TypedefDecl [ cursor>c-typedef CXChildVisit_Continue ] }
505 { CXCursor_UnionDecl [ cursor>c-union CXChildVisit_Continue ] }
506 { CXCursor_StructDecl [ cursor>c-struct CXChildVisit_Continue ] }
507 { CXCursor_EnumDecl [ cursor>enum CXChildVisit_Continue ] }
508 { CXCursor_VarDecl [ drop CXChildVisit_Continue ] }
510 { CXCursor_FieldDecl [
511 cursor>c-field CXChildVisit_Continue
513 { CXCursor_EnumConstantDecl [
516 clang_getTokenSpelling clang-get-cstring
520 clang_getEnumConstantDeclUnsignedValue number>string
522 <c-field> push-child-form
523 CXChildVisit_Continue
525 { CXCursor_UnexposedDecl [ drop CXChildVisit_Continue ] }
527 "cursor-visitor unhandled: " gwrite dup g... gflush
528 2drop CXChildVisit_Recurse
534 : with-clang-index ( quot: ( index -- ) -- )
535 [ 0 0 clang_createIndex ] dip keep clang_disposeIndex ; inline
537 : with-clang-translation-unit ( idx source-file command-line-args nargs unsaved-files nunsaved-files options quot: ( tu -- ) -- )
538 [ enum>number clang_parseTranslationUnit ] dip
539 keep clang_disposeTranslationUnit ; inline
541 : with-clang-default-translation-unit ( path quot: ( tu path -- ) -- )
543 _ f 0 f 0 CXTranslationUnit_None [
545 ] with-clang-translation-unit
546 ] with-clang-index ; inline
548 : with-clang-cursor ( path quot: ( tu path cursor -- ) -- )
550 _ f 0 f 0 CXTranslationUnit_None [
551 _ over clang_getTranslationUnitCursor @
552 ] with-clang-translation-unit
553 ] with-clang-index ; inline
555 : parse-c-exports ( path -- )
557 2nip cursor-visitor f clang_visitChildren drop
558 ] with-clang-cursor ;
560 : write-c-defs ( clang-state -- )
564 [ def>out-forms ] each
567 [ members [ length ] inv-sort-by ] assoc-map
568 ] change-out-forms-by-name
570 sort-keys values [ print ] each
573 : parse-include ( path -- libclang-state )
574 <libclang-state> clang-state [
577 ] with-output-global-variable
581 : parse-hpp-files ( path -- assoc )
582 ?qualified-directory-files
583 [ ".hpp" tail? ] filter
584 [ parse-include ] zip-with ;
586 : parse-h-files ( path -- assoc )
587 ?qualified-directory-files
588 [ ".h" tail? ] filter
589 [ parse-include ] zip-with ;
591 : parse-cpp-files ( path -- assoc )
592 ?qualified-directory-files
593 [ ".cpp" tail? ] filter
594 [ parse-include ] zip-with ;