diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerationContext.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerationContext.cs new file mode 100644 index 000000000..60811553f --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerationContext.cs @@ -0,0 +1,10 @@ +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolGenerationContext : RegistrableGeneratorContext + { + public LuaSolGenerationContext() + : base() + { + } + } +} diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerator.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerator.cs new file mode 100644 index 000000000..ddb2faeeb --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGenerator.cs @@ -0,0 +1,30 @@ +using CppSharp.AST; +using System.Collections.Generic; + +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolGenerator : RegistrableGenerator + { + public const string Id = "Lua::Sol"; + public static readonly GeneratorKind Kind = new(Id, "lua::sol", typeof(LuaSolGenerator), typeof(LuaSolTypePrinter), new[] { "lua::sol" }); + + public LuaSolGenerator(BindingContext context) : base(context) + { + } + + protected override LuaSolGeneratorOptions CreateOptions() + { + return new LuaSolGeneratorOptions(this); + } + + protected override LuaSolHeaders CreateHeader(IEnumerable units) + { + return new LuaSolHeaders(this, units); + } + + protected override LuaSolSources CreateSource(IEnumerable units) + { + return new LuaSolSources(this, units); + } + } +} diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGeneratorOptions.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGeneratorOptions.cs new file mode 100644 index 000000000..32531c212 --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolGeneratorOptions.cs @@ -0,0 +1,24 @@ +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolGeneratorOptions : TRegistrableGeneratorOptions + { + public LuaSolNamingStrategy NamingStrategy; + + public LuaSolGeneratorOptions(LuaSolGenerator generator) : base(generator) + { + NamingStrategy = new LuaSolNamingStrategy(generator); + } + + public override string DefaultRootContextType => "::sol::state_view&"; + + public override string DefaultRootContextName => "state"; + + public override string DefaultTemplateContextDefaultType => "::sol::table"; + + public override string DefaultTemplateContextDefaultValue => "::sol::nil"; + + public override string DefaultCmakeVariableHeader => "LUA_SOL_BINDINGS_HEADER"; + + public override string DefaultCmakeVariableSource => "LUA_SOL_BINDINGS_SOURCE"; + } +} diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolHeaders.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolHeaders.cs new file mode 100644 index 000000000..acc9fe919 --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolHeaders.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using CppSharp.AST; + +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolHeaders : LuaSolSources + { + public LuaSolHeaders(LuaSolGenerator generator, IEnumerable units) + : base(generator, units) + { + } + + public override string FileExtension => "h"; + + protected override bool TemplateAllowed { get { return true; } } + + public override void Process() + { + GenerateFilePreamble(CommentKind.BCPL); + + PushBlock(); + WriteLine("#pragma once"); + PopBlock(NewLineKind.BeforeNextBlock); + + TranslationUnit.Visit(this); + } + + #region TranslationUnit + + public virtual void GenerateTranslationUnitRegistrationFunctionDeclaration(TranslationUnit translationUnit) + { + NewLine(); + GenerateTranslationUnitRegistrationFunctionSignature(translationUnit); + WriteLine(";"); + NewLine(); + } + + public override void GenerateTranslationUnit(TranslationUnit translationUnit) + { + GenerateTranslationUnitNamespaceBegin(translationUnit); + GenerateTranslationUnitRegistrationFunctionBody(translationUnit); + GenerateTranslationUnitRegistrationFunctionDeclaration(translationUnit); + GenerateTranslationUnitNamespaceEnd(translationUnit); + } + + public override bool CanGenerateTranslationUnit(TranslationUnit unit) + { + if (AlreadyVisited(unit)) + { + return false; + } + return true; + } + + public override bool VisitTranslationUnit(TranslationUnit unit) + { + if (!CanGenerateTranslationUnit(unit)) + { + return false; + } + + GenerateTranslationUnit(unit); + + return true; + } + + #endregion + + public virtual void GenerateMain() + { + VisitNamespace(TranslationUnit); + } + + public virtual void GenerateIncludes() + { + if (Generator.GeneratorOptions.BaseInclude != null) + { + WriteLineIndent(Generator.GeneratorOptions.BaseInclude.ToString()); + } + } + } +} diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolNamingStrategy.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolNamingStrategy.cs new file mode 100644 index 000000000..2f26269ba --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolNamingStrategy.cs @@ -0,0 +1,10 @@ +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolNamingStrategy : RegistrableNamingStrategy + { + public LuaSolNamingStrategy(LuaSolGenerator generator) + : base(generator) + { + } + } +} diff --git a/src/Generator/Generators/Registrable/Lua/Sol/LuaSolSources.cs b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolSources.cs new file mode 100644 index 000000000..f1a15576b --- /dev/null +++ b/src/Generator/Generators/Registrable/Lua/Sol/LuaSolSources.cs @@ -0,0 +1,2068 @@ +using CppSharp.AST; +using CppSharp.Generators.C; +using System.Collections.Generic; +using System.Linq; + +namespace CppSharp.Generators.Registrable.Lua.Sol +{ + public class LuaSolSources : RegistrableSources + { + protected LuaSolGenerationContext GenerationContext { get; } + protected LuaSolNamingStrategy NamingStrategy => Generator.GeneratorOptions.NamingStrategy; + + public LuaSolSources(LuaSolGenerator generator, IEnumerable units) + : base(generator, units) + { + GenerationContext = new LuaSolGenerationContext(); + } + + public override string FileExtension { get { return "cpp"; } } + + protected virtual bool TemplateAllowed { get { return false; } } + + protected bool NonTemplateAllowed { get { return !TemplateAllowed || GenerationContext.PeekTemplateLevel() != 0; } } + + public override void Process() + { + GenerateFilePreamble(CommentKind.BCPL); + + PushBlock(BlockKind.Includes); + var file = Context.Options.GetIncludePath(TranslationUnit); + WriteLine($"#include \"{file}\""); + + NewLine(); + PopBlock(); + + TranslationUnit.Visit(this); + + PushBlock(BlockKind.Footer); + PopBlock(); + } + + public virtual void GenerateDeclarationGlobalStateRegistration(Declaration declaration) + { + if (declaration.Access != AccessSpecifier.Protected) + { + if (declaration.OriginalNamespace is not Class) + { + Write(NamingStrategy.GetBindingContext(declaration, GenerationContext)); + } + else + { + Write($"{NamingStrategy.GetRootContextName(GenerationContext)}[{NamingStrategy.GetBindingIdValue(declaration.Namespace, GenerationContext)}]"); + } + Write($"[{NamingStrategy.GetRegistrationNameQuoted(declaration)}] = "); + Write($"{NamingStrategy.GetRootContextName(GenerationContext)}[{NamingStrategy.GetBindingIdName(declaration)}];"); + NewLine(); + } + } + + public virtual void GenerateDeclarationContainerList(DeclarationContext declaration) + { + List containerList = declaration.Declarations.Where(declaration => + { + if (declaration is Namespace || declaration is Enumeration) + { + return true; + } + else if (declaration is Class) + { + return Utils.FindDescribedTemplate(declaration) == null; + } + return false; + }).ToList(); + containerList.Sort((x, y) => x.LineNumberStart.CompareTo(y.LineNumberStart)); + foreach (var item in containerList) + { + item.Visit(this); + }; + + if (NonTemplateAllowed) + { + List classTemplateList = declaration.Templates.Where(template => template is ClassTemplate).Cast().ToList(); + classTemplateList.Sort((x, y) => x.LineNumberStart.CompareTo(y.LineNumberStart)); + foreach (var classTemplate in classTemplateList) + { + if (Utils.IsDefaultTemplateParameterList(classTemplate.Parameters)) + { + Write(string.Format("{0}<>{{}}({1}", + NamingStrategy.GetClassTemplateName(classTemplate), + NamingStrategy.GetRootContextName(GenerationContext) + )); + if (classTemplate.OriginalName != classTemplate.Name) + { + Write(", "); + Write(NamingStrategy.GetRootContextName(GenerationContext)); + Write(", "); + Write(classTemplate.Name); + } + WriteLine(");"); + } + + foreach (var classTemplateSpecialization in classTemplate.Specializations) + { + if (classTemplateSpecialization is not ClassTemplatePartialSpecialization) + { + if (classTemplateSpecialization.SpecializationKind == TemplateSpecializationKind.ExplicitSpecialization) + { + Write(string.Format("{0}<{1}>{{}}({2}", + NamingStrategy.GetClassTemplateName(classTemplateSpecialization), + NamingStrategy.PrintClassTemplateSpecializationArguments(classTemplateSpecialization.Arguments, false), + NamingStrategy.GetRootContextName(GenerationContext) + )); + if (classTemplateSpecialization.OriginalName != classTemplateSpecialization.Name) + { + Write(", "); + Write(NamingStrategy.GetRootContextName(GenerationContext)); + Write(", "); + Write(classTemplateSpecialization.Name); + } + WriteLine(");"); + } + } + } + }; + } + } + + public virtual void GenerateDeclarationTemplateList(DeclarationContext declaration) + { + if (!TemplateAllowed) + { + return; + } + + List containerList = declaration.Declarations.Where(declaration => + { + if (declaration is Namespace || declaration is Enumeration) + { + return true; + } + else if (declaration is Class) + { + return Utils.FindDescribedTemplate(declaration) == null; + } + return false; + }).ToList(); + containerList.Sort((x, y) => x.LineNumberStart.CompareTo(y.LineNumberStart)); + foreach (var item in containerList) + { + if (item.Access == AccessSpecifier.Protected) + { + item.Visit(this); + } + else + { + GenerateDeclarationTemplateList((DeclarationContext)item); + } + }; + + List classTemplateList = declaration.Templates.Where(template => template is ClassTemplate).Cast().ToList(); + classTemplateList.Sort((x, y) => x.LineNumberStart.CompareTo(y.LineNumberStart)); + foreach (var classTemplate in classTemplateList) + { + classTemplate.Visit(this); + foreach (var classTemplateSpecialization in classTemplate.Specializations) + { + classTemplateSpecialization.Visit(this); + } + }; + + //List