public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     bool result = base.VisitTranslationUnit(unit);
     unit.Classes.AddRange(internalImpls);
     internalImpls.Clear();
     return result;
 }
Beispiel #2
0
        public void Process(Namespace @namespace, bool filterNamespaces = false)
        {
            TranslationUnit = @namespace.TranslationUnit;

            var collector = new RecordCollector(TranslationUnit);
            @namespace.Visit(collector);

            foreach (var record in collector.Declarations)
            {
                if (record.Value is Namespace)
                    continue;

                if (record.Value.IsDependent)
                    continue;

                if (filterNamespaces)
                {
                    var declNamespace = GetEffectiveNamespace(record.Value);

                    var isSameNamespace = declNamespace == @namespace;
                    if (declNamespace != null)
                        isSameNamespace |= declNamespace.QualifiedName == @namespace.QualifiedName;

                    if (!isSameNamespace)
                        continue;
                }

                record.Value.Visit(this);
                GenerateInclude(record);
            }
        }
Beispiel #3
0
        private static void GenerateFileBeginning(TextWriter writer, CppSharp.AST.TranslationUnit unit)
        {
            string beggings = @"/*
 * copyright (c) 2013 Crazyender
 *
 * This file is part of FFmpeg.mono
 *
 * FFmpeg.mono is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg.mono is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */


using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security;";

            writer.WriteLine(beggings);
            writer.WriteLine("namespace " + nameSpace + "\r\n" +
                             "{\r\n" +
                             "    public partial class " + className + " \r\n" +
                             "{\r\n" +
                             "public const string " + unit.FileNameWithoutExtension.ToUpper() + " = \"" + dllName + "\";\r\n\r\n");;
        }
Beispiel #4
0
        public Namespace ConvertForwardReferencesToNamespaces(
            IEnumerable<CLITypeReference> typeReferences)
        {
            // Create a new tree of namespaces out of the type references found.
            var rootNamespace = new TranslationUnit();
            rootNamespace.Module = TranslationUnit.Module;

            var sortedRefs = typeReferences.ToList();
            sortedRefs.Sort((ref1, ref2) =>
                string.CompareOrdinal(ref1.FowardReference, ref2.FowardReference));

            var forwardRefs = new SortedSet<string>();

            foreach (var typeRef in sortedRefs)
            {
                if (string.IsNullOrWhiteSpace(typeRef.FowardReference))
                    continue;

                var declaration = typeRef.Declaration;
                if (!(declaration.Namespace is Namespace))
                    continue;

                if (!forwardRefs.Add(typeRef.FowardReference))
                    continue;

                if (typeRef.Include.InHeader)
                    continue;

                var @namespace = FindCreateNamespace(rootNamespace, declaration);
                @namespace.TypeReferences.Add(typeRef);
            }

            return rootNamespace;
        }
Beispiel #5
0
        private static void TranslateClass(TextWriter writer, CppSharp.AST.TranslationUnit unit)
        {
            foreach (var @class in unit.Classes)
            {
                if (@class.Fields.Count == 0)
                {
                    continue;
                }
                writer.WriteLine("public struct " + @class.Name + "{");

                foreach (var field in @class.Fields)
                {
                    string marshalTag = TypeHeloer.GetMarshalTag(field.Type);
                    if (!string.IsNullOrEmpty(marshalTag))
                    {
                        writer.WriteLine("\t" + marshalTag);
                    }

                    string type = TypeHeloer.GetSystemTypeName(field.Type, true);
                    writer.Write("\tpublic ");
                    writer.Write(type);
                    writer.Write(" ");
                    writer.WriteLine(field.Name + ";");
                    writer.WriteLine();
                }

                writer.WriteLine("};");
                writer.WriteLine();
            }
        }
Beispiel #6
0
        private static void GenerateInteropFile(CppSharp.AST.TranslationUnit unit)
        {
            if (unit.FilePath.IndexOf("Microsoft") != -1)
            {
                return;
            }

            TypeWrapperFactory.CurrentPrefix = unit.FileNameWithoutExtension;
            var    outFile = unit.FileNameWithoutExtension + ".cs";
            string path    = unit.FilePath;

            foreach (var inc in includes)
            {
                path = path.Replace(inc, "");
            }

            var folder = path.Replace(unit.FileName, "").Replace("\\", "");

            if (string.IsNullOrEmpty(folder))
            {
                folder = "default";
            }
            else if (sourceFile.IndexOf(folder) == -1)
            {
                return;
            }

            DirectoryInfo info = null;

            if (string.IsNullOrEmpty(folder))
            {
                info = new DirectoryInfo(Environment.CurrentDirectory);
            }
            else
            {
                info = Directory.CreateDirectory(folder);
            }
            var savedPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = info.FullName;

            if (File.Exists(outFile))
            {
                File.Delete(outFile);
            }
            TextWriter writer = new StreamWriter(outFile);

            GenerateFileBeginning(writer, unit);

            GenerateContext(writer, unit);

            GenerateFileEnd(writer, unit);



            writer.Flush();
            writer.Close();

            Environment.CurrentDirectory = savedPath;
        }
Beispiel #7
0
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            // Try to get an include path that works from the original include
            // directories paths.

            unit.IncludePath = GetIncludePath(unit.FilePath);
            return true;
        }
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     bool result = base.VisitTranslationUnit(unit);
     foreach (var @interface in interfaces)
         @interface.Key.Namespace.Classes.Add(@interface.Value);
     interfaces.Clear();
     return result;
 }
Beispiel #9
0
 private static void GenerateContext(TextWriter writer, CppSharp.AST.TranslationUnit unit)
 {
     TranslatMacro(writer, unit);
     TranslatEnum(writer, unit);
     TranslatFunction(writer, unit);
     TranslateClass(writer, unit);
     TranslateDelegate(writer, unit);
 }
        public override List<Template> Generate(TranslationUnit unit)
        {
            var outputs = new List<Template>();

            var template = new CSharpTextTemplate(Driver, unit, typePrinter, expressionPrinter);
            outputs.Add(template);

            return outputs;
        }
Beispiel #11
0
 private static void TranslatMacro(TextWriter writer, CppSharp.AST.TranslationUnit unit)
 {
     foreach (var macro in unit.Macros)
     {
         string target = macro.ToString();
         var    enums  = ParseEnums(target);
         writer.WriteLine("public static readonly uint " + macro.Name + " = " + enums + ";");
     }
 }
Beispiel #12
0
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     if (!this.eventAdded)
     {
         this.generator.OnUnitGenerated += this.OnUnitGenerated;
         this.eventAdded = true;
     }
     return base.VisitTranslationUnit(unit);
 }
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     if (!unit.IsGenerated)
         return false;
     var result = base.VisitTranslationUnit(unit);
     foreach (var overload in overloads)
         overload.Key.Functions.AddRange(overload.Value);
     overloads.Clear();
     return result;
 }
Beispiel #14
0
        public virtual bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (unit.Ignore)
                return false;

            if (unit.IsSystemHeader)
                return false;

            VisitDeclarationContext(unit);

            return true;
        }
Beispiel #15
0
        public virtual bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (unit.IsSystemHeader)
                return false;

            if (ClearVisitedDeclarations)
                Visited.Clear();

            VisitDeclarationContext(unit);

            return true;
        }
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!unit.IsValid)
                return false;

            if (ClearVisitedDeclarations)
                Visited.Clear();

            VisitDeclarationContext(unit);

            return true;
        }
Beispiel #17
0
        public override List<Template> Generate(TranslationUnit unit)
        {
            var outputs = new List<Template>();

            var header = new CLIHeadersTemplate(Driver, unit);
            outputs.Add(header);

            var source = new CLISourcesTemplate(Driver, unit);
            outputs.Add(source);

            return outputs;
        }
Beispiel #18
0
        public virtual bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!unit.IsValid || unit.Ignore)
                return false;

            if (ClearVisitedDeclarations)
                Visited.Clear();

            VisitDeclarationContext(unit);

            return true;
        }
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            var result = base.VisitTranslationUnit(unit);
            foreach (var internalImpl in internalImpls)
                if (internalImpl.Namespace != null)
                    internalImpl.Namespace.Classes.Add(internalImpl);
                else
                    unit.Classes.AddRange(internalImpls);

            internalImpls.Clear();
            return result;
        }
Beispiel #20
0
        bool IsExternalDeclaration(TranslationUnit translationUnit)
        {
            if (DriverOptions.NoGenIncludeDirs == null)
                return false;

            foreach (var path in DriverOptions.NoGenIncludeDirs)
            {
                if (translationUnit.FilePath.StartsWith(path))
                    return true;
            }

            return false;
        }
Beispiel #21
0
 private static void TranslatEnum(TextWriter writer, CppSharp.AST.TranslationUnit unit)
 {
     foreach (var enums in unit.Enums)
     {
         writer.WriteLine("public enum " + enums.Name);
         writer.WriteLine("{");
         foreach (var item in enums.Items)
         {
             writer.WriteLine("\t" + item.Name + " = " + item.Value.ToString() + ",");
         }
         writer.WriteLine("}");
         writer.WriteLine("");
     }
 }
Beispiel #22
0
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (IsExternalDeclaration(unit) && unit.IsGenerated)
                unit.GenerationKind = GenerationKind.Link;

            // Try to get an include path that works from the original include
            // directories paths.
            if (unit.IsValid)
            {
                unit.IncludePath = GetIncludePath(unit.FilePath);
                return true;
            }
            return false;
        }
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!unit.IsValid)
                return false;

            if (unit.IsSystemHeader)
                unit.ExplicitlyIgnore();

            if (ClearVisitedDeclarations)
                Visited.Clear();

            VisitDeclarationContext(unit);

            return true;
        }
Beispiel #24
0
        public override AST.Declaration VisitTranslationUnit(TranslationUnit decl)
        {
            var _unit = new AST.TranslationUnit();

            _unit.FilePath       = decl.FileName;
            _unit.IsSystemHeader = decl.IsSystemHeader;

            for (uint i = 0; i < decl.MacrosCount; ++i)
            {
                var macro  = decl.getMacros(i);
                var _macro = Visit(macro) as AST.MacroDefinition;
                _unit.Macros.Add(_macro);
            }

            VisitDeclaration(decl, _unit);
            VisitDeclContext(decl, _unit);
            return(_unit);
        }
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!base.VisitTranslationUnit(unit))
                return false;

            var fileName = unit.TranslationUnit.FileName;
            if (rootNamespaceRenames.ContainsKey(fileName))
            {
                var rootNamespace = rootNamespaceRenames[fileName];
                if (this.Driver.Options.OutputNamespace != rootNamespace)
                    unit.Name = rootNamespace;
            }
            else if (unit.GenerationKind == GenerationKind.Generate)
            {
                rootNamespaceRenames.Add(fileName, Driver.Options.OutputNamespace);
            }
            return true;
        }
Beispiel #26
0
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (unit.IsValid && !unit.IsSystemHeader && unit.HasDeclarations)
            {
                var includeDir = Path.GetDirectoryName(unit.FilePath);
                unit.Module = DriverOptions.Modules.FirstOrDefault(
                    m => m.IncludeDirs.Contains(includeDir)) ?? DriverOptions.MainModule;
                unit.Module.Units.Add(unit);
            }

            // Try to get an include path that works from the original include
            // directories paths.
            if (unit.IsValid)
            {
                unit.IncludePath = GetIncludePath(unit.FilePath);
                return true;
            }
            return false;
        }
Beispiel #27
0
        private static void TranslatFunction(TextWriter writer, CppSharp.AST.TranslationUnit unit)
        {
            /*
             * [DllImport(AVCODEC), SuppressUnmanagedCodeSecurity]
             * public static extern int avcodec_default_get_buffer2(IntPtr pAVCodecContext, IntPtr pAVFrame, int flags);
             */
            foreach (var function in unit.Functions)
            {
                writer.WriteLine("[DllImport(" + unit.FileNameWithoutExtension.ToUpper() + "), SuppressUnmanagedCodeSecurity]");
                writer.Write("public static extern ");
                writer.Write(TypeHeloer.GetSystemTypeName(function.ReturnType.Type, true));
                writer.Write(" ");
                writer.Write(function.Name);
                writer.Write("(\r\n");

                for (int i = 0; i < function.Parameters.Count; i++)
                {
                    var    param      = function.Parameters[i];
                    string marshalTag = TypeHeloer.GetMarshalTag(param.Type);
                    if (!string.IsNullOrEmpty(marshalTag))
                    {
                        writer.WriteLine("\t" + marshalTag);
                    }
                    writer.Write("\t" + TypeHeloer.GetSystemTypeName(param.Type, true));
                    writer.Write(" ");
                    if (!string.IsNullOrEmpty(param.Name))
                    {
                        writer.Write(param.Name);
                    }
                    else
                    {
                        writer.Write(" __arg" + i.ToString());
                    }
                    if (i != (function.Parameters.Count - 1))
                    {
                        writer.Write(", \r\n");
                    }
                }

                writer.Write(");");
                writer.Write("\r\n\r\n");
            }
        }
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!base.VisitTranslationUnit(unit) || !unit.IsValid ||
                unit.IsSystemHeader || !unit.HasDeclarations)
                return false;

            var fileName = unit.TranslationUnit.FileName;
            if (rootNamespaceRenames.ContainsKey(fileName))
            {
                var rootNamespace = rootNamespaceRenames[fileName];
                unit.Name = rootNamespace;
            }
            else if (unit.GenerationKind == GenerationKind.Generate)
            {
                if (Driver.Options.IsCSharpGenerator)
                    unit.Name = unit.Module.OutputNamespace;
                rootNamespaceRenames.Add(fileName, unit.Module.OutputNamespace);
            }
            return true;
        }
Beispiel #29
0
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            if (!base.VisitTranslationUnit(unit))
                return false;

            if (unit.IsSystemHeader)
            {
                unit.Module = Options.SystemModule;
            }
            else
            {
                var includeDir = Path.GetFullPath(Path.GetDirectoryName(unit.FilePath));
                unit.Module = Options.Modules.FirstOrDefault(
                    m => m.IncludeDirs.Any(i => Path.GetFullPath(i) == includeDir)) ??
                    Options.MainModule;
            }
            unit.Module.Units.Add(unit);
            // Try to get an include path that works from the original include directories paths
            unit.IncludePath = GetIncludePath(unit.FilePath);
            return true;
        }
Beispiel #30
0
 public string VisitTranslationUnit(TranslationUnit unit)
 {
     return(VisitDeclaration(unit));
 }
Beispiel #31
0
        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            var expansions = unit.PreprocessedEntities.OfType<MacroExpansion>();

            if (expansions.Any(e => e.Text == Prefix + "_IGNORE_FILE"))
            {
                unit.ExplicitlyIgnore();
            }

            return base.VisitTranslationUnit(unit);
        }
Beispiel #32
0
 private static void GenerateFileEnd(TextWriter writer, CppSharp.AST.TranslationUnit unit)
 {
     writer.WriteLine("}\r\n}\r\n");
 }
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     currentUnit = unit;
     return base.VisitTranslationUnit(unit);
 }
Beispiel #34
0
 /// <summary>
 /// Generates the outputs for a given translation unit.
 /// </summary>
 public abstract List<Template> Generate(TranslationUnit unit);
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     return !unit.IsSystemHeader && unit.IsGenerated && base.VisitTranslationUnit(unit);
 }
Beispiel #36
0
 protected CLITextTemplate(Driver driver, TranslationUnit unit)
     : base(driver, unit)
 {
     TypePrinter = new CLITypePrinter(driver);
     Includes = new HashSet<Include>();
 }
Beispiel #37
0
 public virtual bool VisitTranslationUnit(TranslationUnit unit)
 {
     return(VisitNamespace(unit));
 }
Beispiel #38
0
        private void GenerateTemplate(TranslationUnit unit, ICollection<GeneratorOutput> outputs)
        {
            var templates = Generate(new[] { unit });
            if (templates.Count == 0)
                return;

            foreach (var template in templates)
            {
                template.Process();
            }

            var output = new GeneratorOutput
            {
                TranslationUnit = unit,
                Templates = templates
            };
            outputs.Add(output);

            OnUnitGenerated(output);
        }
Beispiel #39
0
        private string GetIncludePath(TranslationUnit translationUnit)
        {
            if (!DriverOptions.UseHeaderDirectories)
                return translationUnit.FileName;

            var rel = PathHelpers.GetRelativePath(
                TranslationUnit.FileRelativeDirectory,
                translationUnit.FileRelativeDirectory);

            if (string.IsNullOrEmpty(rel))
                return translationUnit.FileName;

            return Path.Combine(rel, translationUnit.FileName);
        }
 public override bool VisitTranslationUnit(TranslationUnit unit)
 {
     bool result = base.VisitTranslationUnit(unit);
     GenerateProperties();
     return result;
 }
Beispiel #41
0
 protected Template(Driver driver, TranslationUnit unit)
 {
     Driver = driver;
     Options = driver.Options;
     TranslationUnit = unit;
     RootBlock = new Block();
     ActiveBlock = RootBlock;
 }