Exemple #1
0
        public static void Generate(Config config, CppCompilation comp)
        {
            var typeMap = new TypedefMap(comp.Typedefs);

            V.AddTypeConversions(config.CTypeToVType);

            // add conversions for any types in the lib
            foreach (var s in comp.Classes)
            {
                var mappedName = typeMap.GetOrNot(s.Name);
                if (mappedName != s.Name)
                {
                    V.AddTypeConversion(s.Name, mappedName);
                }
                V.AddTypeConversion(typeMap.GetOrNot(s.Name));
            }

            // enums will all be replaced by our V enums
            foreach (var e in comp.Enums)
            {
                var mappedName = typeMap.GetOrNot(e.Name);
                if (mappedName != e.Name)
                {
                    V.AddTypeConversion(mappedName, V.GetVEnumName(config.StripFunctionPrefix(e.Name)));
                }
                V.AddTypeConversion(e.Name, V.GetVEnumName(config.StripFunctionPrefix(e.Name)));
            }

            Directory.CreateDirectory(config.DstDir);

            StreamWriter writer = null;

            if (config.SingleVFileExport)
            {
                writer = new StreamWriter(File.Open(Path.Combine(config.DstDir, config.CDeclarationFileName), FileMode.Create));
                writer.WriteLine($"module {config.ModuleName}");
            }

            var parsedFiles = ParsedFile.ParseIntoFiles(comp, config);

            foreach (var file in parsedFiles)
            {
                WriteFile(config, file, writer);
            }
            writer?.Dispose();

            // now we write the V wrapper
            writer = new StreamWriter(File.Open(Path.Combine(config.DstDir, config.VWrapperFileName), FileMode.Create));
            writer.WriteLine($"module {config.ModuleName}\n");
            foreach (var file in parsedFiles.Where(f => !config.IsFileExcludedFromVWrapper(f)))
            {
                foreach (var func in file.ParsedFunctions)
                {
                    WriteVFunction(writer, func);
                }
            }
            writer.Dispose();
        }
Exemple #2
0
        public static void Dump(this CppCompilation comp, bool printComments = false)
        {
            var typeMap = new TypedefMap(comp.Typedefs);

            _printComments = printComments;

            if (comp.Diagnostics.Messages.Count > 0)
            {
                Console.WriteLine("------ Messages ------");
            }
            foreach (var message in comp.Diagnostics.Messages)
            {
                Console.WriteLine(message);
            }

            if (comp.Macros.Count > 0)
            {
                Console.WriteLine("\n------ Macros ------");
            }
            foreach (var macro in comp.Macros)
            {
                Console.WriteLine(macro);
            }

            if (comp.Typedefs.Count > 0)
            {
                Console.WriteLine("\n------ Typedefs ------");
            }
            foreach (var typedef in comp.Typedefs)
            {
                PrintComment(typedef.Comment);
                Console.WriteLine(typedef);
            }

            if (comp.Enums.Count > 0)
            {
                Console.WriteLine("\n------ Enums ------");
            }
            foreach (var @enum in comp.Enums)
            {
                PrintComment(@enum.Comment);
                Console.WriteLine($"enum {typeMap.GetOrNot(@enum.Name)}");
                Console.WriteLine($"\tType: {@enum.IntegerType}");

                foreach (var t in @enum.Items)
                {
                    Console.WriteLine($"\t{t}");
                }

                if (comp.Enums.Last() != @enum)
                {
                    Console.WriteLine();
                }
            }

            if (comp.Functions.Count > 0)
            {
                Console.WriteLine("\n------ Functions ------");
            }
            foreach (var cppFunction in comp.Functions)
            {
                PrintComment(cppFunction.Comment);
                Console.WriteLine(cppFunction);
            }

            if (comp.Classes.Count > 0)
            {
                Console.WriteLine("\n------ Structs ------");
            }
            foreach (var cppClass in comp.Classes)
            {
                if (cppClass.ClassKind != CppClassKind.Struct)
                {
                    Console.WriteLine($"Error: found a non-struct type! {cppClass.ClassKind} - {cppClass.Name}");
                }
                PrintComment(cppClass.Comment);
                Console.WriteLine($"struct {cppClass.Name}");
                foreach (var field in cppClass.Fields)
                {
                    if (field.Type.TypeKind == CppTypeKind.Array)
                    {
                        Console.WriteLine($"\t-- array --");
                    }

                    if (field.Type.TypeKind == CppTypeKind.StructOrClass && field.Parent is CppClass parent)
                    {
                        Console.WriteLine($"\t{parent.Name} {field.Name}");
                    }
                    else
                    {
                        var typeName = field.Type.ToString();
                        if (field.Type.TypeKind == CppTypeKind.Typedef)
                        {
                            var t = field.Type as CppTypedef;
                            typeName = t.Name;
                        }

                        Console.WriteLine($"\t{typeName} {field.Name}");
                    }
                }

                if (comp.Classes.Last() != cppClass)
                {
                    Console.WriteLine();
                }
            }
        }