Tools and libraries to glue C/C++ APIs to high-level languages
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.
 
 
 
 
 

70 lines
2.1 KiB

using System.Collections.Generic;
using CppSharp.AST;
using CppSharp.Generators.C;
using CppSharp.Passes;
namespace CppSharp.Generators.Cpp
{
/// <summary>
/// C++ generator responsible for driving the generation of source and
/// header files.
/// </summary>
public class CppGenerator : CGenerator
{
private readonly CppTypePrinter typePrinter;
public CppGenerator(BindingContext context) : base(context)
{
typePrinter = new CppTypePrinter(Context);
}
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
{
var outputs = new List<CodeGenerator>();
var header = new CppHeaders(Context, units);
outputs.Add(header);
var source = new CppSources(Context, units);
outputs.Add(source);
return outputs;
}
public override bool SetupPasses()
{
new FixupPureMethodsPass().VisitASTContext(Context.ASTContext);
return true;
}
public static bool ShouldGenerateClassNativeInstanceField(Class @class)
{
if (@class.IsStatic)
return false;
return @class.IsRefType && (!@class.HasBase || !@class.HasRefBase());
}
protected override string TypePrinterDelegate(Type type)
{
return type.Visit(typePrinter).ToString();
}
}
/// <summary>
/// Removes the pureness of virtual abstract methods in C++ classes since
/// the generated classes cannot have virtual pure methods, as they call
/// the original pure method.
/// This lets user code mark some methods as pure if needed, in that case
/// the generator can generate the necessary pure C++ code annotations safely
/// knowing the only pure functions were user-specified.
/// </summary>
public class FixupPureMethodsPass : TranslationUnitPass
{
public override bool VisitMethodDecl(Method method)
{
method.IsPure = false;
return base.VisitMethodDecl(method);
}
}
}