示例#1
0
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.IsInSystemHeader())
            {
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            var cursorKind = cursor.kind;
            if (cursorKind == CXCursorKind.CXCursor_MacroDefinition)
            {
                var macroName = clang.getCursorSpelling(cursor).ToString();

                if (m_visitedMacros.Contains(macroName))
                {
                    return CXChildVisitResult.CXChildVisit_Continue;
                }

                m_visitedMacros.Add(macroName);

                var expression = getMacroExpression(cursor);
                if (string.IsNullOrWhiteSpace(expression))
                {
                    return CXChildVisitResult.CXChildVisit_Continue;
                }

                m_tw.WriteLine(@"public const int {0} = {1};", macroName, expression);
            }

            return CXChildVisitResult.CXChildVisit_Continue;
        }
        public ParameterSyntax PortParameter(CXCursor paramDecl)
        {
            string paramName = paramDecl.ToString ();
            CXType type = clang.getCursorType (paramDecl);

            // We can't pass void as function parameter
            if (type.kind == CXTypeKind.CXType_Void)
                throw new ArgumentException ();

            var paramId = SF.Identifier (paramName);
            ParameterSyntax paramSyntax = SF.Parameter (paramId);

            if (type.kind == CXTypeKind.CXType_ObjCObjectPointer) {
                type = type.GetPointee ();
                TypeSyntax typeSyntax = PortType (type);
                return paramSyntax.WithType (typeSyntax);
            }

            if (type.kind == CXTypeKind.CXType_Pointer) {
                type = type.GetPointee ();

                if(type.kind == CXTypeKind.CXType_Void) // original type was void*
                    return paramSyntax.WithType(CommonTypes.IntPtrTypeSyntax);

                TypeSyntax typeSyntax = PortType (type);
                return paramSyntax.WithType (typeSyntax).WithModifiers (SF.TokenList (SF.Token (SyntaxKind.OutKeyword)));
            }

            TypeSyntax ts = PortType (type);
            return paramSyntax.WithType (ts);
        }
        public void WriteFunction(CXCursor cursor, string prefixStrip)
        {
            var functionType = clang.getCursorType(cursor);
            var functionName = clang.getCursorSpelling(cursor).ToString();
            var resultType = clang.getCursorResultType(cursor);

            m_tw.WriteLine(@"[DllImport({0}, EntryPoint = ""{1}"", CallingConvention = {2})]",
                m_libraryVarName,
                functionName,
                functionType.CallingConventionSpelling());
            m_tw.Write(@"public static extern ");

            FunctionHelper.WriteReturnType(resultType, m_tw);

            if (functionName.StartsWith(prefixStrip))
            {
                functionName = functionName.Substring(prefixStrip.Length);
            }

            m_tw.Write(@" " + functionName + @"(");

            var numArgTypes = clang.getNumArgTypes(functionType);

            for (uint i = 0; i < numArgTypes; ++i)
            {
                FunctionHelper.WriteArgument(functionType, clang.Cursor_getArgument(cursor, i), m_tw, i);
            }

            m_tw.WriteLine(@");");
            m_tw.WriteLine();
        }
示例#4
0
        public static string GetTextFromCompoundStmt(CXCursor cursor)
        {
            var stmts = cursor.GetChildren ();

            CXSourceLocation start = clang.getCursorLocation (stmts.First ());
            CXSourceLocation end = clang.getRangeEnd (clang.getCursorExtent (stmts.Last ()));

            CXFile file;
            uint line1, line2;
            uint column1, column2;
            uint offset1, offset2;

            clang.getFileLocation (start, out file, out line1, out column1, out offset1);
            clang.getFileLocation (end, out file, out line2, out column2, out offset2);

            string filePath = clang.getFileName (file).ToString ();

            // We have to read bytes first and then convert them to utf8 string
            // because .net string is utf16 char array, but clang handles utf8 src text only.
            // clang's offset means byte offset (not utf16 char offset)

            uint count = offset2 - offset1 + 1;
            byte[] text = new byte[count];
            using(FileStream fs = File.OpenRead(filePath)) {
                fs.Seek (offset1, SeekOrigin.Begin);
                fs.Read (text, 0, (int)count);
            }

            return Encoding.UTF8.GetString (text);
        }
        public TranslationUnitPorter(CXCursor translationUnit, string ns, IBindingLocator bindingLocator)
        {
            if (translationUnit.kind != CXCursorKind.CXCursor_TranslationUnit)
                throw new ArgumentException ();

            if (string.IsNullOrWhiteSpace (ns))
                throw new ArgumentException ();

            if (bindingLocator == null)
                throw new ArgumentNullException ();

            this.translationUnit = translationUnit;
            this.bindingLocator = bindingLocator;

            cu = SyntaxFactory.CompilationUnit ();

            IEnumerable<UsingDirectiveSyntax> usings = CreateUsings ();
            foreach (var u in usings)
                cu = cu.AddUsings (u);

            var nsDecl = CreateNamespace (ns);
            foreach (var c in PortClasses ())
                nsDecl = nsDecl.AddMembers (c);

            cu = cu.AddMembers (nsDecl);
        }
        private void WriteFunction(CXCursor cursor, string prefixStrip)
        {
            var functionType = clang.getCursorType(cursor);
            var functionName = clang.getCursorSpelling(cursor).ToString();
            var resultType = clang.getCursorResultType(cursor);

            _tw.WriteLine($@"[DllImport({_libraryVarName}, " +
                          $@"EntryPoint = ""{functionName}"", " +
                          $@"CallingConvention = {functionType.CallingConventionSpelling()}, CharSet = CharSet.Ansi)]");

            if (resultType.IsPtrToConstChar())
                _tw.WriteLine(@"[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ConstCharPtrMarshaler))]");

            _tw.Write(@"public static extern ");

            FunctionHelper.WriteReturnType(resultType, _tw);

            if (functionName.StartsWith(prefixStrip))
                functionName = functionName.Substring(prefixStrip.Length);

            _tw.Write(@" " + functionName + @"(");

            var numArgTypes = clang.getNumArgTypes(functionType);

            for (uint i = 0; i < numArgTypes; ++i)
                FunctionHelper.WriteArgument(functionType, clang.Cursor_getArgument(cursor, i), _tw, i);

            _tw.WriteLine(@");");
            _tw.WriteLine();
        }
示例#7
0
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.IsInSystemHeader())
            {
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            CXCursorKind curKind = clang.getCursorKind(cursor);

            // look only at function decls
            if (curKind == CXCursorKind.CXCursor_FunctionDecl)
            {
                var functionName = clang.getCursorSpelling(cursor).ToString();

                if (this.visitedFunctions.Contains(functionName))
                {
                    return CXChildVisitResult.CXChildVisit_Continue;
                }

                this.visitedFunctions.Add(functionName);

                Extensions.WriteFunctionInfoHelper(cursor, this.tw, this.prefixStrip);

                return CXChildVisitResult.CXChildVisit_Continue;
            }

            return CXChildVisitResult.CXChildVisit_Recurse;
        }
示例#8
0
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.IsInSystemHeader())
            {
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            CXCursorKind curKind = clang.getCursorKind(cursor);
            if (curKind == CXCursorKind.CXCursor_StructDecl)
            {
                this.fieldPosition = 0;
                var structName = clang.getCursorSpelling(cursor).ToString();

                // struct names can be empty, and so we visit its sibling to find the name
                if (string.IsNullOrEmpty(structName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor);
                    clang.visitChildren(clang.getCursorSemanticParent(cursor), forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero));
                    structName = clang.getCursorSpelling(forwardDeclaringVisitor.ForwardDeclarationCursor).ToString();

                    if (string.IsNullOrEmpty(structName))
                    {
                        structName = "_";
                    }
                }

                if (!this.visitedStructs.Contains(structName))
                {
                    this.IndentedWriteLine("public partial struct " + structName);
                    this.IndentedWriteLine("{");

                    this.indentLevel++;
                    clang.visitChildren(cursor, this.Visit, new CXClientData(IntPtr.Zero));
                    this.indentLevel--;

                    this.IndentedWriteLine("}");
                    this.tw.WriteLine();

                    this.visitedStructs.Add(structName);
                }

                return CXChildVisitResult.CXChildVisit_Continue;
            }

            if (curKind == CXCursorKind.CXCursor_FieldDecl)
            {
                var fieldName = clang.getCursorSpelling(cursor).ToString();
                if (string.IsNullOrEmpty(fieldName))
                {
                    fieldName = "field" + this.fieldPosition; // what if they have fields called field*? :)
                }

                this.fieldPosition++;
                this.IndentedWriteLine(cursor.ToMarshalString(fieldName));
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            return CXChildVisitResult.CXChildVisit_Recurse;
        }
        public ObjCCategoryImplDeclContext(CXCursor categoryImplDecl)
        {
            if (categoryImplDecl.kind != CXCursorKind.CXCursor_ObjCCategoryImplDecl)
                throw new ArgumentException ();

            ObjCCategoryImpl = categoryImplDecl;
            ExtendedClassRef = GetExtendedClass (ObjCCategoryImpl);
        }
        public ObjCImplementationDeclContext(CXCursor cursor)
        {
            if (cursor.kind != CXCursorKind.CXCursor_ObjCImplementationDecl)
                throw new ArgumentException ();

            ImplCursor = cursor;
            DeclCursor = clang.getCanonicalCursor (cursor);
            SuperClassRef = GetSuperClass (DeclCursor);
        }
示例#11
0
        public static void ArgumentHelper(CXType functionType, CXCursor paramCursor, TextWriter tw, uint index)
        {
            var numArgTypes = clang.getNumArgTypes(functionType);
            var type = clang.getArgType(functionType, index);
            var cursorType = clang.getCursorType(paramCursor);

            var spelling = clang.getCursorSpelling(paramCursor).ToString();
            if (string.IsNullOrEmpty(spelling))
            {
                spelling = "param" + index;
            }

            switch (type.kind)
            {
                case CXTypeKind.CXType_Pointer:
                    var pointee = clang.getPointeeType(type);
                    switch (pointee.kind)
                    {
                        case CXTypeKind.CXType_Pointer:
                            tw.Write(pointee.IsPtrToConstChar() && clang.isConstQualifiedType(pointee) != 0 ? "string[]" : "out IntPtr");
                            break;
                        case CXTypeKind.CXType_FunctionProto:
                            tw.Write(clang.getTypeSpelling(cursorType).ToString());
                            break;
                        case CXTypeKind.CXType_Void:
                            tw.Write("IntPtr");
                            break;
                        case CXTypeKind.CXType_Char_S:
                            tw.Write(type.IsPtrToConstChar() ? "[MarshalAs(UnmanagedType.LPStr)] string" : "IntPtr"); // if it's not a const, it's best to go with IntPtr
                            break;
                        case CXTypeKind.CXType_WChar:
                            tw.Write(type.IsPtrToConstChar() ? "[MarshalAs(UnmanagedType.LPWStr)] string" : "IntPtr");
                            break;
                        default:
                            CommonTypeHandling(pointee, tw, "out ");
                            break;
                    }
                    break;
                default:
                    CommonTypeHandling(type, tw);
                    break;
            }

            tw.Write(" @");
            tw.Write(spelling);

            if (index != numArgTypes - 1)
            {
                tw.Write(", ");
            }
        }
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.IsInSystemHeader())
            {
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            var cursorKind = clang.getCursorKind(cursor);

            // look only at function decls
            if (cursorKind == CXCursorKind.CXCursor_FunctionDecl)
            {
                var functionName = clang.getCursorSpelling(cursor).ToString();
                
                if (m_visitedFunctions.Contains(functionName))
                {
                    return CXChildVisitResult.CXChildVisit_Continue;
                }

                string libraryName;
                if (m_exportMap.TryGetValue(functionName, out libraryName))
                {
                    if (!string.Equals(libraryName, m_libraryName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        string message = string.Format(@"Warning: Function {0} belongs to {1}. Skipping generation for {2}.",
                            functionName, libraryName, m_libraryName);
                        Console.WriteLine(message);
                        return CXChildVisitResult.CXChildVisit_Continue;
                    }
                }
                else
                {
                    m_visitedFunctions.Add(functionName);

                    string message = string.Format(@"Info: Unknow function export {0}. Skipping generation for {1}.",
                           functionName, m_libraryName);
                    Console.WriteLine(message);
                    return CXChildVisitResult.CXChildVisit_Continue;
                }

                m_visitedFunctions.Add(functionName);

                WriteFunction(cursor, m_prefixStrip);

                return CXChildVisitResult.CXChildVisit_Continue;
            }

            return CXChildVisitResult.CXChildVisit_Recurse;
        }
        static void Dump(CXCursor cursor, StringBuilder sb, int level, int mask)
        {
            for (int i = 1; i <= level; i++) {
                if (IsSet (mask, level - i)) {
                    if (i == level)
                        sb.Append ("|-");
                    else
                        sb.Append ("| ");
                } else {
                    if (i == level)
                        sb.Append ("`-");
                    else
                        sb.Append ("  ");
                }
            }
            sb.AppendFormat ("{0} {1}\n", cursor.kind, cursor.ToString ());

            CXCursor[] children = cursor.GetChildren ().ToArray();
            for (int i = 0; i < children.Length; i++)
                Dump (children[i], sb, level + 1, (mask << 1) | (i == children.Length - 1 ? 0 : 1));
        }
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.IsInSystemHeader())
            {
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            if (clang.equalCursors(cursor, m_beginningCursor) != 0)
            {
                m_beginningCursorReached = true;
                return CXChildVisitResult.CXChildVisit_Continue;
            }

            if (m_beginningCursorReached)
            {
                ForwardDeclarationCursor = cursor;
                return CXChildVisitResult.CXChildVisit_Break;
            }

            return CXChildVisitResult.CXChildVisit_Recurse;
        }
示例#15
0
        private string getMacroExpression(CXCursor cursor)
        {
            var tu = clang.Cursor_getTranslationUnit(cursor);
            var range = clang.getCursorExtent(cursor);

            IntPtr pTokens;
            uint numTokens;
            clang.tokenize(tu, range, out pTokens, out numTokens);

            var sbValue = new StringBuilder();
            for (var n = 1; n < numTokens; n++)
            {
                var offset = n*Marshal.SizeOf<CXToken>();
                var token = Marshal.PtrToStructure<CXToken>(pTokens + offset);

                var tokenKind = clang.getTokenKind(token);
                if (tokenKind != CXTokenKind.CXToken_Comment)
                {
                    var spelling = clang.getTokenSpelling(tu, token).ToString();
                    if (spelling == @"#" ||
                        spelling == @"}" ||
                        spelling == @"L" ||
                        spelling == @"enum" ||
                        spelling == @"int" ||
                        spelling == @"int8_t" ||
                        spelling == @"int64_t" ||
                        spelling == @"struct" ||
                        spelling == @"typedef")
                    {
                        continue;
                    }
                    sbValue.Append(spelling);
                }
            }

            disposeTokens(tu, pTokens, numTokens);

            return sbValue.ToString();
        }
示例#16
0
 public static extern void getDefinitionSpellingAndExtent(CXCursor @param0, out IntPtr @startBuf, out IntPtr @endBuf, out uint @startLine, out uint @startColumn, out uint @endLine, out uint @endColumn);
示例#17
0
 public static extern CXResult findReferencesInFile(CXCursor @cursor, CXFile @file, CXCursorAndRangeVisitor @visitor);
 internal CXXDestructorDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_Destructor, CX_DeclKind.CX_DeclKind_CXXDestructor)
 {
     _operatorDelete        = new Lazy <FunctionDecl>(() => TranslationUnit.GetOrCreate <FunctionDecl>(Handle.GetSubDecl(0)));
     _operatorDeleteThisArg = new Lazy <Expr>(() => TranslationUnit.GetOrCreate <Expr>(Handle.GetExpr(0)));
 }
示例#19
0
 public static extern CXSourceRange getCursorReferenceNameRange(CXCursor @C, uint @NameFlags, uint @PieceIndex);
示例#20
0
 internal GenericSelectionExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_GenericSelectionExpr, CX_StmtClass.CX_StmtClass_GenericSelectionExpr)
 {
 }
示例#21
0
 internal CXXThisExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_CXXThisExpr, CX_StmtClass.CX_StmtClass_CXXThisExpr)
 {
 }
示例#22
0
 internal AccessSpecDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_CXXAccessSpecifier, CX_DeclKind.CX_DeclKind_AccessSpec)
 {
 }
示例#23
0
 internal VarDecl(CXCursor handle) : this(handle, CXCursorKind.CXCursor_VarDecl, CX_DeclKind.CX_DeclKind_Var)
 {
 }
示例#24
0
文件: DoStmt.cs 项目: xen2/ClangSharp
 internal DoStmt(CXCursor handle) : base(handle, CXCursorKind.CXCursor_DoStmt)
 {
 }
 internal UnresolvedUsingValueDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UsingDeclaration, CX_DeclKind.CX_DeclKind_UnresolvedUsingValue)
 {
 }
 internal EnumConstantDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_EnumConstantDecl, CX_DeclKind.CX_DeclKind_EnumConstant)
 {
     _initExpr = new Lazy <Expr>(() => TranslationUnit.GetOrCreate <Expr>(Handle.InitExpr));
 }
示例#27
0
 internal FieldDecl(CXCursor handle) : this(handle, CXCursorKind.CXCursor_FieldDecl, CX_DeclKind.CX_DeclKind_Field)
 {
 }
示例#28
0
 internal FloatingLiteral(CXCursor handle) : base(handle, CXCursorKind.CXCursor_FloatingLiteral)
 {
 }
示例#29
0
 internal OMPParallelSectionsDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPParallelSectionsDirective)
 {
 }
示例#30
0
 internal DecompositionDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UnexposedDecl, CX_DeclKind.CX_DeclKind_Decomposition)
 {
 }
示例#31
0
 internal OMPBarrierDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPBarrierDirective)
 {
 }
示例#32
0
 internal NonTypeTemplateParmDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_NonTypeTemplateParameter)
 {
 }
示例#33
0
 internal CXXStdInitializerListExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UnexposedExpr, CX_StmtClass.CX_StmtClass_CXXStdInitializerListExpr)
 {
     Debug.Assert(NumChildren is 1);
 }
 internal OMPDistributeDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPDistributeDirective, CX_StmtClass.CX_StmtClass_OMPDistributeDirective)
 {
 }
示例#35
0
 internal ImportDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_ModuleImportDecl, CX_DeclKind.CX_DeclKind_Import)
 {
 }
 internal OMPTaskgroupDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPTaskgroupDirective, CX_StmtClass.CX_StmtClass_OMPTaskgroupDirective)
 {
 }
示例#37
0
 public static extern CXString Cursor_getBriefCommentText(CXCursor @C);
示例#38
0
 internal ObjCStringLiteral(CXCursor handle) : base(handle, CXCursorKind.CXCursor_ObjCStringLiteral, CX_StmtClass.CX_StmtClass_ObjCStringLiteral)
 {
 }
示例#39
0
 public static extern CXModule Cursor_getModule(CXCursor @C);
示例#40
0
 public static extern uint CXXMethod_isVirtual(CXCursor @C);
示例#41
0
 internal MacroDefinitionRecord(CXCursor handle) : base(handle, CXCursorKind.CXCursor_MacroDefinition)
 {
 }
示例#42
0
 public static extern CXCursorKind getTemplateCursorKind(CXCursor @C);
示例#43
0
 internal BinaryConditionalOperator(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UnexposedExpr, CX_StmtClass.CX_StmtClass_BinaryConditionalOperator)
 {
 }
示例#44
0
 internal CXXConstCastExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_CXXConstCastExpr)
 {
 }
示例#45
0
 public static extern CXSourceRange Cursor_getCommentRange(CXCursor @C);
示例#46
0
 internal ObjCAtThrowStmt(CXCursor handle) : base(handle, CXCursorKind.CXCursor_ObjCAtThrowStmt, CX_StmtClass.CX_StmtClass_ObjCAtThrowStmt)
 {
 }
示例#47
0
 public static extern CXString Cursor_getMangling(CXCursor @param0);
示例#48
0
 internal CXXAddrspaceCastExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_CXXAddrspaceCastExpr, CX_StmtClass.CX_StmtClass_CXXAddrspaceCastExpr)
 {
 }
示例#49
0
 public static extern uint CXXMethod_isStatic(CXCursor @C);
 internal OMPGenericLoopDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPGenericLoopDirective, CX_StmtClass.CX_StmtClass_OMPGenericLoopDirective)
 {
 }
示例#51
0
 public static extern uint CXXMethod_isConst(CXCursor @C);
示例#52
0
 internal MemberExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_MemberRefExpr, CX_StmtClass.CX_StmtClass_MemberExpr)
 {
 }
示例#53
0
 public static extern CXCursor getSpecializedCursorTemplate(CXCursor @C);
示例#54
0
 internal CXXNoexceptExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UnaryExpr, CX_StmtClass.CX_StmtClass_CXXNoexceptExpr)
 {
     Debug.Assert(NumChildren is 1);
 }
示例#55
0
 public static extern void annotateTokens(CXTranslationUnit @TU, out CXToken @Tokens, uint @NumTokens, out CXCursor @Cursors);
 internal NamespaceAliasDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_NamespaceAlias, CX_DeclKind.CX_DeclKind_NamespaceAlias)
 {
     _aliasedNamespace = new Lazy <NamedDecl>(() => TranslationUnit.GetOrCreate <NamedDecl>(Handle.GetSubDecl(0)));
     _namespace        = new Lazy <NamespaceDecl>(() => TranslationUnit.GetOrCreate <NamespaceDecl>(Handle.GetSubDecl(1)));
 }
示例#57
0
 public static extern CXCompletionString getCursorCompletionString(CXCursor @cursor);
示例#58
0
 internal CXXThisExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_CXXThisExpr)
 {
 }
示例#59
0
 public static extern CXComment Cursor_getParsedComment(CXCursor @C);
示例#60
0
 internal OMPTaskDirective(CXCursor handle) : base(handle, CXCursorKind.CXCursor_OMPTaskDirective)
 {
 }