Skip to content

Commit 94da6be

Browse files
committed
Registrable: add support for function
1 parent d87d95a commit 94da6be

File tree

1 file changed

+135
-3
lines changed

1 file changed

+135
-3
lines changed

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

Lines changed: 135 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public virtual void GenerateTranslationUnitRegistrationFunctionBody(TranslationU
111111
{
112112
variable.Visit(this);
113113
}
114+
115+
var methods = translationUnit.Functions.Where(method => !(method.IsOperator));
116+
var overloads = methods.GroupBy(m => m.Name);
117+
foreach (var overload in overloads)
118+
{
119+
GenerateFunctions(translationUnit, overload.ToList());
120+
}
114121
});
115122
}
116123

@@ -191,6 +198,13 @@ public virtual void GenerateNamespaceDeclarationList(Namespace @namespace, Detac
191198
{
192199
GenerateNamespaceFunctions(@namespace);
193200
GenerateNamespaceVariables(@namespace);
201+
202+
var methods = @namespace.Functions.Where(method => !(method.IsOperator));
203+
var overloads = methods.GroupBy(m => m.Name);
204+
foreach (var overload in overloads)
205+
{
206+
GenerateFunctions(@namespace, overload.ToList());
207+
}
194208
});
195209
}
196210

@@ -522,9 +536,11 @@ public virtual void GenerateClassDeclDeclarationList(Class @class, DetachmentOpt
522536
GenerateConstructors(@class, @class.Constructors);
523537

524538
var methods = @class.Methods.Where(method => !(method.IsConstructor || method.IsDestructor || method.IsOperator));
525-
var uniqueMethods = methods.GroupBy(m => m.Name);
526-
foreach (var group in uniqueMethods)
527-
GenerateMethods(@class, group.ToList());
539+
var overloads = methods.GroupBy(m => m.Name);
540+
foreach (var overload in overloads)
541+
{
542+
GenerateMethods(@class, overload.ToList());
543+
}
528544

529545
GenerateClassDeclFunctions(@class);
530546
GenerateClassDeclVariables(@class);
@@ -966,6 +982,122 @@ public virtual void GenerateConstructor(Class @class, Method constructor, bool d
966982

967983
#endregion
968984

985+
#region Function
986+
987+
public virtual bool NeedExpansionForFunctions(Declaration declaration, IEnumerable<Function> functions)
988+
{
989+
return false;
990+
}
991+
992+
public virtual void GenerateFunctions(Declaration declaration, IEnumerable<Function> functions)
993+
{
994+
var isDetach = GenerationContext.PeekIsDetach();
995+
996+
List<Function> filteredFunctions = functions.Where((function) => CanGenerateFunction(function)).ToList();
997+
if (filteredFunctions.Any())
998+
{
999+
Function function = filteredFunctions.First();
1000+
string functionName = function.Name;
1001+
string functionNameQuoted = $"\"{functionName}\"";
1002+
string functionBindingContext = NamingStrategy.GetBindingContext(function, GenerationContext);
1003+
string functionContextualName = NamingStrategy.GetContextualName(function, GenerationContext, FQNOption.IgnoreNone);
1004+
1005+
if (isDetach == DetachmentOption.Forced || isDetach == Utils.FindDetachmentOption(function))
1006+
{
1007+
1008+
if (isDetach != DetachmentOption.Off)
1009+
{
1010+
Write($"{functionBindingContext}[{functionNameQuoted}] = ");
1011+
}
1012+
else
1013+
{
1014+
WriteLine(",");
1015+
Write($"{functionNameQuoted}, ");
1016+
}
1017+
if (filteredFunctions.Count == 1)
1018+
{
1019+
GenerateFunction(declaration, filteredFunctions.First());
1020+
}
1021+
else
1022+
{
1023+
Write("::sol::overload(");
1024+
Indent();
1025+
for (int i = 0; i < filteredFunctions.Count; i++)
1026+
{
1027+
if (i > 0)
1028+
{
1029+
Write(",");
1030+
}
1031+
NewLine();
1032+
GenerateFunction(declaration, filteredFunctions[i]);
1033+
}
1034+
Unindent();
1035+
NewLine();
1036+
Write(")");
1037+
}
1038+
if (isDetach != DetachmentOption.Off)
1039+
{
1040+
WriteLine(";");
1041+
}
1042+
}
1043+
}
1044+
}
1045+
1046+
public virtual bool CanGenerateFunction(Function function)
1047+
{
1048+
if (AlreadyVisited(function))
1049+
{
1050+
return false;
1051+
}
1052+
else if (function.Access != AccessSpecifier.Public)
1053+
{
1054+
return false;
1055+
}
1056+
else if (!NonTemplateAllowed)
1057+
{
1058+
return false;
1059+
}
1060+
return function.IsGenerated;
1061+
}
1062+
1063+
public virtual void GenerateFunction(Declaration declaration, Function function)
1064+
{
1065+
{
1066+
Write("static_cast<");
1067+
Write(function.ReturnType.Visit(new CppTypePrinter(Context)));
1068+
Write("(");
1069+
Write("*)");
1070+
Write("(");
1071+
var needsComma = false;
1072+
foreach (var parameter in function.Parameters)
1073+
{
1074+
if (needsComma)
1075+
{
1076+
Write(", ");
1077+
}
1078+
else
1079+
{
1080+
needsComma = true;
1081+
}
1082+
Write(parameter.Type.Visit(new CppTypePrinter(Context)));
1083+
}
1084+
if (function.IsVariadic)
1085+
{
1086+
if (needsComma)
1087+
{
1088+
Write(", ");
1089+
}
1090+
Write("...");
1091+
}
1092+
Write(")");
1093+
Write(">(&");
1094+
Write(NamingStrategy.GetContextualName(function, GenerationContext, FQNOption.IgnoreNone));
1095+
Write(")");
1096+
}
1097+
}
1098+
1099+
#endregion
1100+
9691101
#region Method
9701102

9711103
public virtual bool NeedExpansionForMethods(Class @class, IEnumerable<Method> methods)

0 commit comments

Comments
 (0)