Ejemplo n.º 1
0
        private protected ObjCObjectType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
        {
            _baseType  = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.ObjCObjectBaseType));
            _interface = new Lazy <ObjCInterfaceDecl>(() => TranslationUnit.GetOrCreate <ObjCInterfaceDecl>(Handle.Declaration));

            _protocols = new Lazy <IReadOnlyList <ObjCProtocolDecl> >(() => {
                var numProtocols = unchecked ((int)Handle.NumObjCProtocolRefs);
                var protocols    = new List <ObjCProtocolDecl>(numProtocols);

                for (var i = 0; i < numProtocols; i++)
                {
                    var protocol = TranslationUnit.GetOrCreate <ObjCProtocolDecl>(Handle.GetObjCProtocolDecl(unchecked ((uint)i)));
                    protocols.Add(protocol);
                }

                return(protocols);
            });

            _superClassType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.UnderlyingType));
            _typeArgs       = new Lazy <IReadOnlyList <Type> >(() => {
                var numTypeArgs = unchecked ((int)Handle.NumObjCTypeArgs);
                var typeArgs    = new List <Type>(numTypeArgs);

                for (var i = 0; i < numTypeArgs; i++)
                {
                    var typeArg = TranslationUnit.GetOrCreate <Type>(Handle.GetObjCTypeArg(unchecked ((uint)i)));
                    typeArgs.Add(typeArg);
                }

                return(typeArgs);
            });
        }
Ejemplo n.º 2
0
        public static string GetPrimitiveCsTypeOrNull(this CXTypeKind kind)
        {
            switch (kind)
            {
            case CXTypeKind.CXType_Bool:
                return("bool");

            case CXTypeKind.CXType_Char16:
            case CXTypeKind.CXType_WChar:
                return("char");

            case CXTypeKind.CXType_UChar:
            case CXTypeKind.CXType_Char_U:
                return("byte");

            case CXTypeKind.CXType_SChar:
            case CXTypeKind.CXType_Char_S:
                return("sbyte");

            case CXTypeKind.CXType_UShort:
                return("ushort");

            case CXTypeKind.CXType_Short:
                return("short");

            case CXTypeKind.CXType_Float:
                return("float");

            case CXTypeKind.CXType_Double:
                return("double");

            case CXTypeKind.CXType_Int:
                return("int");

            case CXTypeKind.CXType_UInt:
                return("uint");

            case CXTypeKind.CXType_Pointer:
            case CXTypeKind.CXType_NullPtr:     // ugh, what else can I do?
                return("IntPtr");

            case CXTypeKind.CXType_Long:
                return("int");

            case CXTypeKind.CXType_ULong:
                return("uint");

            case CXTypeKind.CXType_LongLong:
                return("long");

            case CXTypeKind.CXType_ULongLong:
                return("ulong");

            case CXTypeKind.CXType_Void:
                return("void");

            default:
                return(null);
            }
        }
Ejemplo n.º 3
0
        protected Type(CXType handle, CXTypeKind expectedKind)
        {
            if (handle.kind != expectedKind)
            {
                throw new ArgumentException(nameof(handle));
            }
            Handle = handle;

            _canonicalType   = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.CanonicalType));
            _translationUnit = new Lazy <TranslationUnit>(() => TranslationUnit.GetOrCreate((CXTranslationUnit)Handle.data[1]));
        }
Ejemplo n.º 4
0
        protected Type(CXType handle, CXTypeKind expectedKind, CX_TypeClass expectedTypeClass)
        {
            if (handle.kind != expectedKind)
            {
                throw new ArgumentOutOfRangeException(nameof(handle));
            }

            if ((handle.TypeClass == CX_TypeClass.CX_TypeClass_Invalid) || (handle.TypeClass != expectedTypeClass))
            {
                throw new ArgumentOutOfRangeException(nameof(handle));
            }
            Handle = handle;

            _canonicalType   = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.CanonicalType));
            _desugar         = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.Desugar));
            _pointeeType     = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.PointeeType));
            _translationUnit = new Lazy <TranslationUnit>(() => TranslationUnit.GetOrCreate((CXTranslationUnit)Handle.data[1]));
        }
Ejemplo n.º 5
0
        public static bool IsPrimitiveNumericType(this CXTypeKind kind)
        {
            switch (kind)
            {
            case CXTypeKind.CXType_Bool:
            case CXTypeKind.CXType_UChar:
            case CXTypeKind.CXType_Char_U:
            case CXTypeKind.CXType_SChar:
            case CXTypeKind.CXType_Char_S:
            case CXTypeKind.CXType_UShort:
            case CXTypeKind.CXType_Short:
            case CXTypeKind.CXType_Float:
            case CXTypeKind.CXType_Double:
            case CXTypeKind.CXType_Int:
            case CXTypeKind.CXType_UInt:
            case CXTypeKind.CXType_Long:
            case CXTypeKind.CXType_ULong:
            case CXTypeKind.CXType_LongLong:
            case CXTypeKind.CXType_ULongLong:
                return(true);
            }

            return(false);
        }
Ejemplo n.º 6
0
 internal BuiltinType(CXType handle, CXTypeKind expectedKind) : base(handle, expectedKind)
 {
 }
Ejemplo n.º 7
0
 private protected ReferenceType(CXType handle, CXTypeKind expectedKind) : base(handle, expectedKind)
 {
     _pointeeType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.PointeeType));
 }
Ejemplo n.º 8
0
 private protected FunctionType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _returnType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.ResultType));
 }
 public static extern CXString clang_getTypeKindSpelling(
     CXTypeKind K);
Ejemplo n.º 10
0
 private protected VectorType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _desugaredType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.Desugar()));
     _elementType   = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.ElementType));
 }
Ejemplo n.º 11
0
 private protected ObjCObjectType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
 }
Ejemplo n.º 12
0
 private protected ArrayType(CXType handle, CXTypeKind expectedKind) : base(handle, expectedKind)
 {
     _elementType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.ArrayElementType));
 }
Ejemplo n.º 13
0
 private protected DeducedType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _deducedType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.DeducedType));
 }
Ejemplo n.º 14
0
 private protected UnaryTransformType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
 }
Ejemplo n.º 15
0
 /// <summary>
 /// The GetTypeKindSpelling
 /// </summary>
 /// <param name="typeKind">The typeKind<see cref="CXTypeKind"/></param>
 /// <returns>The <see cref="string"/></returns>
 public static string GetTypeKindSpelling(CXTypeKind typeKind)
 {
     return(clang.clang_getTypeKindSpelling(typeKind).ToStringAndDispose());
 }
Ejemplo n.º 16
0
 private protected TypeWithKeyword(CXType handle, CXTypeKind expectedKind) : base(handle, expectedKind)
 {
 }
Ejemplo n.º 17
0
 private protected TypeWithKeyword(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
 }
Ejemplo n.º 18
0
 private protected MatrixType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _elementType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(handle.ElementType));
 }
Ejemplo n.º 19
0
 public static bool IsPointer(this CXTypeKind kind)
 {
     return(kind == CXTypeKind.CXType_Pointer || kind == CXTypeKind.CXType_ConstantArray);
 }
Ejemplo n.º 20
0
 private protected UnaryTransformType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _baseType       = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.BaseType));
     _underlyingType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.UnderlyingType));
 }
Ejemplo n.º 21
0
 private protected TagType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _decl = new Lazy <TagDecl>(() => TranslationUnit.GetOrCreate <TagDecl>(Handle.Declaration));
 }
Ejemplo n.º 22
0
 public static string ToPrimitiveCsType(this CXTypeKind kind)
 {
     return(GetPrimitiveCsTypeOrNull(kind) ?? "__Invalid__");
 }
Ejemplo n.º 23
0
 private protected AdjustedType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
     _adjustedType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.AdjustedType));
     _originalType = new Lazy <Type>(() => TranslationUnit.GetOrCreate <Type>(Handle.OriginalType));
 }
Ejemplo n.º 24
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_EnumDecl)
            {
                string     inheritedEnumType;
                CXTypeKind kind = clang.getEnumDeclIntegerType(cursor).kind;

                switch (kind)
                {
                case CXTypeKind.CXType_Int:
                    inheritedEnumType = "int";
                    break;

                case CXTypeKind.CXType_UInt:
                    inheritedEnumType = "uint";
                    break;

                case CXTypeKind.CXType_Short:
                    inheritedEnumType = "short";
                    break;

                case CXTypeKind.CXType_UShort:
                    inheritedEnumType = "ushort";
                    break;

                case CXTypeKind.CXType_LongLong:
                    inheritedEnumType = "long";
                    break;

                case CXTypeKind.CXType_ULongLong:
                    inheritedEnumType = "ulong";
                    break;

                default:
                    inheritedEnumType = "int";
                    break;
                }

                // Cross-plat hack:
                // For whatever reason, libclang detects untyped enums (i.e. your average 'enum X { A, B }')
                // as uints on Linux and ints on Windows.
                // Since we want to have the same generated code everywhere, we try to force 'int'
                // if it doesn't change semantics, i.e. if all enum values are in the right range.
                // Remember that 2's complement ints use the same binary representation as uints for positive numbers.
                if (inheritedEnumType == "uint")
                {
                    bool hasOneValue = false;
                    long minValue    = long.MaxValue;
                    long maxValue    = long.MinValue;
                    clang.visitChildren(cursor, (cxCursor, _, __) =>
                    {
                        hasOneValue = true;

                        long value = clang.getEnumConstantDeclValue(cxCursor);
                        minValue   = Math.Min(minValue, value);
                        maxValue   = Math.Max(maxValue, value);

                        return(CXChildVisitResult.CXChildVisit_Continue);
                    }, new CXClientData(IntPtr.Zero));

                    if (hasOneValue && minValue >= 0 && maxValue <= int.MaxValue)
                    {
                        inheritedEnumType = "int";
                    }
                }

                var enumName = clang.getCursorSpelling(cursor).ToString();

                // 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(enumName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor);
                    clang.visitChildren(clang.getCursorLexicalParent(cursor), forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero));
                    enumName = clang.getCursorSpelling(forwardDeclaringVisitor.ForwardDeclarationCursor).ToString();

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

                // if we've printed these previously, skip them
                if (this.printedEnums.Contains(enumName))
                {
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }

                this.printedEnums.Add(enumName);

                this.tw.WriteLine("    public enum " + enumName + " : " + inheritedEnumType);
                this.tw.WriteLine("    {");

                // visit all the enum values
                clang.visitChildren(cursor, (cxCursor, _, __) =>
                {
                    this.tw.WriteLine("        @" + clang.getCursorSpelling(cxCursor).ToString() + " = " + clang.getEnumConstantDeclValue(cxCursor) + ",");
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }, new CXClientData(IntPtr.Zero));

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

            return(CXChildVisitResult.CXChildVisit_Recurse);
        }
Ejemplo n.º 25
0
 public static bool IsChar(this CXTypeKind kind)
 {
     return(kind == CXTypeKind.CXType_UChar || kind == CXTypeKind.CXType_Char_U ||
            kind == CXTypeKind.CXType_SChar || kind == CXTypeKind.CXType_Char_S);
 }
Ejemplo n.º 26
0
 private protected DeducedType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
 }
Ejemplo n.º 27
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_EnumDecl)
            {
                string     inheritedEnumType;
                CXTypeKind kind = clang.getEnumDeclIntegerType(cursor).kind;

                switch (kind)
                {
                case CXTypeKind.CXType_Int:
                    inheritedEnumType = "int";
                    break;

                case CXTypeKind.CXType_UInt:
                    inheritedEnumType = "uint";
                    break;

                case CXTypeKind.CXType_Short:
                    inheritedEnumType = "short";
                    break;

                case CXTypeKind.CXType_UShort:
                    inheritedEnumType = "ushort";
                    break;

                case CXTypeKind.CXType_LongLong:
                    inheritedEnumType = "long";
                    break;

                case CXTypeKind.CXType_ULongLong:
                    inheritedEnumType = "ulong";
                    break;

                default:
                    inheritedEnumType = "int";
                    break;
                }

                var enumName = clang.getCursorSpelling(cursor).ToString();

                // 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(enumName))
                {
                    var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor);
                    clang.visitChildren(clang.getCursorLexicalParent(cursor), forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero));
                    enumName = clang.getCursorSpelling(forwardDeclaringVisitor.ForwardDeclarationCursor).ToString();

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

                // if we've printed these previously, skip them
                if (this.printedEnums.Contains(enumName))
                {
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }

                this.printedEnums.Add(enumName);

                this.tw.WriteLine("    public enum " + enumName + " : " + inheritedEnumType);
                this.tw.WriteLine("    {");

                // visit all the enum values
                clang.visitChildren(cursor, (cxCursor, _, __) =>
                {
                    this.tw.WriteLine("        @" + clang.getCursorSpelling(cxCursor).ToString() + " = " + clang.getEnumConstantDeclValue(cxCursor) + ",");
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }, new CXClientData(IntPtr.Zero));

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

            return(CXChildVisitResult.CXChildVisit_Recurse);
        }
Ejemplo n.º 28
0
 private protected ReferenceType(CXType handle, CXTypeKind expectedTypeKind, CX_TypeClass expectedTypeClass) : base(handle, expectedTypeKind, expectedTypeClass)
 {
 }
Ejemplo n.º 29
0
 public static extern CXString getTypeKindSpelling(CXTypeKind @K);
Ejemplo n.º 30
0
            //public static ClangType TypedefDeclUnderlyingType(CXCursor c) {
            //	return new ClangType(clang.getTypedefDeclUnderlyingType(c));
            //}

            public static string KindSpelling(CXTypeKind K)
            {
                return(clang.getTypeKindSpelling(K).ToString());
            }