Ejemplo n.º 1
0
        private CXChildVisitResult Visitor(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (CXCursorKind.CXCursor_ParmDecl == cursor.kind)
            {
                // prepare client data
                GCHandle      funcHandle = (GCHandle)data;
                FunctionProto proto      = funcHandle.Target as FunctionProto;

                CXType type = clang.getCursorType(cursor);

                FunctionParameter param = new FunctionParameter
                {
                    Name = clang.getCursorSpelling(cursor).ToString(),
                    Type = TypeVisitorHelper.GetNativeType(AST_, type)
                };

                clang.visitChildren(cursor, (CXCursor c, CXCursor p, IntPtr d) =>
                {
                    if (ClangTraits.IsLiteralCursor(c))
                    {
                        // get liter-string from token
                        List <string> tokens = ASTVisitor.GetCursorTokens(c);

                        // set default literal
                        param.DefaultValue = string.Concat(tokens);
                    }
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }, new CXClientData(IntPtr.Zero));

                proto.AddParameter(param);
            }

            return(CXChildVisitResult.CXChildVisit_Recurse);
        }
Ejemplo n.º 2
0
        private CXChildVisitResult Visitor(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (cursor.kind == CXCursorKind.CXCursor_EnumConstantDecl)
            {
                // prepare enumeration
                // prepare client data
                GCHandle    enumHandle = (GCHandle)data;
                Enumeration thisEnum   = enumHandle.Target as Enumeration;

                // get constant name
                string constantName = clang.getCursorSpelling(cursor).ToString();

                // get enum constant value
                EnumField c = new EnumField {
                    Name = constantName
                };
                CXType     underlyingType       = clang.getEnumDeclIntegerType(parent);
                NativeType underlyingNativeType = TypeVisitorHelper.GetNativeType(AST_, underlyingType);
                if (ASTTraits.IsSigned(underlyingNativeType))
                {
                    c.Constant = clang.getEnumConstantDeclValue(cursor);
                }
                else
                {
                    Debug.Assert(ASTTraits.IsUnSigned(underlyingNativeType));
                    c.Constant = (long)clang.getEnumConstantDeclUnsignedValue(cursor);
                }

                // add it to enumeration
                thisEnum.AddConstant(c);
            }
            return(CXChildVisitResult.CXChildVisit_Continue);
        }
Ejemplo n.º 3
0
        private void ProcessMethod(NativeClass thisClass, CXCursor cursor, CXCursor parent)
        {
            string name       = clang.getCursorSpelling(cursor).ToString();
            bool   isStataic  = clang.CXXMethod_isStatic(cursor) != 0;
            bool   isConst    = clang.CXXMethod_isConst(cursor) != 0;
            bool   isVirtual  = clang.CXXMethod_isVirtual(cursor) != 0;
            bool   isAbastrct = clang.CXXMethod_isPureVirtual(cursor) != 0;

            // create method
            Method memberFunc = new Method(thisClass,
                                           name, isStataic, isConst, isVirtual, isAbastrct);
            OnVisitFunctionParameter func = (FunctionParameter param) =>
            {
                memberFunc.AddParameter(param);
            };

            // proces result type
            CXType resultType = clang.getCursorResultType(cursor);

            memberFunc.ResultType = TypeVisitorHelper.GetNativeType(AST_, resultType);

            // deep visit children
            GCHandle delegateHandler = GCHandle.Alloc(func);

            clang.visitChildren(cursor, ParameterVisitor, new CXClientData((IntPtr)delegateHandler));

            // process access specifier
            memberFunc.Access = ClangTraits.ToAccessSpecifier(clang.getCXXAccessSpecifier(cursor));

            // register method
            thisClass.AddMethod(memberFunc);
        }
Ejemplo n.º 4
0
        public bool DoVisit(CXCursor cursor, CXCursor parent)
        {
            string cursorName       = clang.getCursorSpelling(cursor).ToString();
            string functionName     = cursorName;
            string functionTypeName = clang.getCursorDisplayName(cursor).ToString();

            NativeFunction function = AST_.GetFunction(functionName, functionTypeName);

            if (!function.Parsed)
            {
                function.Parsed = true;

                // spelling as unscoped name
                function.UnscopedName = clang.getCursorSpelling(cursor).ToString();

                // create proto
                FunctionProto proto = new FunctionProto();

                // proces result type
                CXType resultType = clang.getCursorResultType(cursor);
                proto.ResultType = TypeVisitorHelper.GetNativeType(AST_, resultType);

                // create IntPtr for context
                GCHandle protoHandle = GCHandle.Alloc(proto);

                // visit children
                clang.visitChildren(cursor, Visitor, new CXClientData((IntPtr)protoHandle));

                // set proto
                function.Proto = proto;
            }

            return(true);
        }
Ejemplo n.º 5
0
        public bool DoVisit(CXCursor cursor, CXCursor parent)
        {
            CXType           cxType  = clang.getCursorType(cursor);
            TypeVisitContext context = new TypeVisitContext(cursor);
            NativeType       type    = TypeVisitorHelper.GetNativeType(AST_, cxType, context);

            return(true);
        }
Ejemplo n.º 6
0
        private TemplateParameter GetTemplateNonTypeParameter(AST ast, TemplateProto tp, CXCursor cursor)
        {
            string paramName  = clang.getCursorSpelling(cursor).ToString();
            bool   isVariadic = ClangTraits.IsVariadicTemplateParameter(cursor);

            // check if dependent or nontype
            bool   isDependent   = false;
            string dependName    = null;
            string default_value = null;

            clang.visitChildren(cursor, (CXCursor c, CXCursor p, IntPtr data) =>
            {
                if (ClangTraits.IsTypeRef(c))
                {
                    CXType t = clang.getCursorType(c);
                    if (ClangTraits.IsUnexposedType(t))
                    {
                        isDependent = true;
                        dependName  = clang.getCursorSpelling(c).ToString();
                    }
                }
                else if (ClangTraits.IsNonTypeTemplateParamLiteral(c))
                {
                    List <string> tokens = ASTVisitor.GetCursorTokens(c);
                    default_value        = string.Concat(tokens);
                }
                return(CXChildVisitResult.CXChildVisit_Continue);
            }, new CXClientData(IntPtr.Zero));

            TemplateParameter param;

            if (isDependent)
            {
                Debug.Assert(dependName != null);
                param = new TemplateParameter(paramName, TemplateParameterKind.Dependent, isVariadic);
                TemplateParameter dependeParam = tp.GetTemplateParameter(dependName);
                Debug.Assert(dependeParam != null);
                param.SetExtra(dependeParam, default_value);
            }
            else
            {
                CXType     type       = clang.getCursorType(cursor);
                NativeType nativeType = TypeVisitorHelper.GetNativeType(AST_, type);
                param = new TemplateParameter(paramName, TemplateParameterKind.NoneType, isVariadic);
                param.SetExtra(nativeType, default_value);
            }
            return(param);
        }
Ejemplo n.º 7
0
        private CXChildVisitResult ParameterVisitor(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (CXCursorKind.CXCursor_ParmDecl == cursor.kind)
            {
                // prepare client data
                GCHandle astHandle            = (GCHandle)data;
                OnVisitFunctionParameter func = astHandle.Target as OnVisitFunctionParameter;

                CXType type = clang.getCursorType(cursor);

                FunctionParameter param = new FunctionParameter
                {
                    Name = clang.getCursorSpelling(cursor).ToString(),
                    Type = TypeVisitorHelper.GetNativeType(AST_, type)
                };

                clang.visitChildren(cursor, (CXCursor c, CXCursor p, IntPtr d) =>
                {
                    if (ClangTraits.IsLiteralCursor(c))
                    {
                        // get liter-string from token
                        CXSourceRange range = clang.getCursorExtent(c);
                        IntPtr tokens       = IntPtr.Zero;
                        uint numToken;
                        string liter = "";
                        clang.tokenize(ASTVisitor.CurrentTU, range, out tokens, out numToken);
                        IntPtr tmp = tokens;
                        for (uint loop = 0; loop < numToken; ++loop, IntPtr.Add(tmp, 1))
                        {
                            CXToken token = Marshal.PtrToStructure <CXToken>(tmp);
                            liter        += clang.getTokenSpelling(ASTVisitor.CurrentTU, token).ToString();
                        }
                        clang.disposeTokens(ASTVisitor.CurrentTU, tokens, numToken);

                        // set default literal
                        param.DefaultValue = liter;
                    }
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }, new CXClientData(IntPtr.Zero));

                func(param);
            }

            return(CXChildVisitResult.CXChildVisit_Recurse);
        }
Ejemplo n.º 8
0
        private void ProcessTypeExport(NativeClass thisClass, CXCursor cursor, CXCursor parent)
        {
            // createfield context
            TypeVisitContext context = new TypeVisitContext(cursor);

            // get field type
            CXType     type       = clang.getCursorType(cursor);
            NativeType nativeType = TypeVisitorHelper.GetNativeType(AST_, type, context);

            // get field access specifier
            AccessSpecifier access = ClangTraits.ToAccessSpecifier(clang.getCXXAccessSpecifier(cursor));

            // create the exported member type
            MemberType memberType = new MemberType
            {
                Access = access,
                Type   = nativeType
            };

            thisClass.AddMemberType(memberType);
        }
Ejemplo n.º 9
0
        private void ProcessBaseClass(NativeClass thisClass, CXCursor cursor, CXCursor parent)
        {
            // get class name
            CXType type = clang.getCursorType(cursor);

            // type visit context
            TypeVisitContext context = new TypeVisitContext(cursor);

            // create the base class
            BaseClass baseClass = new BaseClass
            {
                // check access specifier
                Access = ClangTraits.ToAccessSpecifier(clang.getCXXAccessSpecifier(cursor)),
                // native class type
                Type = TypeVisitorHelper.GetNativeType(AST_, type, context),
                // check is virtual base
                IsVirtual = clang.isVirtualBase(cursor) != 0
            };

            // register base class
            thisClass.AddBaseClass(baseClass);
        }
Ejemplo n.º 10
0
        private void ProcessField(NativeClass thisClass, CXCursor cursor, CXCursor parent, bool isStatic = false)
        {
            // get field name
            string fieldName = clang.getCursorSpelling(cursor).ToString();

            // get field type
            CXType     type       = clang.getCursorType(cursor);
            NativeType nativeType = TypeVisitorHelper.GetNativeType(AST_, type);

            // get field access specifier
            AccessSpecifier access = ClangTraits.ToAccessSpecifier(clang.getCXXAccessSpecifier(cursor));

            // create field object
            Field f = new Field
            {
                Name     = fieldName,
                Access   = access,
                Type     = nativeType,
                IsStatic = isStatic
            };

            thisClass.AddField(f);
        }
Ejemplo n.º 11
0
        public static bool VisitTemplateParameter(
            CXCursor cursor,
            CXType type,
            NativeClass @class,
            AST ast,
            TypeVisitContext context)
        {
            ClassTemplate template = @class.OriginalTemplate;

            Debug.Assert(template != null);
            Debug.Assert(template.TP != null);

            int templateArgNum = clang.Type_getNumTemplateArguments(type);

            if (templateArgNum < 0)
            {
                return(false);
            }

            @class.SetTemplateParameterCount(templateArgNum);
            int contextIndex = 0;

            for (uint loop = 0; loop < templateArgNum; ++loop)
            {
                TemplateParameter param = template.TP.GetTemplateParameter(loop);
                Debug.Assert(param != null);

                if (param.Kind == TemplateParameterKind.Type)
                {
                    CXType argType = clang.Type_getTemplateArgumentAsType(type, loop);
                    Debug.Assert(!ClangTraits.IsInvalid(argType));
                    NativeType nativeArgType = TypeVisitorHelper.GetNativeType(ast, argType, context);
                    @class.SetTemplateParameter(loop, nativeArgType);
                }
                else if (param.Kind == TemplateParameterKind.NoneType ||
                         param.Kind == TemplateParameterKind.Dependent)
                {
                    string literal;
                    if (context != null && !context.Empty)
                    {
                        literal = context.Consume();
                    }
                    else
                    {
                        literal = (param.Extra as TemplateNonTypeParam).DefaultLiteral;
                    }

                    Debug.Assert(literal != null);

                    @class.SetTemplateParameter(loop, literal);
                    ++contextIndex;
                }
                else
                {
                    Debug.Assert(TemplateParameterKind.Template == param.Kind);
                    // not support now
                    ClassTemplate templateParam = null;
                    @class.SetTemplateParameter(loop, templateParam);
                }
            }

            return(true);
        }