using System.Collections.Generic;
using CppSharp.AST;
using CppSharp.Generators.C;
using CppSharp.Passes;
namespace CppSharp.Generators.Cpp
{
///
/// C++ generator responsible for driving the generation of source and
/// header files.
///
public class CppGenerator : CGenerator
{
private readonly CppTypePrinter typePrinter;
public CppGenerator(BindingContext context) : base(context)
{
typePrinter = new CppTypePrinter(Context);
}
public override List Generate(IEnumerable units)
{
var outputs = new List();
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();
}
}
///
/// 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.
///
public class FixupPureMethodsPass : TranslationUnitPass
{
public override bool VisitMethodDecl(Method method)
{
method.IsPure = false;
return base.VisitMethodDecl(method);
}
}
}