mirror of https://github.com/mono/CppSharp.git
c-sharpdotnetmonobindingsbridgecclangcpluspluscppsharpglueinteropparserparsingpinvokeswigsyntax-treevisitorsxamarinxamarin-bindings
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.9 KiB
62 lines
1.9 KiB
using System.Linq; |
|
using CppSharp.AST; |
|
|
|
namespace CppSharp.Passes |
|
{ |
|
/// <summary> |
|
/// Moves a function to a class, if any, named after the function's header. |
|
/// </summary> |
|
public class MoveFunctionToClassPass : TranslationUnitPass |
|
{ |
|
public override bool VisitFunctionDecl(Function function) |
|
{ |
|
if (!VisitDeclaration(function)) |
|
return false; |
|
|
|
if (function.Ignore || function.Namespace is Class) |
|
return false; |
|
|
|
var @class = FindClassToMoveFunctionTo(function.Namespace); |
|
if (@class != null) |
|
MoveFunction(function, @class); |
|
|
|
return true; |
|
} |
|
|
|
private Class FindClassToMoveFunctionTo(INamedDecl @namespace) |
|
{ |
|
var unit = @namespace as TranslationUnit; |
|
if (unit == null) |
|
{ |
|
return Driver.ASTContext.FindClass( |
|
@namespace.Name, ignoreCase: true).FirstOrDefault(); |
|
} |
|
return Driver.ASTContext.FindCompleteClass( |
|
unit.FileNameWithoutExtension.ToLowerInvariant(), true); |
|
} |
|
|
|
private static void MoveFunction(Function function, Class @class) |
|
{ |
|
var method = new Method(function) |
|
{ |
|
Namespace = @class, |
|
IsStatic = true |
|
}; |
|
|
|
if (method.OperatorKind != CXXOperatorKind.None) |
|
{ |
|
var param = function.Parameters[0]; |
|
Class type; |
|
if (!FunctionToInstanceMethodPass.GetClassParameter(param, out type)) |
|
return; |
|
method.Kind = CXXMethodKind.Operator; |
|
method.SynthKind = FunctionSynthKind.NonMemberOperator; |
|
method.OriginalFunction = null; |
|
} |
|
|
|
function.ExplicityIgnored = true; |
|
|
|
@class.Methods.Add(method); |
|
} |
|
} |
|
}
|
|
|