Skip to content

Commit 8309a7e

Browse files
committed
Registrable: add support for field/variable
1 parent 403968c commit 8309a7e

File tree

4 files changed

+248
-13
lines changed

4 files changed

+248
-13
lines changed

src/Generator/Generators/Registrable/Lua/Sol/LuaSolSources.cs

Lines changed: 237 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CppSharp.AST;
2+
using CppSharp.Generators.C;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -103,6 +104,14 @@ public virtual void GenerateTranslationUnitRegistrationFunctionBegin(Translation
103104
public virtual void GenerateTranslationUnitRegistrationFunctionBody(TranslationUnit translationUnit)
104105
{
105106
GenerateDeclarationContainerList(translationUnit);
107+
108+
GenerationContext.Scoped(RegistrableGeneratorContext.IsDetach, DetachmentOption.On, () =>
109+
{
110+
foreach (var variable in translationUnit.Variables)
111+
{
112+
variable.Visit(this);
113+
}
114+
});
106115
}
107116

108117
public virtual void GenerateTranslationUnitRegistrationFunctionEnd(TranslationUnit translationUnit)
@@ -175,19 +184,14 @@ public virtual void GenerateNamespaceBody(Namespace @namespace)
175184

176185
public virtual void GenerateNamespaceDeclarationList(Namespace @namespace, DetachmentOption detachment)
177186
{
178-
if (detachment == DetachmentOption.Off)
179-
{
180-
GenerateNamespaceFunctions(@namespace);
181-
GenerateNamespaceVariables(@namespace);
182-
}
183-
else
187+
GenerateNamespaceContainerList(@namespace);
188+
GenerateNamespaceTemplates(@namespace);
189+
GenerateNamespaceTypedefs(@namespace);
190+
GenerationContext.Scoped(RegistrableGeneratorContext.IsDetach, DetachmentOption.On, () =>
184191
{
185-
GenerateNamespaceContainerList(@namespace);
186-
GenerateNamespaceTemplates(@namespace);
187-
GenerateNamespaceTypedefs(@namespace);
188192
GenerateNamespaceFunctions(@namespace);
189193
GenerateNamespaceVariables(@namespace);
190-
}
194+
});
191195
}
192196

193197
public virtual void GenerateNamespaceContainerList(Namespace @namespace)
@@ -209,6 +213,10 @@ public virtual void GenerateNamespaceFunctions(Namespace @namespace)
209213

210214
public virtual void GenerateNamespaceVariables(Namespace @namespace)
211215
{
216+
foreach (var variable in @namespace.Variables)
217+
{
218+
variable.Visit(this);
219+
}
212220
}
213221

214222
public virtual void GenerateNamespaceEnd(Namespace @namespace)
@@ -543,6 +551,14 @@ public virtual void GenerateClassDeclFunctions(Class @class)
543551

544552
public virtual void GenerateClassDeclVariables(Class @class)
545553
{
554+
foreach (var field in @class.Fields)
555+
{
556+
field.Visit(this);
557+
}
558+
foreach (var variable in @class.Variables)
559+
{
560+
variable.Visit(this);
561+
}
546562
}
547563

548564
public virtual void GenerateClassDeclEnd(Class @class)
@@ -611,6 +627,217 @@ public override bool VisitClassDecl(Class @class)
611627

612628
#endregion
613629

630+
#region Field
631+
632+
#region Field
633+
634+
public virtual bool CanGenerateFieldDecl(Field field)
635+
{
636+
if (AlreadyVisited(field))
637+
{
638+
return false;
639+
}
640+
else if (field.Access != AccessSpecifier.Public)
641+
{
642+
return false;
643+
}
644+
else if (!NonTemplateAllowed)
645+
{
646+
return false;
647+
}
648+
return field.IsGenerated;
649+
}
650+
651+
public virtual bool GenerateFieldDecl(Field field)
652+
{
653+
var isDetach = GenerationContext.PeekIsDetach(DetachmentOption.Off);
654+
655+
if (isDetach == DetachmentOption.Forced || isDetach == Utils.FindDetachmentOption(field))
656+
{
657+
string fieldName = field.Name;
658+
string fieldNameQuoted = $"\"{fieldName}\"";
659+
string fieldContextualName = NamingStrategy.GetContextualName(field, GenerationContext, FQNOption.IgnoreNone);
660+
661+
if (isDetach != DetachmentOption.Off)
662+
{
663+
Write($"{NamingStrategy.GetBindingContext(field, GenerationContext)}[{fieldNameQuoted}] = ");
664+
}
665+
else
666+
{
667+
WriteLine(",");
668+
Write($"{fieldNameQuoted}, ");
669+
}
670+
// TODO : check for typemaps!!!
671+
{
672+
Write($"&{fieldContextualName}");
673+
}
674+
if (isDetach != DetachmentOption.Off)
675+
{
676+
WriteLine(";");
677+
}
678+
}
679+
680+
return true;
681+
}
682+
683+
#endregion
684+
685+
#region Bitfield
686+
687+
public virtual bool CanGenerateFieldDeclBitfield(Field field)
688+
{
689+
if (AlreadyVisited(field))
690+
{
691+
return false;
692+
}
693+
else if (field.Access != AccessSpecifier.Public)
694+
{
695+
return false;
696+
}
697+
else if (!NonTemplateAllowed)
698+
{
699+
return false;
700+
}
701+
return field.IsGenerated;
702+
}
703+
704+
public virtual bool GenerateFieldDeclBitfield(Field field)
705+
{
706+
var isDetach = GenerationContext.PeekIsDetach(DetachmentOption.Off);
707+
708+
if (isDetach == DetachmentOption.Forced || isDetach == Utils.FindDetachmentOption(field))
709+
{
710+
string bitfieldOriginalName = field.OriginalName;
711+
string bitfieldName = field.Name;
712+
string bitfieldNameQuoted = $"\"{bitfieldName}\"";
713+
string bitfieldCppContext = NamingStrategy.GetCppContext(field, GenerationContext, FQNOption.IgnoreNone);
714+
string bitfieldType = field.Type.Visit(new CppTypePrinter(Context));
715+
716+
if (isDetach != DetachmentOption.Off)
717+
{
718+
Write($"{NamingStrategy.GetBindingContext(field, GenerationContext)}[{bitfieldNameQuoted}] = ");
719+
}
720+
else
721+
{
722+
WriteLine(",");
723+
Write($"{bitfieldNameQuoted}, ");
724+
}
725+
WriteLine("::sol::property(");
726+
Indent();
727+
WriteLine($"[]({bitfieldCppContext}& self) {{");
728+
Indent();
729+
WriteLine($"return self.{bitfieldOriginalName};");
730+
Unindent();
731+
WriteLine("}, ");
732+
WriteLine($"[]({bitfieldCppContext}& self, {bitfieldType} value) {{");
733+
Indent();
734+
WriteLine($"self.{bitfieldOriginalName} = value;");
735+
Unindent();
736+
WriteLine("}");
737+
Unindent();
738+
Write(")");
739+
if (isDetach != DetachmentOption.Off)
740+
{
741+
WriteLine(";");
742+
}
743+
}
744+
745+
return true;
746+
}
747+
748+
#endregion
749+
750+
public override bool VisitFieldDecl(Field field)
751+
{
752+
if (field.IsBitField)
753+
{
754+
if (!CanGenerateFieldDeclBitfield(field))
755+
{
756+
return false;
757+
}
758+
759+
return GenerateFieldDeclBitfield(field);
760+
}
761+
else
762+
{
763+
if (!CanGenerateFieldDecl(field))
764+
{
765+
return false;
766+
}
767+
768+
return GenerateFieldDecl(field);
769+
}
770+
return false;
771+
}
772+
773+
#endregion
774+
775+
#region Variable
776+
777+
public virtual bool CanGenerateVariableDecl(Variable variable)
778+
{
779+
if (AlreadyVisited(variable))
780+
{
781+
return false;
782+
}
783+
else if (variable.Access != AccessSpecifier.Public)
784+
{
785+
return false;
786+
}
787+
else if (!NonTemplateAllowed)
788+
{
789+
return false;
790+
}
791+
return variable.IsGenerated;
792+
}
793+
794+
public virtual bool GenerateVariableDecl(Variable variable)
795+
{
796+
var isDetach = GenerationContext.PeekIsDetach(DetachmentOption.Off);
797+
798+
if (isDetach == DetachmentOption.Forced || isDetach == Utils.FindDetachmentOption(variable))
799+
{
800+
string variableName = variable.Name;
801+
string variableNameQuoted = $"\"{variableName}\"";
802+
string variableBindingContext = NamingStrategy.GetBindingContext(variable, GenerationContext);
803+
string variableContextualName = NamingStrategy.GetContextualName(variable, GenerationContext, FQNOption.IgnoreNone);
804+
// TODO: Bug in sol until it gets resolved: we can only bind static class variable by reference.
805+
if (variable.OriginalNamespace is Class)
806+
{
807+
variableContextualName = $"::std::ref({variableContextualName})";
808+
}
809+
810+
// TODO: check for typemaps!!!
811+
if (isDetach != DetachmentOption.Off)
812+
{
813+
Write($"{variableBindingContext}[{variableNameQuoted}] = ::sol::var({variableContextualName});");
814+
}
815+
else
816+
{
817+
WriteLine(",");
818+
Write($"{variableNameQuoted}, ::sol::var({variableContextualName})");
819+
}
820+
if (isDetach != DetachmentOption.Off)
821+
{
822+
WriteLine(";");
823+
}
824+
}
825+
826+
return true;
827+
}
828+
829+
public override bool VisitVariableDecl(Variable variable)
830+
{
831+
if (!CanGenerateVariableDecl(variable))
832+
{
833+
return false;
834+
}
835+
836+
return GenerateVariableDecl(variable);
837+
}
838+
839+
#endregion
840+
614841
public virtual bool CanGenerateConstructor(Method method)
615842
{
616843
// if not self:isNonTemplateAllowed(context) then

src/Generator/Generators/Registrable/RegistrableNamingStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public string GetCppContext(Declaration entity, RegistrableGeneratorContext cont
396396
return cppContext.GetFullQualifiedName(option);
397397
}
398398
}
399-
return "";// GetFullyQualifiedName(entity.OriginalNamespace, option, context);
399+
return GetFullyQualifiedName(entity.OriginalNamespace, option);
400400
}
401401
}
402402
}

src/Generator/Generators/Registrable/Utils/InfoMapStack.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public T1 Peek<T1>(InfoEntry infoEntry, T1 defaultValue = default) where T1 : T
1414
{
1515
if (TryGetValue(infoEntry, out Stack<T> stack))
1616
{
17-
return (T1)stack.Peek();
17+
if (stack.Count > 0)
18+
{
19+
return (T1)stack.Peek();
20+
}
1821
}
1922
return defaultValue;
2023
}
@@ -61,7 +64,7 @@ public bool TryPop<T1>(InfoEntry infoEntry, [MaybeNullWhen(false)] out T1 result
6164
return false;
6265
}
6366

64-
public void Scoped<T1>(InfoEntry infoEntry, T1 item, Action action) where T1 : T
67+
public void Scoped(InfoEntry infoEntry, T item, Action action)
6568
{
6669
Push(infoEntry, item);
6770
action();

src/Generator/Generators/Registrable/Utils/Utils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ public static Declaration FindDescribedTemplate(Declaration declaration)
1515
}
1616
return null;
1717
}
18+
19+
public static DetachmentOption FindDetachmentOption(Declaration declaration)
20+
{
21+
return (declaration.Namespace is Class) ? DetachmentOption.Off : DetachmentOption.On;
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)