private IClangType ParseStruct(CXCursor cursor)
        {
            IncrementStatistic("structs");
            var name = cursor.ToString();

            if (name == null)
            {
                throw new NotImplementedException("Handling of unnamed structs are not implemented.");
            }


            var type = clang.getCursorType(cursor);

            var fields = new LinkedList <ClangFieldInfo>();

            var alignment = (uint)Math.Max(0, clang.Type_getAlignOf(type));

            var size = (uint)Math.Max(0, clang.Type_getSizeOf(type));

            /*
             * var typeDef = Module.DefineType(name,
             *      TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.SequentialLayout,
             *      null, typeAlign, typeSize);
             */

            clang.Type_visitFields(type, (fieldCursor, p) => {
                var fieldName   = fieldCursor.ToString();
                var fieldType   = clang.getCursorType(fieldCursor);
                var fieldOffset = (uint)clang.Cursor_getOffsetOfField(fieldCursor);
                fields.AddLast(new ClangFieldInfo(fieldType, fieldName, fieldOffset));
                return(CXVisitorResult.CXVisit_Continue);
            }, default(CXClientData));

            return(new ClangStructInfo(name, fields.ToArray(), size, alignment));
        }
        private IClangType ParseDelegate(CXCursor cursor, CXCallingConv callConv)
        {
            IncrementStatistic("delegates");
            var name         = cursor.ToString();
            var pfnType      = clang.getTypedefDeclUnderlyingType(cursor);
            var funcType     = clang.getPointeeType(pfnType);
            var argTypeCount = clang.getNumArgTypes(funcType);
            var retType      = clang.getResultType(funcType);
            //var clrRetType = ResolveParameter(retType);
            var paramInfos = new ClangParameterInfo[argTypeCount];
            var i          = 0u;

            clang.visitChildren(cursor, (paramCursor, parent, p) => {
                if (paramCursor.kind != CXCursorKind.CXCursor_ParmDecl)
                {
                    // return type
                    if (i == 0 && paramCursor.kind == CXCursorKind.CXCursor_TypeRef)
                    {
                        return(CXChildVisitResult.CXChildVisit_Continue);
                    }
                    throw new NotImplementedException();
                }
                var paramType = clang.getCursorType(paramCursor);
                var paramName = paramCursor.ToString();
                if (string.IsNullOrEmpty(paramName))
                {
                    paramName = "_" + i;
                }
                //var clrArgParam = ResolveParameter(argType, paramName);
                if (i >= argTypeCount)
                {
                    throw new NotImplementedException();
                }
                paramInfos[i] = new ClangParameterInfo(paramType, paramName, i);
                ++i;
                return(CXChildVisitResult.CXChildVisit_Continue);
            }, default(CXClientData));

            /*
             *      var funcDef = Module.DefineType(name,
             *              TypeAttributes.Sealed | TypeAttributes.Public,
             *              typeof(MulticastDelegate));
             */
            return(new ClangDelegateInfo(callConv, retType, name, paramInfos));
        }
Beispiel #3
0
        private IClangType ParseFunction(CXCursor cursor)
        {
            IncrementStatistic("functions");
            var name = cursor.ToString();

            if (name == null)
            {
                throw new NotImplementedException("Handling of unnamed functions are not implemented.");
            }

            /*
             * if (TypeRedirects.TryGetValue(name, out var renamed)) {
             *      name = renamed;
             * }
             */

            var funcType = clang.getCursorType(cursor);

            var retType = clang.getCursorResultType(cursor);

            var argTypeCount = clang.getNumArgTypes(funcType);

            var paramInfos = new ClangParameterInfo[argTypeCount];

            for (var i = 0u; i < argTypeCount; ++i)
            {
                var argCursor = clang.Cursor_getArgument(cursor, i);
                var argType   = clang.getArgType(funcType, i);
                var paramName = argCursor.ToString();
                if (string.IsNullOrEmpty(paramName))
                {
                    paramName = "_" + i;
                }
                paramInfos[i] = new ClangParameterInfo(argType, paramName, i);
            }

            var callConv = clang.getFunctionTypeCallingConv(funcType);

            return(new ClangFunctionInfo(callConv, retType, name, paramInfos));
        }
        private IClangType ParseUnion(CXCursor cursor)
        {
            IncrementStatistic("unions");
            var name = cursor.ToString();

            if (name == null)
            {
                throw new NotImplementedException("Handling of unnamed unions are not implemented.");
            }

            var type = clang.getCursorType(cursor);

            var fields = new LinkedList <ClangFieldInfo>();

            var alignment = (uint)clang.Type_getAlignOf(type);

            var size = (uint)clang.Type_getSizeOf(type);

            /*
             * var typeDef = Module.DefineType(name,
             *      TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.ExplicitLayout,
             *      null, alignment, size);
             */
            //var fieldPosition = 0;
            clang.Type_visitFields(type, (fieldCursor, p) => {
                var fieldName = fieldCursor.ToString();
                var fieldType = clang.getCursorType(fieldCursor);
                //var fieldDef = ResolveField(fieldType, fieldName);
                var fieldOffset = (uint)clang.Cursor_getOffsetOfField(fieldCursor);
                //fieldDef.AddCustomAttribute(() => new FieldOffsetAttribute(fieldOffset));
                //fieldDef.Position = fieldPosition;
                fields.AddLast(new ClangFieldInfo(fieldType, fieldName, fieldOffset));
                //++fieldPosition;
                return(CXVisitorResult.CXVisit_Continue);
            }, default(CXClientData));

            return(new ClangUnionInfo(name, fields.ToArray(), size, alignment));
        }
Beispiel #5
0
        private IClangType ParseEnum(CXCursor cursor)
        {
            IncrementStatistic("enums");
            var type     = clang.getEnumDeclIntegerType(cursor);
            var typeKind = type.kind;
            var isSigned = SignedCxTypeKinds.Contains(typeKind);

            var name = cursor.ToString();

            if (name == null)
            {
                throw new NotImplementedException("Handling of unnamed enumerations are not implemented.");
            }

            ICollection <ClangConstantInfo> defs
                = new LinkedList <ClangConstantInfo>();

            clang.visitChildren(cursor,
                                (current, parent, p) => {
                var constName = current.ToString();

                if (string.IsNullOrEmpty(constName))
                {
                    throw new NotImplementedException("Handling of unnamed enumeration literals are not implemented.");
                }

                defs.Add(isSigned
                                                ? (ClangConstantInfo) new ClangConstantInfo <long>(constName,
                                                                                                   clang.getEnumConstantDeclValue(current))
                                                : new ClangConstantInfo <ulong>(constName,
                                                                                clang.getEnumConstantDeclUnsignedValue(current)));
                return(CXChildVisitResult.CXChildVisit_Continue);
            },
                                default(CXClientData));

            return(new ClangEnumInfo(type, name, defs.ToArray()));
        }
        private IClangType ParseTypeDef(CXCursor cursor)
        {
            var originalType   = clang.getCursorType(cursor);
            var canonType      = clang.getCanonicalType(originalType);
            var typeDeclCursor = clang.getTypeDeclaration(canonType);

            if (IsCursorInSystemHeader(typeDeclCursor))
            {
                return(null);
            }

            var name = cursor.ToString();

            if (typeDeclCursor.kind == CXCursorKind.CXCursor_NoDeclFound)
            {
                if (canonType.kind != CXTypeKind.CXType_Pointer)
                {
                    // likely simple type alias
                    if (TypeRedirects.TryGetValue(name, out var renamed))
                    {
                        name = renamed;
                    }
                    if (KnownTypes.TryGetValue(name, out var knownType))
                    {
                        if (PrimitiveTypeMap.TryGetValue(canonType.kind, out var primitiveType))
                        {
                            var existingType = Module.GetType(name);
                            if (existingType == null)
                            {
                                throw new NotImplementedException();
                            }

                            switch (knownType)
                            {
                            case KnownType.Bitmask:
                            case KnownType.Enum: {
                                existingType.ChangeUnderlyingType(primitiveType.Import(Module));
                                break;
                            }

                            default:
                                break;
                            }

                            IncrementStatistic("typedefs");
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }

                    return(null);
                }

                var pointeeType = clang.getPointeeType(canonType);
                var callConv    = clang.getFunctionTypeCallingConv(pointeeType);
                if (callConv == CXCallingConv.CXCallingConv_Invalid)
                {
                    // likely a pointer type alias
                    return(null);
                }

                return(ParseDelegate(cursor, callConv));
            }

            switch (typeDeclCursor.kind)
            {
            case CXCursorKind.CXCursor_UnionDecl:
            case CXCursorKind.CXCursor_StructDecl: {
                var typeName = typeDeclCursor.ToString();
                if (name == typeName)
                {
                    return(null);
                }

                throw new NotImplementedException();
            }

            case CXCursorKind.CXCursor_EnumDecl: {
                if (TypeRedirects.TryGetValue(name, out var renamed))
                {
                    name = renamed;
                }
                if (KnownTypes.TryGetValue(name, out var knownType))
                {
                    var existingType = Module.GetType(name);
                    if (existingType != null)
                    {
                        return(null);
                    }

                    switch (knownType)
                    {
                    case KnownType.Enum: {
                        throw new NotImplementedException();
                    }

                    case KnownType.Bitmask: {
                        throw new NotImplementedException();
                    }

                    default:
                        throw new NotImplementedException();
                    }
                }

                throw new NotImplementedException();
            }
            }

            IncrementStatistic("typedefs");
            throw new NotImplementedException();
        }
Beispiel #7
0
 public override string ToString()
 {
     return(Handle.ToString());
 }
        public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (!File.Equals(TranslationUnitParser.GetFileName(cursor)))
            {
                return(CXChildVisitResult.Continue);
            }

            CXCursor referenced = project.ClangManager.GetCursorReferenced(cursor);
            string   Usr        = project.ClangManager.GetCursorUsrString(referenced);

            if (UsrReferenced.Equals(Usr))
            {
                CXSourceRange range     = clang.Cursor_getSpellingNameRange(cursor, 0, 0);
                var           reference = new Reference(project, cursor, range);

                //FIXME: don't block!
                Document doc = IdeApp.Workbench.OpenDocument(reference.FileName, project, false).Result;
                if (!references.Contains(reference)
                    //this check is needed because explicit namespace qualifiers, eg: "std" from std::toupper
                    //are also found when finding eg:toupper references, but has the same cursorkind as eg:"toupper"
                    && doc.Editor.GetTextAt(reference.Begin.Offset, reference.Length).Equals(referenced.ToString()))
                {
                    references.Add(reference);
                }
            }
            return(CXChildVisitResult.Recurse);
        }