Exemple #1
0
 static private bool IsIndexCursorKind(CursorKind k)
 {
     return 
         (
             CursorKinds.IsClassStructEtc(k) ||
             k == LibClang.CursorKind.CXXMethod ||
             k == LibClang.CursorKind.Constructor ||
             k == LibClang.CursorKind.Destructor ||
             k == LibClang.CursorKind.FieldDecl ||
             k == LibClang.CursorKind.ClassTemplate ||
             k == LibClang.CursorKind.Namespace ||
             k == LibClang.CursorKind.FunctionDecl ||
             k == LibClang.CursorKind.VarDecl ||
             k == LibClang.CursorKind.EnumDecl ||
             k == LibClang.CursorKind.EnumConstantDecl ||
             k == LibClang.CursorKind.ParamDecl ||
             k == CursorKind.ConversionFunction ||
             k == CursorKind.ParamDecl ||
             k == CursorKind.TemplateTypeParameter ||
             k == CursorKind.CallExpr ||
             k == CursorKind.DeclRefExpr ||
             k == CursorKind.MemberRefExpr ||
             k == CursorKind.InclusionDirective ||
             CursorKinds.IsReference(k) ||
             CursorKinds.IsStatement(k) ||
             CursorKinds.IsExpression(k)
         );
 }
Exemple #2
0
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            // We only care about function declarations in the main file.
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            // We can't P/Invoke inlined functions.
            if (cursor.IsInlinedFunction())
            {
                return(ChildVisitResult.Continue);
            }
            ;

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.UnexposedDecl)
            {
                return(ChildVisitResult.Recurse);
            }

            if (curKind == CursorKind.FunctionDecl)
            {
                var function = this.ExtractFunction(cursor);
                this.generator.AddMethod(cursor.GetDisplayName(), function);
                return(ChildVisitResult.Continue);
            }

            return(ChildVisitResult.Continue);
        }
 public IndexEntry(string spelling, int offset, int endOffset, CursorKind kind)
 {
     Spelling  = spelling;
     Kind      = kind;
     Offset    = offset;
     EndOffset = endOffset;
 }
Exemple #4
0
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.TypedefDecl)
            {
                var nativeName = cursor.GetSpelling();
                var clrName    = NameConversions.ToClrName(nativeName, NameConversion.Type);

                // if we've printed these previously, skip them
                if (this.generator.NameMapping.ContainsKey(nativeName))
                {
                    return(ChildVisitResult.Continue);
                }

                TypeInfo type = cursor.GetTypedefDeclUnderlyingType().GetCanonicalType();

                // we handle enums and records in struct and enum visitors with forward declarations also
                if (type.Kind == TypeKind.Record || type.Kind == TypeKind.Enum)
                {
                    return(ChildVisitResult.Continue);
                }

                if (type.Kind == TypeKind.Pointer)
                {
                    var pointee = type.GetPointeeType();
                    if (pointee.Kind == TypeKind.Record || pointee.Kind == TypeKind.Void)
                    {
                        var types = Handles.CreateSafeHandle(clrName, this.generator).ToArray();
                        this.generator.AddType(nativeName, types[0]);

                        for (int i = 1; i < types.Length; i++)
                        {
                            this.generator.AddType(types[i].Name, types[i]);
                        }

                        return(ChildVisitResult.Continue);
                    }

                    if (pointee.Kind == TypeKind.FunctionProto)
                    {
                        var functionType = cursor.GetTypedefDeclUnderlyingType();
                        var pt           = functionType.GetPointeeType();
                        CodeTypeDelegate delegateType = pt.ToDelegate(nativeName, cursor, this.generator);
                        this.generator.AddType(nativeName, delegateType);

                        return(ChildVisitResult.Continue);
                    }
                }

                return(ChildVisitResult.Continue);
            }

            return(ChildVisitResult.Recurse);
        }
Exemple #5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="kind">Cursor Kind</param>
 /// <param name="xdata">X Data</param>
 /// <param name="data">Data Array of void* [3]</param>
 public CXCursor(CursorKind kind, int xdata, VoidPtr[] data)
 {
     this.Kind  = kind;
     this.XData = xdata;
     this.Data1 = data[0];
     this.Data2 = data[1];
     this.Data3 = data[2];
 }
Exemple #6
0
        static public bool IsClassStructEtc(CursorKind k)
        {
            return k == CursorKind.ClassDecl ||
                    k == CursorKind.StructDecl ||
                    k == CursorKind.ClassTemplate ||
                    k == CursorKind.ClassTemplatePartialSpecialization;

        }
Exemple #7
0
 static public bool IsFunctionEtc(CursorKind k)
 {
     return k == CursorKind.CXXMethod ||
             k == CursorKind.FunctionDecl ||
             k == CursorKind.Constructor ||
             k == CursorKind.Destructor ||
             k == CursorKind.ConversionFunction;
             //conv func?
 }
Exemple #8
0
        public void testGetAST(string code, CursorKind fstChildKind, string fstChildSpelling, Type.Kind fstChildTypeKind)
        {
            Cursor root = getWrapper(code).getRoot();

            Assert.AreEqual(CursorKind.TranslationUnit, root.Kind);
            Assert.AreEqual(fstChildKind, root.Children[0].Kind);
            Assert.AreEqual(fstChildSpelling, root.Children[0].Spelling);
            Assert.AreEqual(fstChildTypeKind, root.Children[0].ResultType.TypeKind);
        }
 public void GetTypedefDeclUnderlyingType(string source, CursorKind cursorKind)
 {
     using (var empty = disposables.WriteToEmpty(source))
     {
         var file    = empty.GetFile(TestFiles.Empty);
         var typedef = empty.GetCursor(file.GetLocation(1, 1));
         Assert.AreEqual(cursorKind, typedef.Kind);
         Assert.AreEqual(TypeKind.Typedef, typedef.GetTypeInfo().Kind);
         Assert.AreEqual(TypeKind.Int, typedef.GetTypedefDeclUnderlyingType().Kind);
     }
 }
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.TypedefDecl)
            {
                var nativeName = cursor.GetSpelling();
                var clrName    = NameConversions.ToClrName(nativeName, NameConversion.Type);

                TypeInfo type = cursor.GetTypedefDeclUnderlyingType().GetCanonicalType();

                // we handle enums and records in struct and enum visitors with forward declarations also
                switch (type.Kind)
                {
                case TypeKind.Record:
                    this.generator.AddType(
                        nativeName,
                        new Primitives.SafeHandle()
                    {
                        NativeName = nativeName,
                        Name       = NameConversions.ToClrName(nativeName, NameConversion.Type)
                    });
                    return(ChildVisitResult.Continue);

                case TypeKind.Pointer:
                    var pointee = type.GetPointeeType();

                    if (pointee.Kind == TypeKind.FunctionProto)
                    {
                        var functionType = cursor.GetTypedefDeclUnderlyingType();
                        var pt           = functionType.GetPointeeType();

                        var delegateType = pt.ToDelegate(nativeName, cursor, this.generator);
                        this.generator.AddType(nativeName, delegateType);

                        return(ChildVisitResult.Continue);
                    }
                    else
                    {
                        return(ChildVisitResult.Continue);
                    }

                default:
                    return(ChildVisitResult.Continue);
                }
            }

            return(ChildVisitResult.Recurse);
        }
Exemple #11
0
        /// <summary>
        /// Return an iterator for accessing the children of this cursor that are
        /// of the specified <paramref name="kind"/>.
        /// </summary>
        /// <returns></returns>
        public ReadOnlyCollection <ClangCursor> GetChildrenOfKind(CursorKind kind)
        {
            List <ClangCursor> children = new List <ClangCursor> ();

            this.VisitChildren((child, parent, data) => {
                if (child.Kind == kind)
                {
                    children.Add(child);
                }
                return(ChildVisitResult.Continue);
            }, IntPtr.Zero);

            return(new ReadOnlyCollection <ClangCursor> (children));
        }
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            if (this.nativeMethods == null)
            {
                var name = this.generator.Name + "NativeMethods";
                this.nativeMethods            = new CodeTypeDeclaration();
                this.nativeMethods.Name       = name;
                this.nativeMethods.Attributes = /*MemberAttributes.Static |*/ MemberAttributes.Public | MemberAttributes.Final;
                this.nativeMethods.IsPartial  = true;
                this.nativeMethods.Members.Add(
                    new CodeMemberField(typeof(string), "libraryName")
                {
                    Attributes     = MemberAttributes.Const,
                    InitExpression = new CodePrimitiveExpression(this.libraryName)
                });

                this.generator.Types.Add(new CodeDomGeneratedType(this.nativeMethods));
            }

            CursorKind curKind = cursor.Kind;

            // look only at function decls

            /*
             * if (curKind == CursorKind.Cursor_FirstDecl)
             * {
             *  return ChildVisitResult.Recurse;
             * }*/

            if (curKind == CursorKind.UnexposedDecl)
            {
                return(ChildVisitResult.Recurse);
            }

            if (curKind == CursorKind.FunctionDecl)
            {
                var function = this.WriteFunctionInfoHelper(cursor);
                this.nativeMethods.Members.Add(function);
                return(ChildVisitResult.Continue);
            }

            return(ChildVisitResult.Continue);
        }
        /*
         *  Load dictionary template file from specified path.
         */
        private void loadDictionary(string pathToTemplate)
        {
            string[] lines = System.IO.File.ReadAllLines(pathToTemplate);

            string[] splitter = new string[] { "::" };
            foreach (string line in lines)
            {
                string[]   parts = line.Split(splitter, StringSplitOptions.None);
                CursorKind kind  = stringToCursorKind(parts[0]);
                if (kind != 0)
                {
                    cursorDictionary.Add(kind, parts[1]);
                }
                else
                {
                    typeDictionary.Add(parts[0], parts[1]);
                }
            }
        }
Exemple #14
0
 static public bool IsDefinition(CursorKind k)
 {
     int kAsInt = (int)k;
     return (kAsInt >= (int)CursorKind.FirstDecl && kAsInt <= (int)CursorKind.LastDecl);
 }
Exemple #15
0
 public static extern uint clang_isPreprocessing(CursorKind ck);
Exemple #16
0
 public static extern ClangString clang_getCursorKindSpelling(CursorKind ck);
Exemple #17
0
 public static extern uint clang_isStatement(CursorKind ck);
Exemple #18
0
 public static extern uint clang_isInvalid(CursorKind ck);
Exemple #19
0
 public static extern ClangString clang_getCursorKindSpelling(CursorKind ck);
Exemple #20
0
 public static extern uint clang_isReference(CursorKind ck);
 internal static extern uint clang_isInvalid(CursorKind _);
Exemple #22
0
 public static extern uint clang_isPreprocessing(CursorKind ck);
Exemple #23
0
 static public bool IsReference(CursorKind k)
 {
     int kAsInt = (int)k;
     return (kAsInt >= (int)CursorKind.FirstRef && kAsInt <= (int)CursorKind.LastRef);
 }
 internal static extern uint clang_isStatement(CursorKind _);
Exemple #25
0
 static public bool IsStatement(CursorKind k)
 {
     int kAsInt = (int)k;
     return (kAsInt >= (int)CursorKind.FirstStmt && kAsInt <= (int)CursorKind.LastStmt);
 }
Exemple #26
0
 static public bool IsExpression(CursorKind k)
 {
     int kAsInt = (int)k;
     return (kAsInt >= (int)CursorKind.FirstExpr && kAsInt <= (int)CursorKind.LastExpr);
 }
 internal static extern uint clang_isUnexposed(CursorKind _);
 internal static extern uint clang_isPreprocessing(CursorKind _);
 internal static extern uint clang_isTranslationUnit(CursorKind _);
Exemple #30
0
 public static extern uint clang_isTranslationUnit(CursorKind ck);
Exemple #31
0
 internal Cursor(Interop.Cursor native) {
     Native = native;
     Kind = Interop.clang_getCursorKind(Native);
 }
Exemple #32
0
 public static extern uint clang_isUnexposed(CursorKind ck);
Exemple #33
0
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.EnumDecl)
            {
                var nativeName  = cursor.GetSpelling();
                var type        = cursor.GetEnumDeclIntegerType().ToClrType();
                var enumComment = this.GetComment(cursor, forType: true);

                // enumName can be empty because of typedef enum { .. } enumName;
                // so we have to find the sibling, and this is the only way I've found
                // to do with libclang, maybe there is a better way?
                if (string.IsNullOrEmpty(nativeName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor, skipSystemHeaderCheck: true);
                    forwardDeclaringVisitor.VisitChildren(cursor.GetLexicalParent());
                    nativeName = forwardDeclaringVisitor.ForwardDeclarationCursor.GetSpelling();

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

                var clrName = NameConversions.ToClrName(nativeName, NameConversion.Type);

                // if we've printed these previously, skip them
                if (this.generator.NameMapping.ContainsKey(nativeName))
                {
                    return(ChildVisitResult.Continue);
                }

                CodeTypeDeclaration enumDeclaration = new CodeTypeDeclaration();
                enumDeclaration.Attributes = MemberAttributes.Public;
                enumDeclaration.IsEnum     = true;
                enumDeclaration.Name       = clrName;
                enumDeclaration.BaseTypes.Add(type);

                if (enumComment != null)
                {
                    enumDeclaration.Comments.Add(enumComment);
                }

                // visit all the enum values
                DelegatingCursorVisitor visitor = new DelegatingCursorVisitor(
                    (c, vistor) =>
                {
                    var field =
                        new CodeMemberField()
                    {
                        Name           = NameConversions.ToClrName(c.GetSpelling(), NameConversion.Field),
                        InitExpression = new CodePrimitiveExpression(c.GetEnumConstantDeclValue())
                    };

                    var fieldComment = this.GetComment(c, forType: true);
                    if (fieldComment != null)
                    {
                        field.Comments.Add(fieldComment);
                    }

                    enumDeclaration.Members.Add(field);
                    return(ChildVisitResult.Continue);
                });
                visitor.VisitChildren(cursor);

                this.generator.AddType(nativeName, new CodeDomGeneratedType(enumDeclaration));
            }

            return(ChildVisitResult.Recurse);
        }
Exemple #34
0
 public static extern uint clang_isDeclaration(CursorKind ck);
Exemple #35
0
 internal unsafe static extern CXString clang_getCompletionParent(CXCompletionString cs, out CursorKind kind);
Exemple #36
0
 public static extern uint clang_isExpression(CursorKind ck);
Exemple #37
0
 public static extern uint clang_isReference(CursorKind ck);
Exemple #38
0
 public static extern uint clang_isAttribute(CursorKind ck);
Exemple #39
0
 public static extern uint clang_isExpression(CursorKind ck);
Exemple #40
0
 public static extern uint clang_isTranslationUnit(CursorKind ck);
Exemple #41
0
 public static extern uint clang_isStatement(CursorKind ck);
Exemple #42
0
 public static extern uint clang_isUnexposed(CursorKind ck);
Exemple #43
0
 public static extern uint clang_isAttribute(CursorKind ck);
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            if (parent.Kind == CursorKind.UnionDecl)
            {
                return(ChildVisitResult.Continue);
            }

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.StructDecl)
            {
                this.fieldPosition = 0;
                var nativeName = cursor.GetSpelling();

                // struct names can be empty, and so we visit its sibling to find the name
                if (string.IsNullOrEmpty(nativeName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor, skipSystemHeaderCheck: true);
                    forwardDeclaringVisitor.VisitChildren(cursor.GetSemanticParent());
                    nativeName = forwardDeclaringVisitor.ForwardDeclarationCursor.GetSpelling();

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

                var clrName = NameConversions.ToClrName(nativeName, NameConversion.Type);

                this.current = new Struct()
                {
                    Name        = clrName,
                    Description = GetComment(cursor)
                };

                // This is a lazy attempt to handle forward declarations. A better way would be as described here:
                // https://joshpeterson.github.io/identifying-a-forward-declaration-with-libclang
                // but libclang doesn't expose clang_getNullCursor (yet)
                current = this.generator.AddType(nativeName, current) as Struct;

                // If the struct is in use as a 'handle', this AddType would have returned an Handle and the 'as Struct'
                // statement would have cast it to null. In that case, we don't create an explicit struct.
                if (current != null)
                {
                    var visitor = new DelegatingCursorVisitor(this.Visit);
                    visitor.VisitChildren(cursor);
                }

                return(ChildVisitResult.Continue);
            }

            if (curKind == CursorKind.FieldDecl)
            {
                var fieldName = cursor.GetSpelling();
                if (string.IsNullOrEmpty(fieldName))
                {
                    fieldName = "field" + this.fieldPosition; // what if they have fields called field*? :)
                }
                else
                {
                    fieldName = NameConversions.ToClrName(fieldName, NameConversion.Field);
                }

                this.fieldPosition++;

                foreach (var member in GetFields(cursor, fieldName, this.generator))
                {
                    this.current.Fields.Add(member);
                }

                return(ChildVisitResult.Continue);
            }

            return(ChildVisitResult.Recurse);
        }
Exemple #45
0
 public static extern uint clang_isInvalid(CursorKind ck);
        public ChildVisitResult Visit(Cursor cursor, Cursor parent)
        {
            if (!cursor.GetLocation().IsFromMainFile())
            {
                return(ChildVisitResult.Continue);
            }

            CursorKind curKind = cursor.Kind;

            if (curKind == CursorKind.EnumDecl)
            {
                var nativeName  = cursor.GetSpelling();
                var enumComment = this.GetComment(cursor, forType: true);

                // enumName can be empty because of typedef enum { .. } enumName;
                // so we have to find the sibling, and this is the only way I've found
                // to do with libclang, maybe there is a better way?
                if (string.IsNullOrEmpty(nativeName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor, skipSystemHeaderCheck: true);
                    forwardDeclaringVisitor.VisitChildren(cursor.GetLexicalParent());
                    nativeName = forwardDeclaringVisitor.ForwardDeclarationCursor.GetSpelling();

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

                var clrName = NameConversions.ToClrName(nativeName, NameConversion.Type);

                Enum enumDeclaration = new Enum()
                {
                    Name        = clrName,
                    Description = enumComment,
                    BaseType    = "byte"
                };

                // Most enums maps to bytes, with these exceptiosn
                if (nativeName == "libusb_error" || nativeName == "libusb_capability")
                {
                    enumDeclaration.BaseType = "int";
                }

                // Normally the enum values are prefixed with the enum name, e.g.
                // enum log_level => LOG_LEVEL_DEBUG = 1
                //
                // However, this is not always consistent, e.g.
                // enum libusb_capability => LIBUSB_CAP_HAS_CAPABILITY = 1
                //
                // Patch the prefix where required.
                var prefix = nativeName;

                var mappings = new Dictionary <string, string>();
                mappings.Add("libusb_capability", "libusb_cap");
                mappings.Add("libusb_class_code", "libusb_class");
                mappings.Add("libusb_descriptor_type", "libusb_dt");
                mappings.Add("libusb_endpoint_direction", "libusb_endpoint");
                mappings.Add("libusb_hotplug_flag", "libusb_hotplug");
                mappings.Add("libusb_request_recipient", "libusb_recipient");
                mappings.Add("libusb_standard_request", "libusb_request");
                mappings.Add("libusb_transfer_flags", "libusb_transfer");
                mappings.Add("libusb_transfer_status", "libusb_transfer");
                mappings.Add("libusb_bos_type", "libusb_bt");

                if (mappings.ContainsKey(prefix))
                {
                    prefix = mappings[prefix];
                }

                // visit all the enum values
                DelegatingCursorVisitor visitor = new DelegatingCursorVisitor(
                    (c, vistor) =>
                {
                    var value = c.GetEnumConstantDeclValue();

                    var field =
                        new EnumValue()
                    {
                        Name  = NameConversions.ToClrName(c.GetSpelling(), prefix, NameConversion.Enum),
                        Value = value > 0 ? $"0x{(int)c.GetEnumConstantDeclValue():X}" : $"{value}"
                    };

                    field.Description = this.GetComment(c, forType: true);

                    enumDeclaration.Values.Add(field);
                    return(ChildVisitResult.Continue);
                });
                visitor.VisitChildren(cursor);

                // Add a missing 'None' value
                if (nativeName == "libusb_transfer_flags")
                {
                    enumDeclaration.Values.Add(new EnumValue()
                    {
                        Name  = "None",
                        Value = "0x0"
                    });
                }

                this.generator.AddType(nativeName, enumDeclaration);
            }

            return(ChildVisitResult.Recurse);
        }
 internal static extern uint clang_isAttribute(CursorKind _);
Exemple #48
0
 public InteractableRegion(CursorKind cursorKind, Util.Rect boundingBox)
 {
     this.cursorKind = cursorKind;
     this.rect       = boundingBox;
     this.isolated   = false;
 }
Exemple #49
0
 public static extern uint clang_isDeclaration(CursorKind ck);