Exemplo n.º 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();
        }
Exemplo n.º 2
0
        static void WriteFile(Config config, ParsedFile file, StreamWriter writer)
        {
            var module = config.ModuleName;

            if (writer == null && config.UseHeaderFolder && config.BaseSourceFolder != file.Folder)
            {
                module = file.Folder;
                var dst = Path.Combine(config.DstDir, file.Folder);
                Directory.CreateDirectory(dst);
                writer = new StreamWriter(File.Open(Path.Combine(dst, file.Filename + ".v"), FileMode.Create));
            }
            else if (writer == null)
            {
                writer = new StreamWriter(File.Open(Path.Combine(config.DstDir, file.Filename + ".v"), FileMode.Create));
            }

            if (config.SingleVFileExport)
            {
                writer.WriteLine();
                writer.WriteLine($"// {file.Folder}/{file.Filename}");
            }
            else
            {
                writer.WriteLine($"module {module}");
            }
            writer.WriteLine();

            // write out our types and our C declarations first
            foreach (var e in file.Enums)
            {
                WriteEnum(writer, e, config);
            }

            foreach (var s in file.Structs)
            {
                WriteStruct(writer, s);
            }

            foreach (var f in file.ParsedFunctions)
            {
                WriteCFunction(writer, f);
            }

            if (!config.SingleVFileExport)
            {
                writer.Dispose();
            }
        }
Exemplo n.º 3
0
        static ParsedFile GetOrCreateFile(List <ParsedFile> files, CppSourceSpan span, bool singleFileExport)
        {
            if (singleFileExport)
            {
                if (files.Count == 0)
                {
                    files.Add(new ParsedFile(null, null));
                }
                return(files[0]);
            }

            var filename = span.FilenameNoExtension();
            var folder   = span.Folder();

            var file = files.Where(f => f.Filename == filename && f.Folder == folder).FirstOrDefault();

            if (file == null)
            {
                file = new ParsedFile(filename, folder);
                files.Add(file);
            }
            return(file);
        }
Exemplo n.º 4
0
 public static bool IsFileExcludedFromVWrapper(this Config config, ParsedFile file)
 {
     return(config.ExcludedFromVWrapperFiles.Contains(file.Filename) ||
            config.ExcludedFromVWrapperFiles.Contains(Path.Combine(file.Folder, file.Filename)));
 }