Exemple #1
0
        public static void WriteElements(TypeDecl decl, CodeWriter writer)
        {
            foreach (var declare in decl.Declare.Handlers)
            {
                var @return  = "void";
                var @params  = string.Join(", ", declare.Params.Select(param => $"{GetType(param)} {param.Name.Underscore()}"));
                var function = $"{@return} {declare.Name.Underscore()}({@params})";

                var comment = CommentHelper.FormatComment(declare.Comment);
                writer.AppendLines(comment);

                var implement = decl.Implement?.Handlers.FirstOrDefault(i => i.Name == declare.Name);
                // Abstract
                if (implement == null)
                {
                    writer.AppendLine($"virtual {function} = 0;");
                }
                // Override
                else if (implement.Override == YesNo.Y)
                {
                    writer.AppendLine($"virtual {function} override;");
                }
                // No modifiers.
                else
                {
                    writer.AppendLine($"{function};");
                }

                writer.AppendNewLineWithoutIndent();
            }
        }
        public static void WriteElements(List <ElementDecl> elements, CodeWriter writer)
        {
            foreach (var element in elements)
            {
                var comment = CommentHelper.FormatComment(element.Comment);
                writer.AppendLines(comment);

                var type = GetType(element);
                writer.AppendLine($"{type} {element.Name.Underscore()};");
                writer.AppendNewLineWithoutIndent();
            }
        }
Exemple #3
0
        private static void BuildEnumDeclaration(EnumDecl decl, CodeWriter writer)
        {
            var settings = GeneratorSettingsProvider.Get(decl.Module.ModuleName);
            var type     = decl.Name.Underscore();

            writer.AppendNewLineWithoutIndent();

            writer.AppendLines(CommentHelper.FormatComment(decl.Comment));

            writer.AppendLine($"class {settings.DeclSpec} {type} : public dot::enum_base");
            writer.AppendLine("{");

            writer.PushIndent();
            writer.AppendLine($"typedef {type} self;");
            writer.PopIndent();
            writer.AppendNewLineWithoutIndent();

            var elements = decl.Items;

            writer.AppendLine("public:");
            writer.AppendNewLineWithoutIndent();
            writer.PushIndent();

            writer.AppendLine("enum enum_type {");
            writer.PushIndent();
            foreach (EnumItem item in elements)
            {
                writer.AppendLines(CommentHelper.FormatComment(item.Comment));
                writer.AppendLine($"{item.Name.Underscore()},");
                // Do not add new line after last item
                if (elements.IndexOf(item) != elements.Count - 1)
                {
                    writer.AppendNewLineWithoutIndent();
                }
            }

            writer.PopIndent();
            writer.AppendLine("};");
            writer.AppendNewLineWithoutIndent();
            writer.PopIndent();

            writer.AppendLines(@"private:
    static dot::object make_self() { return self(); }

public:
    typedef self element_type;
    typedef dot::struct_wrapper_impl<self>* pointer_type;
    using dot::enum_base::enum_base;

    operator dot::object();
    operator int() const;
    self& operator=(int rhs);
    self& operator=(const self& other);
    virtual dot::type_t type();
    static dot::type_t typeof();

protected:
    virtual dot::dictionary<dot::string, int> get_enum_map() override;");

            writer.AppendLine("};");
        }
        private static void BuildClassDeclaration(TypeDecl decl, CodeWriter writer)
        {
            var  settings  = GeneratorSettingsProvider.Get(decl.Module.ModuleName);
            var  type      = decl.Name.Underscore();
            bool isRecord  = decl.Keys.Any();
            bool isDerived = decl.Inherit != null;

            // Self-forward
            writer.AppendLine($"class {type}_data_impl; using {type}_data = dot::ptr<{type}_data_impl>;");
            if (isRecord)
            {
                writer.AppendLine($"class {type}_key_impl; using {type}_key = dot::ptr<{type}_key_impl>;");
            }

            // Get unique keys and data from elements
            var dataForwards = decl.Elements.Where(e => e.Data != null)
                               .Where(e => e.Data.Module.ModuleName == decl.Module.ModuleName)
                               .Select(e => $"{e.Data.Name.Underscore()}_data").ToList();

            var keysForwards = decl.Elements.Where(e => e.Key != null)
                               .Where(e => e.Key.Module.ModuleName == decl.Module.ModuleName)
                               .Select(e => $"{e.Key.Name.Underscore()}_key").ToList();

            var forwards = keysForwards.Union(dataForwards).Distinct();

            // Appends forwards
            foreach (var f in forwards)
            {
                writer.AppendLine($"class {f}_impl; using {f} = dot::ptr<{f}_impl>;");
            }
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"inline {type}_data make_{type}_data();");
            writer.AppendNewLineWithoutIndent();

            writer.AppendLines(CommentHelper.FormatComment(decl.Comment));

            var baseType = isRecord ? $"record_impl<{type}_key_impl, {type}_data_impl>" :
                           isDerived    ? $"{decl.Inherit.Name.Underscore()}_data_impl" :
                           "data_impl";

            writer.AppendLine($"class {settings.DeclSpec} {type}_data_impl : public {baseType}");
            writer.AppendLine("{");

            writer.PushIndent();
            writer.AppendLine($"typedef {type}_data_impl self;");
            writer.AppendLine($"friend {type}_data make_{type}_data();");
            writer.PopIndent();
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine("public: // FIELDS");
            writer.AppendNewLineWithoutIndent();

            var elements = decl.Elements;

            if (elements.Any())
            {
                writer.PushIndent();
                CppElementBuilder.WriteElements(decl.Elements, writer);
                writer.PopIndent();
            }

            if (decl.Declare != null)
            {
                writer.AppendLine("public: // METHODS");
                writer.AppendNewLineWithoutIndent();
                writer.PushIndent();
                CppMethodBuilder.WriteElements(decl, writer);
                writer.PopIndent();
            }

            writer.AppendLine("public:");
            writer.PushIndent();
            writer.AppendLine("virtual dot::type_t type();");
            writer.AppendLine("static dot::type_t typeof();");
            writer.PopIndent();

            writer.AppendLine("};");
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine("/// Create an empty instance.");
            writer.AppendLine($"inline {type}_data make_{type}_data() {{ return new {type}_data_impl; }}");
        }