]> gitweb.factorcode.org Git - factor.git/commitdiff
libclang: fix ffi generation
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 14 Dec 2023 21:56:54 +0000 (15:56 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 15 Dec 2023 18:52:58 +0000 (12:52 -0600)
- still need to fix callbacks

extra/libclang/ffi/ffi.factor
extra/libclang/libclang.factor

index 7e21089193fd065a3ec4b8af6adc6f547dc2ff4f..804166174aa9912b5aa8f3234ea8a74e768f1276 100644 (file)
@@ -14,9 +14,8 @@ LIBRARY: clang
 
 CONSTANT: UINT_MAX 4294967295
 
-TYPEDEF: void* CXTranslationUnitImpl
 TYPEDEF: void* CXIndex
-TYPEDEF: CXTranslationUnitImpl* CXTranslationUnit
+TYPEDEF: void* CXTranslationUnit
 TYPEDEF: void* CXClientData
 TYPEDEF: void* CXFile
 
@@ -202,16 +201,24 @@ STRUCT: CXStringSet
     { Strings CXString* }
     { Count uint } ;
 
-TYPEDEF: void* CXSourceLocation
-! STRUCT: CXSourceLocation
-!     { ptr_data void*[2]  }
-!     { data uint } ;
+STRUCT: CXSourceLocation
+    { ptr_data void*[2]  }
+    { data uint } ;
 
-TYPEDEF: void* CXSourceRange
-! STRUCT: CXSourceRange
-!     { ptr_data void*[2]  }
-!     { begin_int_data uint }
-!     { end_int_data uint } ;
+STRUCT: CXSourceRange
+    { ptr_data void*[2]  }
+    { begin_int_data uint }
+    { end_int_data uint } ;
+
+STRUCT: CXSourceRangeList
+    { count uint }
+    { ranges CXSourceRange* } ;
+
+STRUCT: CXDiagnosticSet
+    { data void* } ;
+
+STRUCT: CXDiagnostic
+    { data void*[3] } ;
 
 FUNCTION: CXIndex clang_createIndex ( int excludeDeclarationsFromPCH, int displayDiagnostics )
 
@@ -224,9 +231,6 @@ FUNCTION: CXTranslationUnit clang_parseTranslationUnit (
 FUNCTION: void clang_disposeIndex ( CXIndex index )
 FUNCTION: void clang_disposeTranslationUnit ( CXTranslationUnit c )
 
-: with-clang-index ( quot: ( index -- ) -- )
-    [ 0 0 clang_createIndex ] dip keep clang_disposeIndex ; inline
-
 FUNCTION: CXCursor clang_getTranslationUnitCursor ( CXTranslationUnit c )
 
 FUNCTION: CXCursorKind clang_getCursorKind ( CXCursor c )
@@ -237,7 +241,7 @@ FUNCTION: CXString clang_getCursorKindSpelling ( CXCursorKind Kind )
 FUNCTION: void clang_getDefinitionSpellingAndExtent (
     CXCursor cursor, char **startBuf, char **endBuf, uint *startLine,
     uint *startColumn, uint *endLine, uint *endColumn
-    )
+)
 FUNCTION: void clang_enableStackTraces ( )
 FUNCTION: void clang_executeOnThread ( void* fn, void *user_data, uint stack_size )
 
@@ -269,8 +273,49 @@ FUNCTION: int clang_Cursor_getNumArguments ( CXCursor C )
 FUNCTION: CXType clang_getArgType ( CXType C, uint i )
 FUNCTION: CXCursor clang_Cursor_getArgument ( CXCursor C, uint i )
 
-FUNCTION: CXFile clang_getFile ( CXTranslationUnit tu, char* file_name )
+FUNCTION: CXFile clang_getFile ( CXTranslationUnit tu, c-string file_name )
+FUNCTION: CXString clang_getFileName ( CXFile SFile )
+FUNCTION: uint clang_getFileTime ( CXFile SFile )
+FUNCTION: CXSourceLocation clang_getLocation ( CXTranslationUnit tu, CXFile file, uint line, uint column )
+FUNCTION: CXSourceLocation clang_getNullLocation ( )
+FUNCTION: uint clang_equalLocations ( CXSourceLocation loc1, CXSourceLocation loc2 )
 FUNCTION: CXSourceLocation clang_getLocationForOffset ( CXTranslationUnit tu, CXFile file, uint offset )
+FUNCTION: int clang_Location_isInSystemHeader ( CXSourceLocation location )
+FUNCTION: int clang_Location_isFromMainFile ( CXSourceLocation location )
+FUNCTION: CXSourceRange clang_getNullRange ( )
+FUNCTION: int clang_equalRanges ( CXSourceRange range1, CXSourceRange range2 )
+FUNCTION: int clang_Range_isNull ( CXSourceRange range )
+FUNCTION: void clang_getExpansionLocation (
+    CXSourceLocation location, CXFile *file, uint *line,
+    uint *column, uint *offset
+)
+FUNCTION: void clang_getPresumedLocation (
+    CXSourceLocation location, CXString *filename, uint *line,
+    uint *column
+)
+FUNCTION: void clang_getInstantiationLocation (
+    CXSourceLocation location, CXFile *file, uint *line,
+    uint *column, uint *offset
+    )
+FUNCTION: void clang_getSpellingLocation (
+    CXSourceLocation location, CXFile *file, uint *line,
+    uint *column, uint *offset
+)
+FUNCTION: void clang_getFileLocation (
+    CXSourceLocation location, CXFile *file, uint *line,
+    uint *column, uint *offset
+)
+
+FUNCTION: CXFile clang_getIncludedFile ( CXCursor cursor )
+FUNCTION: CXSourceLocation clang_getCursorLocation ( CXCursor cursor )
+FUNCTION: CXSourceRange clang_getCursorExtent ( CXCursor cursor )
+FUNCTION: CXSourceLocation clang_getRangeStart ( CXSourceRange range )
+FUNCTION: CXSourceLocation clang_getRangeEnd ( CXSourceRange range )
+FUNCTION: CXSourceRangeList* clang_getSkippedRanges ( CXTranslationUnit tu, CXFile file )
+FUNCTION: CXSourceRangeList* clang_getAllSkippedRanges ( CXTranslationUnit tu )
+FUNCTION: void clang_disposeSourceRangeList ( CXSourceRangeList *ranges )
+FUNCTION: CXDiagnosticSet clang_getDiagnosticSetFromTU ( CXTranslationUnit Unit )
+
 FUNCTION: void clang_tokenize ( CXTranslationUnit tu, CXSourceRange range, CXToken **tokens, uint *numTokens )
 FUNCTION: void clang_disposeTokens ( CXTranslationUnit tu, CXToken *tokens, uint numTokens )
 FUNCTION: CXTokenKind clang_getTokenKind ( CXToken token )
@@ -284,15 +329,36 @@ FUNCTION: CXString clang_constructUSR_ObjCIvar ( char *name, CXString classUSR )
 FUNCTION: CXString clang_constructUSR_ObjCMethod ( char *name, uint isInstanceMethod, CXString classUSR )
 FUNCTION: CXString clang_constructUSR_ObjCProperty ( char *property, CXString classUSR )
 
+FUNCTION: CXCursor clang_getTypeDeclaration ( CXType T )
+FUNCTION: uint clang_getNumFields ( CXType T )
+FUNCTION: CXCursor clang_getFieldDecl ( CXType T, uint i )
+FUNCTION: uint clang_Cursor_getNumTemplateArguments ( CXCursor C )
+FUNCTION: CXType clang_Cursor_getTemplateArgumentType ( CXCursor C, uint i )
+FUNCTION: int clang_Cursor_getTemplateArgumentValue ( CXCursor C, uint i )
+FUNCTION: CXCursor clang_Cursor_getTemplateArgumentCursor ( CXCursor C, uint i )
+FUNCTION: uint clang_Cursor_getNumSpecializations ( CXCursor C )
+FUNCTION: CXCursor clang_Cursor_getSpecialization ( CXCursor C, uint i )
+FUNCTION: CXSourceRange clang_Cursor_getCommentRange ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getRawCommentText ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getBriefCommentText ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getMangling ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getCXXManglings ( CXCursor C )
+FUNCTION: CXStringSet* clang_Cursor_getObjCManglings ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getObjCSelectorIndexName ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getObjCPropertyGetterName ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getObjCPropertySetterName ( CXCursor C )
+FUNCTION: CXString clang_Cursor_getObjCDeclQualifiers ( CXCursor C )
+FUNCTION: uint clang_Cursor_isObjCOptional ( CXCursor C )
+FUNCTION: uint clang_Cursor_isVariadic ( CXCursor C )
+
+FUNCTION: CXString clang_getClangVersion ( )
 FUNCTION: CXSourceRange clang_getRange ( CXSourceLocation begin, CXSourceLocation end )
-FUNCTION: c-string clang_getCString ( CXString string )
+FUNCTION: char* clang_getCString ( CXString string )
 FUNCTION: void clang_disposeString ( CXString string )
 FUNCTION: void clang_disposeStringSet ( CXStringSet *set )
 
-
-
 FUNCTION: uint clang_visitChildren (
     CXCursor parent,
     CXCursorVisitor visitor,
     CXClientData client_data
-    )
+)
index 787880310e73c6f3c60cf17881e05893be243575..817813fddf16f92844c8e1b3e6ace9a920bcc727 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2022 Doug Coleman.
 ! See https://factorcode.org/license.txt for BSD license.
-USING: accessors alien.c-types alien.data alien.enums ascii
-classes.struct combinators combinators.smart discord io
-io.backend kernel libclang.ffi sequences splitting ;
+USING: accessors alien alien.c-types alien.data alien.enums
+alien.strings ascii classes.struct combinators combinators.smart
+discord io io.backend io.encodings.utf8 io.files.info kernel
+layouts libclang.ffi math sequences splitting strings ;
 IN: libclang
 
 : function-arg-cursor-visitor ( -- callback )
@@ -18,20 +19,23 @@ IN: libclang
         [ 2drop f ]
     } case ;
 
+: clang-get-cstring ( CXString -- string )
+    clang_getCString [ utf8 alien>string ] [ clang_disposeString ] bi ;
+
 : remove-const ( strinng -- string' )
     "const" split1 [ [ blank? ] trim ] bi@ " " glue [ blank? ] trim ;
 
 : cursor-type ( cursor -- string )
     ! [ "cursor display name" g... clang_getCursorDisplayName g... ] keep
     clang_getCursorType
-    clang_getTypeSpelling clang_getCString ! "type spelling c string" g... dup g...
+    clang_getTypeSpelling clang-get-cstring ! "type spelling c string" g... dup g...
     remove-const ;
 
 : cursor-name ( cursor -- string )
-    clang_getCursorSpelling data>> ;
+    clang_getCursorSpelling clang-get-cstring ;
 
 : arg-info ( cursor -- string )
-    [ cursor-type ] [ cursor-name ] bi " " glue ;
+    [ cursor-type ] [ cursor-name [ "dummy" ] when-empty ] bi " " glue ;
 
 : cursor>args ( cursor -- args/f )
     dup clang_Cursor_getNumArguments dup -1 = [
@@ -42,34 +46,40 @@ IN: libclang
         ] with { } map-as
     ] if ;
 
-: cxreturn-type>factor ( type -- string )
-    dup kind>> dup CXType_Pointer = [
-        drop
-        clang_getPointeeType
-        cxreturn-type>factor "*" append
-    ] [
-        nip
-        {
-            { CXType_Bool [ "bool" ] }
-            { CXType_Char_S [ "char" ] }
-            { CXType_Char_U [ "uchar" ] }
-            { CXType_SChar [ "char" ] }
-            { CXType_UChar [ "uchar" ] }
-            { CXType_Short [ "short" ] }
-            { CXType_UShort [ "ushort" ] }
-            { CXType_Int [ "int" ] }
-            { CXType_UInt [ "uint" ] }
-            { CXType_Long [ "long" ] }
-            { CXType_ULong [ "ulong" ] }
-            { CXType_LongLong [ "longlong" ] }
-            { CXType_ULongLong [ "ulonglong" ] }
-            { CXType_Float [ "float" ] }
-            { CXType_Double [ "double" ] }
-            { CXType_Void [ "void" ] }
-            ! { CXType_Pointer [ "*" ] }
-            [ drop "" ]
-        } case
-    ] if ;
+: cxprimitive-type>factor ( type -- string )
+    {
+        { CXType_Bool [ "bool" ] }
+        { CXType_Char_S [ "char" ] }
+        { CXType_Char_U [ "uchar" ] }
+        { CXType_SChar [ "char" ] }
+        { CXType_UChar [ "uchar" ] }
+        { CXType_Short [ "short" ] }
+        { CXType_UShort [ "ushort" ] }
+        { CXType_Int [ "int" ] }
+        { CXType_UInt [ "uint" ] }
+        { CXType_Long [ "long" ] }
+        { CXType_ULong [ "ulong" ] }
+        { CXType_LongLong [ "longlong" ] }
+        { CXType_ULongLong [ "ulonglong" ] }
+        { CXType_Float [ "float" ] }
+        { CXType_Double [ "double" ] }
+        { CXType_Void [ "void" ] }
+        [ drop "" ]
+    } case ;
+
+: cxreturn-type>factor ( CXType -- string )
+    {
+        { [ dup kind>> CXType_Pointer = ] [
+            clang_getPointeeType cxreturn-type>factor "*" append
+        ] }
+        { [ dup kind>> CXType_Elaborated = ] [
+            clang_getCanonicalType cxreturn-type>factor
+        ] }
+        { [ dup kind>> CXType_Record = ] [
+            clang_getTypeDeclaration clang_getCursorSpelling clang-get-cstring
+        ] }
+        [ kind>> cxprimitive-type>factor ]
+    } cond ;
 
 : cursor>args-info ( cursor -- args-info )
     cursor>args [ arg-info ] map ", " join ;
@@ -78,27 +88,21 @@ IN: libclang
     [
         {
             [ drop "FUNCTION: " ]
-            [ clang_getCursorType clang_getResultType cxreturn-type>factor ]
+            [ clang_getCursorResultType cxreturn-type>factor ]
             [ drop " " ]
-            [ clang_getCursorSpelling data>> ]
+            [ clang_getCursorSpelling clang-get-cstring ]
             [ drop " ( " ]
             [ cursor>args-info dup empty? ")" " )" ? ]
-            ! [ drop " )" ]
         } cleave
     ] "" append-outputs-as ;
 
 : cursor-visitor ( -- callback )
     [
-        2drop
-        dup clang_getCursorKind
-        ! dup g...
+        2drop dup clang_getCursorKind
         {
             { CXCursor_FunctionDecl [ function-cursor>string gprint ] }
-            ! { CXType_Pointer [ function-cursor>string  ] }
-            ! { CXType_Invalid [ drop ] }
             [ 2drop ]
         } case
-        ! nl nl nl
         gflush
         CXChildVisit_Recurse
     ] CXCursorVisitor ;
@@ -106,72 +110,61 @@ IN: libclang
 ! "resource:vm/factor.hpp" parse-include
 ! "C:\\Program Files\\LLVM\\include\\clang-c\\index.h"
 
+: with-clang-index ( quot: ( index -- ) -- )
+    [ 0 0 clang_createIndex ] dip keep clang_disposeIndex ; inline
+
+: with-clang-translation-unit ( idx source-file command-line-args nargs unsaved-files nunsaved-files options quot: ( tu -- ) -- )
+    [ enum>number clang_parseTranslationUnit ] dip
+    keep clang_disposeTranslationUnit ; inline
+
+: with-clang-default-translation-unit ( path quot: ( path tu -- ) -- )
+    dupd '[
+        _ f 0 f 0 CXTranslationUnit_None [
+            [ _ ] dip @
+        ] with-clang-translation-unit
+    ] with-clang-index ; inline
+
+: with-clang-cursor ( path quot: ( path tu cursor -- ) -- )
+    dupd '[
+        _ f 0 f 0 CXTranslationUnit_None [
+            [ _ ] dip dup clang_getTranslationUnitCursor @
+        ] with-clang-translation-unit
+    ] with-clang-index ; inline
+
 : clang-get-file-max-range ( CXTranslationUnit path -- CXSourceRange )
-    dupd clang_getFile
-    [ 0 clang_getLocationForOffset ]
-    [ 1000 clang_getLocationForOffset ] 2bi
+    [ dupd clang_getFile 0 clang_getLocationForOffset ]
+    [ dupd [ clang_getFile ] [ nip file-info size>> ] 2bi clang_getLocationForOffset ] 2bi
     clang_getRange ;
 
 : parse-c-defines ( path -- )
-    dup '[
-        _
-        f 0
-        f 0
-        CXTranslationUnit_None enum>number
-        clang_parseTranslationUnit
-        [ ]
-        [ _ clang-get-file-max-range ] bi
-        ! CXToken 
+    [
+        swap
+        ! tu path
+        dupd clang-get-file-max-range ! tu CXRange
         f void* <ref>
         0 uint <ref>
         [ clang_tokenize ] 2keep
-        [ g... ] bi@
-    ] with-clang-index ;
+        [ void* deref ]
+        [ uint deref <iota> ] bi*
+        [
+            ! tu void* int
+            cell-bits 8 /i * swap <displaced-alien>
+            clang_getTokenKind
+        ] with { } map-as
+        g... gflush
+    ] with-clang-default-translation-unit ;
 
 : parse-c-exports ( path -- )
-    '[
-        _
-        f 0 f 0 CXTranslationUnit_None enum>number
-        clang_parseTranslationUnit
-        clang_getTranslationUnitCursor
-        cursor-visitor f
-        clang_visitChildren drop
-    ] with-clang-index ;
+    [
+        2nip cursor-visitor f clang_visitChildren drop
+    ] with-clang-cursor ;
 
 ! "resource:vm/factor.hpp" parse-include
 : parse-include ( path -- )
     normalize-path
     {
         ! [ parse-c-defines ]
-
         [ parse-c-exports ]
     } cleave ;
 
-
-
-
-    ! CXToken *tokens;
-    ! unsigned numTokens;
-    ! clang_tokenize(unit, range, &tokens, &numTokens);
-
-    ! for (unsigned i = 0; i < numTokens; i++) {
-    !     CXTokenKind kind = clang_getTokenKind(tokens[i]);
-    !     if (kind == CXToken_Comment) {
-    !         continue;
-    !     }
-
-    !     CXString spelling = clang_getTokenSpelling(unit, tokens[i]);
-    !     const char *text = clang_getCString(spelling);
-    !     if (kind == CXToken_Punctuation && strcmp(text, "#") == 0 && i + 1 < numTokens) {
-    !         CXString nextSpelling = clang_getTokenSpelling(unit, tokens[i + 1]);
-    !         const char *nextText = clang_getCString(nextSpelling);
-    !         if (strcmp(nextText, "define") == 0) {
-    !             printf("#define directive found: %s %s\n", text, nextText);
-    !             i++; // Skip the 'define' token
-    !         }
-    !         clang_disposeString(nextSpelling);
-    !     }
-    !     clang_disposeString(spelling);
-    ! }
-
-    ! clang_disposeTokens(unit, tokens, numTokens);
+! "/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/php/ext/sqlite3/libsqlite/sqlite3.h" parse-include
\ No newline at end of file