Ejemplo n.º 1
0
        private static void WriteNamespaceClose(INamedTypeSymbol namedTypeSymbol, CCodeWriterText c)
        {
            foreach (var namespaceNode in namedTypeSymbol.ContainingNamespace.EnumNamespaces())
            {
                c.DecrementIndent();
                c.TextSpan("}");
            }

            c.NewLine();
        }
Ejemplo n.º 2
0
        private static void WriteNamespaceOpen(INamedTypeSymbol namedTypeSymbol, CCodeWriterText c)
        {
            bool any = false;

            foreach (var namespaceNode in namedTypeSymbol.ContainingNamespace.EnumNamespaces())
            {
                c.TextSpan("namespace ");
                c.WriteNamespaceName(namespaceNode);
                c.TextSpan(" { ");
                any = true;
            }

            if (any)
            {
                c.IncrementIndent();
                c.NewLine();
            }

            /*
             * // include using of namespace
             * c.TextSpan("using");
             * c.WhiteSpace();
             * c.TextSpan("namespace");
             * c.WhiteSpace();
             * c.WriteNamespace(namedTypeSymbol.ContainingNamespace);
             * c.EndStatement();
             * c.Separate();
             */

            // write alias for _
            c.TextSpan("namespace");
            c.WhiteSpace();
            c.TextSpan("_");
            c.WhiteSpace();
            c.TextSpan("=");
            c.WhiteSpace();
            c.WriteNamespace(namedTypeSymbol.ContainingNamespace);
            c.EndStatement();
            c.Separate();
            c.NewLine();
        }
Ejemplo n.º 3
0
        private static void WriteNamespaceOpen(INamedTypeSymbol namedTypeSymbol, CCodeWriterText c)
        {
            bool any = false;

            foreach (var namespaceNode in namedTypeSymbol.ContainingNamespace.EnumNamespaces())
            {
                c.TextSpan("namespace ");
                c.WriteNamespaceName(namespaceNode);
                c.TextSpan(" { ");
                any = true;
            }

            if (any)
            {
                c.IncrementIndent();
                c.NewLine();
            }
        }
Ejemplo n.º 4
0
        private static void WriteEnum(IndentedTextWriter itw, CCodeWriterText c, INamedTypeSymbol namedTypeSymbol)
        {
            itw.WriteLine();
            itw.Write("enum class ");
            c.WriteTypeName(namedTypeSymbol, false, true);
            itw.Write(" : ");
            c.WriteType(namedTypeSymbol.EnumUnderlyingType);

            c.NewLine();
            c.OpenBlock();

            var constantValueTypeDiscriminator = namedTypeSymbol.EnumUnderlyingType.SpecialType.GetDiscriminator();

            var any = false;

            foreach (var constValue in namedTypeSymbol.GetMembers().OfType <IFieldSymbol>().Where(f => f.IsConst))
            {
                if (any)
                {
                    c.TextSpan(",");
                    c.WhiteSpace();
                }

                c.TextSpan("c_");
                c.WriteName(constValue);
                if (constValue.ConstantValue != null)
                {
                    c.TextSpan(" = ");
                    new Literal {
                        Value = ConstantValue.Create(constValue.ConstantValue, constantValueTypeDiscriminator)
                    }
                    .WriteTo(c);
                }

                any = true;
            }

            c.EndBlockWithoutNewLine();
            c.EndStatement();
        }
Ejemplo n.º 5
0
        private static void WriteFullDeclarationForUnit(CCodeUnit unit, IndentedTextWriter itw, CCodeWriterText c)
        {
            var namedTypeSymbol = (INamedTypeSymbol)unit.Type;

            WriteTemplateTraits(c, namedTypeSymbol);

            WriteNamespaceOpen(namedTypeSymbol, c);

            // write extern declaration
            var externDeclarations = unit.Declarations.Select(
                declaration => new { declaration, codeMethodDeclaration = declaration as CCodeMethodDeclaration })
                                     .Where(@t => @t.codeMethodDeclaration != null && @t.codeMethodDeclaration.IsExternDeclaration)
                                     .Select(@t => @t.declaration).ToList();

            if (externDeclarations.Any())
            {
                itw.Write("extern \"C\"");
                c.WhiteSpace();
                c.OpenBlock();

                foreach (var declaration in externDeclarations)
                {
                    declaration.WriteTo(c);
                }

                c.EndBlock();
            }

            if (namedTypeSymbol.IsGenericType)
            {
                c.WriteTemplateDeclaration(namedTypeSymbol);
            }

            itw.Write(namedTypeSymbol.IsValueType ? "struct" : "class");
            itw.Write(" ");
            c.WriteTypeName(namedTypeSymbol, false);
            if (namedTypeSymbol.BaseType != null)
            {
                itw.Write(" : public ");
                c.WriteTypeFullName(namedTypeSymbol.BaseType);
            }

            itw.WriteLine();
            itw.WriteLine("{");
            itw.WriteLine("public:");
            itw.Indent++;

            // base declaration
            if (namedTypeSymbol.BaseType != null)
            {
                itw.Write("typedef ");
                c.WriteTypeFullName(namedTypeSymbol.BaseType, false);
                itw.WriteLine(" base;");
            }

            foreach (var method in namedTypeSymbol.IterateAllMethodsWithTheSameNamesTakeOnlyOne())
            {
                c.TextSpan("using");
                c.WhiteSpace();
                c.WriteType(namedTypeSymbol.BaseType ?? method.ReceiverType, suppressReference: true, allowKeywords: true);
                c.TextSpan("::");
                c.WriteMethodName(method);
                c.TextSpan(";");
                c.NewLine();
            }

            if (namedTypeSymbol.TypeKind == TypeKind.Enum)
            {
                // value holder for enum
                c.WriteType(namedTypeSymbol);
                itw.WriteLine(" m_value;");
            }

            if (namedTypeSymbol.IsRuntimeType())
            {
                c.WriteTypeName(namedTypeSymbol, false);
                itw.WriteLine("() = default;");
            }

            /*
             * if (namedTypeSymbol.IsIntPtrType())
             * {
             *  c.WriteTypeName(namedTypeSymbol, false);
             *  itw.WriteLine("() = default;");
             * }
             */

            foreach (var declaration in unit.Declarations)
            {
                var codeMethodDeclaration = declaration as CCodeMethodDeclaration;
                if (codeMethodDeclaration == null || !codeMethodDeclaration.IsExternDeclaration)
                {
                    declaration.WriteTo(c);
                    if (codeMethodDeclaration != null && codeMethodDeclaration.MethodBodyOpt != null)
                    {
                        c.Separate();
                    }
                }
            }

            // write interface wrappers
            foreach (var iface in namedTypeSymbol.Interfaces)
            {
                WriteInterfaceWrapper(c, iface, namedTypeSymbol);
            }

            itw.Indent--;
            itw.WriteLine("};");

            WriteNamespaceClose(namedTypeSymbol, c);
        }
Ejemplo n.º 6
0
        public void WriteHeader(AssemblyIdentity identity, ISet <AssemblyIdentity> references, bool isCoreLib, IEnumerable <CCodeUnit> units, IEnumerable <string> includeHeaders)
        {
            // write header
            var text = new StringBuilder();

            using (var itw = new IndentedTextWriter(new StringWriter(text)))
            {
                itw.WriteLine("#ifndef HEADER_{0}", identity.Name.CleanUpName());
                itw.WriteLine("#define HEADER_{0}", identity.Name.CleanUpName());

                var c = new CCodeWriterText(itw);

                if (isCoreLib)
                {
                    itw.WriteLine(Resources.c_include);
                    itw.WriteLine(Resources.intrin_template);
                }
                else
                {
                    foreach (var reference in references)
                    {
                        itw.WriteLine("#include \"{0}.h\"", reference.Name);
                    }

                    c.Separate();
                    c.NewLine();
                }

                // write forward declaration
                foreach (var unit in units)
                {
                    WriteForwardDeclarationForUnit(unit, itw, c);
                }

                itw.WriteLine();

                if (isCoreLib)
                {
                    itw.WriteLine(Resources.c_forward_declarations);
                }

                // write full declaration
                foreach (var unit in units)
                {
                    itw.WriteLine();
                    WriteFullDeclarationForUnit(unit, itw, c);
                }

                if (isCoreLib)
                {
                    itw.WriteLine();
                    itw.WriteLine(Resources.c_declarations);
                    itw.WriteLine(Resources.c_template_definitions);
                    itw.WriteLine(Resources.overflow);
                }

                foreach (var unit in units)
                {
                    var namedTypeSymbol = (INamedTypeSymbol)unit.Type;
                    if (namedTypeSymbol.TypeKind == TypeKind.Delegate)
                    {
                        itw.WriteLine();
                        WriteNamespaceOpen(namedTypeSymbol, c);
                        new CCodeDelegateWrapperClass(namedTypeSymbol).WriteTo(c);
                        WriteNamespaceClose(namedTypeSymbol, c);
                    }
                }

                foreach (var includeHeader in includeHeaders)
                {
                    itw.WriteLine("#include \"{0}\"", includeHeader);
                }

                itw.WriteLine("#endif");

                itw.Close();
            }

            var path    = this.GetPath(identity.Name, subFolder: "src");
            var newText = text.ToString();

            if (IsNothingChanged(path, newText))
            {
                return;
            }

            using (var textFile = new StreamWriter(path))
            {
                textFile.Write(newText);
            }
        }
Ejemplo n.º 7
0
        private static void WriteFullDeclarationForUnit(CCodeUnit unit, IndentedTextWriter itw, CCodeWriterText c)
        {
            var any             = false;
            var namedTypeSymbol = (INamedTypeSymbol)unit.Type;

            foreach (var namespaceNode in namedTypeSymbol.ContainingNamespace.EnumNamespaces())
            {
                itw.Write("namespace ");
                c.WriteNamespaceName(namespaceNode);
                itw.Write(" { ");
                any = true;
            }

            if (any)
            {
                itw.Indent++;
                itw.WriteLine();
            }

            if (namedTypeSymbol.IsGenericType)
            {
                c.WriteTemplateDeclaration(namedTypeSymbol);
            }

            itw.Write(namedTypeSymbol.IsValueType ? "struct" : "class");
            itw.Write(" ");
            c.WriteTypeName(namedTypeSymbol, false);
            if (namedTypeSymbol.BaseType != null)
            {
                itw.Write(" : public ");
                c.WriteTypeFullName(namedTypeSymbol.BaseType);
            }

            itw.WriteLine();
            itw.WriteLine("{");
            itw.WriteLine("public:");
            itw.Indent++;

            // base declaration
            if (namedTypeSymbol.BaseType != null)
            {
                itw.Write("typedef ");
                c.WriteTypeFullName(namedTypeSymbol.BaseType, false);
                itw.WriteLine(" base;");
            }

            foreach (var method in namedTypeSymbol.IterateAllMethodsWithTheSameNamesTakeOnlyOne())
            {
                c.TextSpan("using");
                c.WhiteSpace();
                c.WriteType(namedTypeSymbol.BaseType ?? method.ReceiverType, suppressReference: true, allowKeywords: true);
                c.TextSpan("::");
                c.WriteMethodName(method);
                c.TextSpan(";");
                c.NewLine();
            }

            if (namedTypeSymbol.TypeKind == TypeKind.Enum)
            {
                // value holder for enum
                c.WriteType(namedTypeSymbol);
                itw.WriteLine(" m_value;");
            }

            /*
             * if (!unit.HasDefaultConstructor)
             * {
             *  c.WriteTypeName(namedTypeSymbol, false);
             *  itw.WriteLine("() = default;");
             * }
             */

            foreach (var declaration in unit.Declarations)
            {
                declaration.WriteTo(c);
            }

            // write interface wrappers
            foreach (var iface in namedTypeSymbol.Interfaces)
            {
                WriteInterfaceWrapper(c, iface, namedTypeSymbol);
            }

            itw.Indent--;
            itw.WriteLine("};");

            if (namedTypeSymbol.TypeKind == TypeKind.Delegate)
            {
                itw.WriteLine();
                new CCodeDelegateWrapperClass(namedTypeSymbol).WriteTo(c);
                itw.WriteLine();
            }

            foreach (var namespaceNode in namedTypeSymbol.ContainingNamespace.EnumNamespaces())
            {
                itw.Indent--;
                itw.Write("}");
            }

            itw.WriteLine();

            if (namedTypeSymbol.IsPrimitiveValueType() || namedTypeSymbol.TypeKind == TypeKind.Enum || namedTypeSymbol.SpecialType == SpecialType.System_Void)
            {
                // value to class
                c.TextSpanNewLine("template<>");
                c.TextSpan("struct");
                c.WhiteSpace();
                c.TextSpan("valuetype_to_class<");
                c.WriteType(namedTypeSymbol);
                c.TextSpan(">");
                c.WhiteSpace();
                c.TextSpan("{ typedef");
                c.WhiteSpace();
                c.WriteType(namedTypeSymbol, true, false, true);
                c.WhiteSpace();
                c.TextSpan("type; };");

                // class to value
                c.TextSpanNewLine("template<>");
                c.TextSpan("struct");
                c.WhiteSpace();
                c.TextSpan("class_to_valuetype<");
                c.WriteType(namedTypeSymbol, true, false, true);
                c.TextSpan(">");
                c.WhiteSpace();
                c.TextSpan("{ typedef");
                c.WhiteSpace();
                c.WriteType(namedTypeSymbol);
                c.WhiteSpace();
                c.TextSpan("type; };");

                itw.WriteLine();
                if (namedTypeSymbol.SpecialType != SpecialType.System_Void)
                {
                    new CCodeBoxForPrimitiveValuesOrEnumsDeclaration(namedTypeSymbol).WriteTo(c);
                    itw.WriteLine();
                }
            }
        }