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.encodings.utf8
7 io.files.info kernel layouts libc libclang.ffi make math
8 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-table unnamed-counter
20 out-forms-counter out-forms out-forms-written ;
22 : <libclang-state> ( -- state )
25 H{ } clone >>c-defs-by-name
26 H{ } clone >>c-defs-by-order
28 H{ } clone >>child-forms
30 H{ } clone >>unnamed-table
32 H{ } clone >>out-forms
33 HS{ } clone >>out-forms-written ;
35 GENERIC: def>out-form ( obj -- string )
37 : save-out-form ( string -- )
39 dup clang-state> out-forms-written>> in? [
44 [ out-forms-counter>> counter ]
45 [ out-forms>> set-at ] bi
47 [ out-forms-written>> adjoin ] 2bi
51 ! some forms must be defined out of order, e.g. anonymous unions/structs
52 : def>out-forms ( obj -- )
53 def>out-form save-out-form ;
55 : peek-current-form ( -- n )
56 clang-state> c-forms>> ?last ; inline
61 : push-child-form ( form -- )
62 ! dup order>> c-defs-by-order get-global set-at ; inline
63 dup parent-order>> clang-state> child-forms>> push-at ; inline
65 : with-new-form ( quot -- n )
66 clang-state> [ defs-counter>> counter ] [ c-forms>> ] bi push
68 clang-state> c-forms>> pop ; inline
70 ERROR: unknown-form name ;
71 GENERIC: print-deferred ( obj -- )
72 M: object print-deferred
73 type>> clang-state> c-defs-by-name>> ?at [ def>out-forms ] [ unknown-form ] if ;
75 : unnamed? ( string -- ? ) "(unnamed" swap subseq? ; inline
76 : append-counter ( string counter -- string' ) counter number>string append ; inline
77 : unnamed-exists? ( string -- value/key ? ) clang-state> unnamed-table>> ?at ; inline
78 : lookup-unnamed ( type string -- type-name )
82 [ clang-state> unnamed-counter>> append-counter ] dip
84 "RECORDING: " gwrite dup g... gflush
85 [ clang-state> unnamed-table>> set-at ] keepd
88 : ?unnamed ( string type -- string' ? )
96 { return-type string }
101 : <c-function> ( return-type name args -- c-function )
106 clang-state> defs-counter>> counter >>order ;
113 : <c-struct> ( name order -- c-struct )
123 : <c-union> ( name order -- c-union )
134 : <c-enum> ( name order -- c-enum )
143 { parent-order integer }
146 : <c-arg> ( name type -- c-arg )
150 peek-current-form >>parent-order
151 clang-state> defs-counter>> counter >>order ;
157 { parent-order integer }
160 : <c-field> ( name type -- c-field )
164 peek-current-form >>parent-order
165 clang-state> defs-counter>> counter >>order ;
173 : <c-typedef> ( type name -- c-typedef )
177 clang-state> defs-counter>> counter >>order ;
179 M: c-function def>out-form
182 [ drop "FUNCTION: " ]
183 [ return-type>> " " ]
185 [ args>> dup empty? ")\n" " )\n" ? ]
187 ] "" append-outputs-as ;
189 : ignore-typedef? ( typedef -- ? )
190 [ type>> ] [ name>> ] bi
191 { [ = ] [ [ empty? ] either? ] } 2|| ;
193 M: c-typedef def>out-form
194 dup ignore-typedef? [
203 ] "" append-outputs-as
206 ERROR: unknown-child-forms order ;
207 M: c-field def>out-form
214 ] "" append-outputs-as ;
216 : lookup-order ( obj -- order )
217 type>> clang-state> c-defs-by-name>> at [ order>> ] ?call -1 or ;
219 : print-defers ( current-order slots -- )
221 tuck lookup-order < [
228 : empty-struct? ( c-struct -- ? )
229 order>> clang-state> child-forms>> key? not ;
231 M: c-struct def>out-form
233 name>> "C-TYPE: " prepend
240 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
242 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
245 ] "" append-outputs-as
248 M: c-enum def>out-form
254 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
256 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
259 ] "" append-outputs-as ;
261 M: c-union def>out-form
264 [ drop "UNION-STRUCT: " ]
267 order>> dup clang-state> child-forms>> ?at [ drop { } ] unless
269 [ nip [ def>out-form ] map "\n" join " ;\n" append ] 2bi
272 ] "" append-outputs-as ;
274 M: object def>out-form
275 class-of name>> "unknown object: " prepend ;
277 : set-definition ( named -- )
278 [ dup name>> clang-state> c-defs-by-name>> set-at ]
279 [ dup order>> clang-state> c-defs-by-order>> set-at ] bi ;
281 : clang-get-cstring ( CXString -- string )
282 clang_getCString [ utf8 alien>string ] [ clang_disposeString ] bi ;
284 : trim-blanks ( string -- string' )
285 [ blank? ] trim ; inline
287 : cut-tail ( string quot -- before after ) (trim-tail) cut ; inline
289 : cell-bytes ( -- n )
290 cell-bits 8 /i ; inline
292 : get-tokens ( tokens ntokens -- tokens )
294 _ * swap <displaced-alien>
298 : clang-get-file-max-range ( CXTranslationUnit path -- CXSourceRange )
299 [ dupd clang_getFile 0 clang_getLocationForOffset ]
300 [ dupd [ clang_getFile ] [ nip file-info size>> ] 2bi clang_getLocationForOffset ] 2bi
303 : clang-tokenize ( CXTranslationUnit CXSourceRange -- tokens ntokens )
306 [ clang_tokenize ] 2keep
310 : tokenize-path ( tu path -- tokens ntokens )
311 [ drop ] [ clang-get-file-max-range ] 2bi
314 : tokenize-translation-unit ( CXTranslationUnit -- tokens ntokens )
315 [ ] [ clang_getTranslationUnitCursor clang_getCursorExtent ] bi
318 : tokenize-cursor ( cursor -- tokens ntokens )
319 [ clang_Cursor_getTranslationUnit ] [ clang_getCursorExtent ] bi
322 : dispose-tokens ( cursor tokens ntokens -- )
323 [ clang_Cursor_getTranslationUnit ] 2dip clang_disposeTokens ;
325 :: with-cursor-tokens ( cursor quot: ( tu token -- obj ) -- )
326 cursor clang_Cursor_getTranslationUnit :> tu
327 cursor tokenize-cursor :> ( tokens ntokens )
328 tokens ntokens <iota>
329 cell-bytes :> bytesize
332 [ tu ] 2dip bytesize * swap <displaced-alien> @
334 tu tokens ntokens dispose-tokens ; inline
336 : clang-get-token-spelling ( CXTranslationUnit CXToken -- string )
337 clang_getTokenSpelling clang-get-cstring ;
339 DEFER: cursor>c-struct
340 DEFER: cursor>c-union
342 :: cursor-type ( cursor -- string )
343 cursor clang_getCursorType clang_getTypeSpelling clang-get-cstring
347 [ CHAR: * = ] cut-tail
348 [ [ trim-blanks ] dip append ] when*
352 { [ dup "struct " head? ] [
354 clang-state> unnamed-table>> ?at or
357 ! libclang uses two forms for unnamed union (why!?)
358 ! union (unnamed at /Users/erg/factor/elf2.h:39:3)
359 ! union (unnamed union at /Users/erg/factor/elf2.h:39:3)
360 { [ dup "union " head? ] [
362 clang-state> unnamed-table>> ?at or
364 { [ dup "_Bool" = ] [ drop "bool" ] }
365 { [ "int8_t" ?head ] [ trim-blanks "char" prepend ] }
366 { [ "int16_t" ?head ] [ trim-blanks "short" prepend ] }
367 { [ "int32_t" ?head ] [ trim-blanks "int" prepend ] }
368 { [ "int64_t" ?head ] [ trim-blanks "longlong" prepend ] }
369 { [ "uint8_t" ?head ] [ trim-blanks "uchar" prepend ] }
370 { [ "uint16_t" ?head ] [ trim-blanks "ushort" prepend ] }
371 { [ "uint32_t" ?head ] [ trim-blanks "uint" prepend ] }
372 { [ "uint64_t" ?head ] [ trim-blanks "ulonglong" prepend ] }
373 { [ "signed char" ?head ] [ trim-blanks "char" prepend ] }
374 { [ "signed short" ?head ] [ trim-blanks "short" prepend ] }
375 { [ "signed int" ?head ] [ trim-blanks "int" prepend ] }
376 { [ "signed long" ?head ] [ trim-blanks "long" prepend ] }
377 { [ "unsigned char" ?head ] [ trim-blanks "uchar" prepend ] }
378 { [ "unsigned short" ?head ] [ trim-blanks "ushort" prepend ] }
379 { [ "unsigned int" ?head ] [ trim-blanks "uint" prepend ] }
380 { [ "unsigned long" ?head ] [ trim-blanks "ulong" prepend ] }
381 { [ dup "(*)" swap subseq? ] [ drop "void*" ] }
385 : cursor-name ( cursor -- string )
386 clang_getCursorSpelling clang-get-cstring "" ?unnamed drop ;
388 : ?cursor-name ( cursor unnamed-type -- string )
389 [ clang_getCursorSpelling clang-get-cstring ] dip ?unnamed drop ;
391 : arg-info ( cursor -- string )
392 [ cursor-type ] [ cursor-name [ "dummy" ] when-empty ] bi " " glue ;
394 : cursor>args ( CXCursor -- args/f )
395 dup clang_Cursor_getNumArguments dup -1 = [
399 clang_Cursor_getArgument
403 : cxprimitive-type>factor ( CXType -- string )
405 { CXType_Bool [ "bool" ] }
406 { CXType_Char_S [ "char" ] }
407 { CXType_Char_U [ "uchar" ] }
408 { CXType_SChar [ "char" ] }
409 { CXType_UChar [ "uchar" ] }
410 { CXType_Short [ "short" ] }
411 { CXType_UShort [ "ushort" ] }
412 { CXType_Int [ "int" ] }
413 { CXType_UInt [ "uint" ] }
414 { CXType_Long [ "long" ] }
415 { CXType_ULong [ "ulong" ] }
416 { CXType_LongLong [ "longlong" ] }
417 { CXType_ULongLong [ "ulonglong" ] }
418 { CXType_Float [ "float" ] }
419 { CXType_Double [ "double" ] }
420 { CXType_Void [ "void" ] }
424 : cxreturn-type>factor ( CXType -- string )
426 { [ dup kind>> CXType_Pointer = ] [
427 clang_getPointeeType dup g... gflush cxreturn-type>factor "*" append
429 { [ dup kind>> CXType_Elaborated = ] [
430 clang_getCanonicalType cxreturn-type>factor
432 { [ dup kind>> CXType_Record = ] [
433 clang_getTypeDeclaration cursor-name
435 { [ dup kind>> CXType_FunctionProto = ] [
436 ! inside a CXType_Pointer, so we get `void*` from that case
439 [ kind>> cxprimitive-type>factor ]
442 : cursor>args-info ( CXCursor -- args-info )
443 cursor>args [ arg-info ] map ", " join ;
445 : cursor>c-function ( CXCursor -- )
446 [ clang_getCursorResultType cxreturn-type>factor ]
448 [ cursor>args-info ] tri <c-function> set-definition ;
450 : cursor>c-typedef ( CXCursor -- )
451 [ clang_getTypedefDeclUnderlyingType cxreturn-type>factor ]
452 [ cursor-name ] bi <c-typedef> set-definition ;
454 : cursor>c-field ( CXCursor -- )
455 [ cursor-name ] [ cursor-type ] bi <c-field> dup g... gflush push-child-form ;
457 DEFER: cursor-visitor
459 : cursor>enum ( CXCursor -- )
461 [ cursor-name ] [ cursor-visitor ] bi
462 f clang_visitChildren drop
463 ] with-new-form <c-enum> set-definition ;
465 : cursor>c-union ( CXCursor -- )
467 "cursor>union start" g...
468 peek-current-form g... gflush
470 [ "Union" ?cursor-name "name: " gwrite dup g... gflush ] keep
471 cursor-visitor f clang_visitChildren drop
473 "cursor>union finish" g... gflush
474 peek-current-form g... gflush
476 <c-union> dup g... gflush set-definition ;
478 : cursor>c-struct ( CXCursor -- )
480 "cursor>c-struct start" g...
481 peek-current-form g... gflush
483 [ "Struct" ?cursor-name ] keep
484 cursor-visitor f clang_visitChildren drop
486 "cursor>c-struct finish" g... gflush
487 peek-current-form g... gflush
489 <c-struct> dup g... gflush set-definition ;
491 : cursor-visitor ( -- callback )
494 dup clang_getCursorKind
495 dup "cursor-visitor got: " gwrite g... gflush
497 { CXCursor_Namespace [ drop CXChildVisit_Recurse ] }
498 { CXCursor_FunctionDecl [ cursor>c-function CXChildVisit_Continue ] }
499 { CXCursor_TypedefDecl [ cursor>c-typedef CXChildVisit_Continue ] }
500 { CXCursor_UnionDecl [ cursor>c-union CXChildVisit_Continue ] }
501 { CXCursor_StructDecl [ cursor>c-struct CXChildVisit_Continue ] }
502 { CXCursor_EnumDecl [ cursor>enum CXChildVisit_Continue ] }
503 { CXCursor_VarDecl [ drop CXChildVisit_Continue ] }
505 { CXCursor_FieldDecl [
506 cursor>c-field CXChildVisit_Continue
508 { CXCursor_EnumConstantDecl [
510 [ clang-get-token-spelling ] with-cursor-tokens
513 clang_getEnumConstantDeclUnsignedValue number>string
515 <c-field> push-child-form
516 CXChildVisit_Continue
519 "cursor-visitor unhandled: " gwrite dup g... gflush
520 2drop CXChildVisit_Recurse
526 : with-clang-index ( quot: ( index -- ) -- )
527 [ 0 0 clang_createIndex ] dip keep clang_disposeIndex ; inline
529 : with-clang-translation-unit ( idx source-file command-line-args nargs unsaved-files nunsaved-files options quot: ( tu -- ) -- )
530 [ enum>number clang_parseTranslationUnit ] dip
531 keep clang_disposeTranslationUnit ; inline
533 : with-clang-default-translation-unit ( path quot: ( tu path -- ) -- )
535 _ f 0 f 0 CXTranslationUnit_None [
537 ] with-clang-translation-unit
538 ] with-clang-index ; inline
540 : with-clang-cursor ( path quot: ( tu path cursor -- ) -- )
542 _ f 0 f 0 CXTranslationUnit_None [
543 _ over clang_getTranslationUnitCursor @
544 ] with-clang-translation-unit
545 ] with-clang-index ; inline
547 ! : parse-c-defines ( path -- )
552 ! cell-bits 8 /i * swap <displaced-alien>
555 ! ] with-clang-default-translation-unit ;
557 : parse-c-exports ( path -- )
559 2nip cursor-visitor f clang_visitChildren drop
560 ] with-clang-cursor ;
562 : write-c-defs ( -- )
563 clang-state> c-defs-by-order>>
565 [ def>out-forms ] each
566 clang-state> out-forms>>
567 sort-keys values [ print ] each ;
569 : parse-include ( path -- libclang-state )
570 <libclang-state> clang-state [
574 ! [ parse-c-defines ]
578 ] with-output-global-variable ; inline
582 ! "/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/php/ext/sqlite3/libsqlite/sqlite3.h" parse-include
584 ! "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/include"
586 ! "resource:elf.h" parse-include
589 "resource:elf.h" parse-include
590 c-defs-by-order get-global write-c-defs
592 "resource:elf2.h" parse-include