Exemplo n.º 1
0
 public CppTypeVisitorResult Visit(CppFunctionType t, Qualifiers q)
 {
     // has inner type
     this.TypeBuilder.Append($"Function()");
     this.DbgBuilder.Append(t.DbgDescription + " ");
     this.QualifierBuilder.Append(q.ToString("g") + " ");
     return(null);
 }
Exemplo n.º 2
0
 public static bool IsFunctionType(CppType type, out CppFunctionType cppFunctionType)
 {
     type            = type.GetCanonicalType();
     cppFunctionType = type as CppFunctionType;
     if (cppFunctionType == null)
     {
         if (type is CppPointerType ptrType && (ptrType.ElementType is CppFunctionType cppFunctionType2))
         {
             cppFunctionType = cppFunctionType2;
         }
         else
         {
             return(false);
         }
     }
Exemplo n.º 3
0
        private void GenerateDelegates(CppCompilation compilation, string outputPath)
        {
            Debug.WriteLine("Generating Delegates...");

            var delegates = compilation.Typedefs
                            .Where(t => t.TypeKind == CppTypeKind.Typedef &&
                                   t.ElementType is CppPointerType &&
                                   ((CppPointerType)t.ElementType).ElementType.TypeKind == CppTypeKind.Function)
                            .ToList();

            using (StreamWriter file = File.CreateText(Path.Combine(outputPath, "Delegates.cs")))
            {
                file.WriteLine("using System;\n");
                file.WriteLine("namespace WaveEngine.Bindings.RenderDoc");
                file.WriteLine("{");

                foreach (var funcPointer in delegates)
                {
                    Helpers.PrintComments(file, funcPointer.Comment, "\t");
                    CppFunctionType pointerType = ((CppPointerType)funcPointer.ElementType).ElementType as CppFunctionType;

                    var returnType = Helpers.ConvertToCSharpType(pointerType.ReturnType);
                    file.Write($"\tpublic unsafe delegate {returnType} {funcPointer.Name}(");

                    if (pointerType.Parameters.Count > 0)
                    {
                        file.Write("\n");

                        for (int i = 0; i < pointerType.Parameters.Count; i++)
                        {
                            if (i > 0)
                            {
                                file.Write(",\n");
                            }

                            var parameter     = pointerType.Parameters[i];
                            var convertedType = Helpers.ConvertToCSharpType(parameter.Type);
                            file.Write($"\t\t {convertedType} {parameter.Name}");
                        }
                    }

                    file.Write(");\n\n");
                }

                file.WriteLine("}");
            }
        }
Exemplo n.º 4
0
        private void DefineField(CodeType typeDecl, CppField field)
        {
            CodeMethod caller = null;

            TypeDesc fieldType = GetTypeDesc(field.Type);
            var      fld       = new CodeField(ResolveCefType(fieldType.ToString()), field.Name.EscapeName());

            if (fieldType.IsCallable)
            {
                fld.Attributes = CodeAttributes.Public;                // | CodeAttributes.ReadOnly;
                fld.Comments.AddVSDocComment(fieldType.Name, "summary");

                CppFunctionType fnType  = fieldType.FunctionTypeRef;
                TypeDesc        retType = GetTypeDesc(fnType.ReturnType);
                caller = new CodeMethod(field.Name.ToUpperCamel(fnType.Parameters.Count).EscapeName());
                var rvtype = new CodeMethodParameter(null);
                rvtype.Type = ResolveCefType(retType.ToString());
                if (retType.Name == "char16" || retType.Name == "wchar")
                {
                    rvtype.CustomAttributes.Add(new CustomCodeAttribute("return: MarshalAs(UnmanagedType.U2)"));
                    throw new NotImplementedException();                     // TODO: check it
                }
                caller.RetVal     = rvtype;
                caller.Attributes = CodeAttributes.Public | CodeAttributes.Unsafe;
                //caller.CustomAttributes.AddMethodImplForwardRefAttribute();
                caller.CustomAttributes.Add(new CustomCodeAttribute("NativeName")
                {
                    Parameters = { "\"" + field.Name + "\"" }
                });
                caller.Comments.AddVSDocComment(field.Comment, "summary");
                caller.Callee = fld;
                CppContainerList <CppParameter> @params = fnType.Parameters;
                for (int i = 0; i < @params.Count; i++)
                {
                    CppParameter arg = @params[i];
                    if (i == 0 && arg.Name == "self")
                    {
                        string argTypeName = ResolveCefType(arg.Type.GetDisplayName());
                        if (argTypeName == typeDecl.Name + "*")
                        {
                            caller.HasThisArg = true;
                            continue;
                        }
                    }
                    var      param     = new CodeMethodParameter(arg.Name.EscapeName());
                    TypeDesc paramType = GetTypeDesc(arg.Type);

                    string argType = paramType.ToString();
                    while (argType.StartsWith("const "))
                    {
                        argType         = argType.Substring(6);
                        param.Direction = CodeMethodParameterDirection.In;
                    }
                    if (param.Direction == CodeMethodParameterDirection.In)
                    {
                        param.CustomAttributes.Add(new CustomCodeAttribute("Immutable"));
                    }
                    if (argType == "char16" || argType == "wchar")
                    {
                        param.CustomAttributes.Add(new CustomCodeAttribute("MarshalAs(UnmanagedType.U2)"));
                        throw new NotImplementedException();                         // TODO: check it
                    }
                    param.Type = ResolveCefType(argType);
                    caller.Parameters.Add(param);
                }

                //fld.TypeName = GetNativeDelegate(caller, typeDecl);
                caller.Body = GetNativeCallMehtodBody(caller, fld, typeDecl);
            }
            else
            {
                fld.Comments.AddVSDocComment(field.Comment, "summary");
                fld.Attributes = CodeAttributes.Public;
                if (fieldType.Name == "char16" || fieldType.Name == "wchar")
                {
                    fld.CustomAttributes.Add(new CustomCodeAttribute("MarshalAs(UnmanagedType.U2)"));
                }
            }
            typeDecl.Members.Add(fld);
            if (caller != null)
            {
                typeDecl.Members.Add(caller);
            }
        }