Browse Source

Introduce a scope kind stack for type printers.

Just like we already have for other context data.
pull/1581/head
Joao Matos 4 years ago committed by João Matos
parent
commit
eed362841c
  1. 20
      src/CppParser/Bootstrap/Bootstrap.cs
  2. 19
      src/Generator.Tests/AST/TestAST.cs
  3. 14
      src/Generator/Generators/C/CppSources.cs
  4. 3
      src/Generator/Generators/C/CppTypePrinter.cs
  5. 3
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  6. 4
      src/Generator/Generators/NAPI/NAPISources.cs
  7. 21
      src/Generator/Generators/TypePrinter.cs
  8. 2
      src/Generator/Passes/SymbolsCodeGenerator.cs
  9. 3
      src/Generator/Types/TypeMapDatabase.cs

20
src/CppParser/Bootstrap/Bootstrap.cs

@ -109,8 +109,8 @@ namespace CppSharp @@ -109,8 +109,8 @@ namespace CppSharp
exprCxxUnit.Visit(exprSubclassVisitor);
ExprClasses = exprSubclassVisitor.Classes;
CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context)
{ ScopeKind = TypePrintScopeKind.Local };
CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context);
CodeGeneratorHelpers.CppTypePrinter.PushScope(TypePrintScopeKind.Local);
GenerateStmt(driver.Context);
GenerateExpr(driver.Context);
@ -501,7 +501,7 @@ namespace CppSharp @@ -501,7 +501,7 @@ namespace CppSharp
: base(context)
{
Declarations = declarations;
TypePrinter.ScopeKind = TypePrintScopeKind.Local;
TypePrinter.PushScope(TypePrintScopeKind.Local);
TypePrinter.PrintModuleOutputNamespace = false;
}
@ -1438,7 +1438,7 @@ namespace CppSharp @@ -1438,7 +1438,7 @@ namespace CppSharp
public override void Process()
{
Context.Options.GeneratorKind = GeneratorKind.CPlusPlus;
CTypePrinter.ScopeKind = TypePrintScopeKind.Local;
CTypePrinter.PushScope(TypePrintScopeKind.Local);
GenerateFilePreamble(CommentKind.BCPL);
NewLine();
@ -1755,12 +1755,9 @@ namespace CppSharp @@ -1755,12 +1755,9 @@ namespace CppSharp
public static string GetQualifiedName(Declaration decl,
TypePrinter typePrinter)
{
var scopeKind = typePrinter.ScopeKind;
typePrinter.ScopeKind = TypePrintScopeKind.Qualified;
typePrinter.PushScope(TypePrintScopeKind.Qualified);
var qualifiedName = decl.Visit(typePrinter).Type;
typePrinter.ScopeKind = scopeKind;
typePrinter.PopScope();
qualifiedName = CleanClangNamespaceFromName(qualifiedName);
@ -1905,10 +1902,9 @@ namespace CppSharp @@ -1905,10 +1902,9 @@ namespace CppSharp
if (iteratorType.IsPointer())
iteratorType = iteratorType.GetFinalPointee();
var scopeKind = typePrinter.ScopeKind;
typePrinter.ScopeKind = TypePrintScopeKind.Qualified;
typePrinter.PushScope(TypePrintScopeKind.Qualified);
var iteratorTypeName = iteratorType.Visit(typePrinter).Type;
typePrinter.ScopeKind = scopeKind;
typePrinter.PopScope();
iteratorTypeName = CleanClangNamespaceFromName(iteratorTypeName);

19
src/Generator.Tests/AST/TestAST.cs

@ -422,11 +422,8 @@ namespace CppSharp.Generator.Tests.AST @@ -422,11 +422,8 @@ namespace CppSharp.Generator.Tests.AST
[Test]
public void TestPrintingConstPointerWithConstType()
{
var cppTypePrinter = new CppTypePrinter(Context)
{
ScopeKind = TypePrintScopeKind.Qualified,
ResolveTypeMaps = false
};
var cppTypePrinter = new CppTypePrinter(Context) { ResolveTypeMaps = false };
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
var builtin = new BuiltinType(PrimitiveType.Char);
var pointee = new QualifiedType(builtin, new TypeQualifiers { IsConst = true });
var pointer = new QualifiedType(new PointerType(pointee), new TypeQualifiers { IsConst = true });
@ -438,7 +435,8 @@ namespace CppSharp.Generator.Tests.AST @@ -438,7 +435,8 @@ namespace CppSharp.Generator.Tests.AST
public void TestPrintingSpecializationWithConstValue()
{
var template = AstContext.FindDecl<ClassTemplate>("TestSpecializationArguments").First();
var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified };
var cppTypePrinter = new CppTypePrinter(Context);
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
Assert.That(template.Specializations.Last().Visit(cppTypePrinter).Type,
Is.EqualTo("TestSpecializationArguments<const TestASTEnumItemByName>"));
}
@ -490,7 +488,8 @@ namespace CppSharp.Generator.Tests.AST @@ -490,7 +488,8 @@ namespace CppSharp.Generator.Tests.AST
[Test]
public void TestVolatile()
{
var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified };
var cppTypePrinter = new CppTypePrinter(Context);
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
var builtin = new BuiltinType(PrimitiveType.Char);
var pointee = new QualifiedType(builtin, new TypeQualifiers { IsConst = true, IsVolatile = true });
var type = pointee.Visit(cppTypePrinter).Type;
@ -508,7 +507,8 @@ namespace CppSharp.Generator.Tests.AST @@ -508,7 +507,8 @@ namespace CppSharp.Generator.Tests.AST
public void TestPrintNestedInSpecialization()
{
var template = AstContext.FindDecl<ClassTemplate>("TestTemplateClass").First();
var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified };
var cppTypePrinter = new CppTypePrinter(Context);
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
Assert.That(template.Specializations[4].Classes.First().Visit(cppTypePrinter).Type,
Is.EqualTo("TestTemplateClass<Math::Complex>::NestedInTemplate"));
}
@ -517,7 +517,8 @@ namespace CppSharp.Generator.Tests.AST @@ -517,7 +517,8 @@ namespace CppSharp.Generator.Tests.AST
public void TestPrintQualifiedSpecialization()
{
var functionWithSpecializationArg = AstContext.FindFunction("functionWithSpecializationArg").First();
var cppTypePrinter = new CppTypePrinter(Context) { ScopeKind = TypePrintScopeKind.Qualified };
var cppTypePrinter = new CppTypePrinter(Context);
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
Assert.That(functionWithSpecializationArg.Parameters[0].Visit(cppTypePrinter).Type,
Is.EqualTo("const TestTemplateClass<int>"));
}

14
src/Generator/Generators/C/CppSources.cs

@ -353,14 +353,20 @@ namespace CppSharp.Generators.Cpp @@ -353,14 +353,20 @@ namespace CppSharp.Generators.Cpp
public override string GetMethodIdentifier(Function function,
TypePrinterContextKind context = TypePrinterContextKind.Managed)
{
var method = function as Method;
if (method != null)
string id;
if (function is Method method)
{
var @class = method.Namespace as Class;
return $"{QualifiedIdentifier(@class)}::{base.GetMethodIdentifier(method, context)}";
CTypePrinter.PushScope(TypePrintScopeKind.Qualified);
id = $"{QualifiedIdentifier(@class)}::{base.GetMethodIdentifier(method, context)}";
CTypePrinter.PopScope();
return id;
}
return base.GetMethodIdentifier(function);
CTypePrinter.PushScope(TypePrintScopeKind.Qualified);
id = base.GetMethodIdentifier(function);
CTypePrinter.PopScope();
return id;
}
public override bool VisitMethodDecl(Method method)

3
src/Generator/Generators/C/CppTypePrinter.cs

@ -27,7 +27,6 @@ namespace CppSharp.Generators.C @@ -27,7 +27,6 @@ namespace CppSharp.Generators.C
{
Context = context;
PrintFlavorKind = CppTypePrintFlavorKind.Cpp;
ScopeKind = TypePrintScopeKind.GlobalQualified;
PrintTypeQualifiers = true;
PrintTypeModifiers = true;
}
@ -60,12 +59,12 @@ namespace CppSharp.Generators.C @@ -60,12 +59,12 @@ namespace CppSharp.Generators.C
var typePrinter = new CppTypePrinter(Context)
{
PrintFlavorKind = PrintFlavorKind,
ScopeKind = ScopeKind,
PrintTypeQualifiers = PrintTypeQualifiers,
PrintTypeModifiers = PrintTypeModifiers,
ResolveTypeMaps = false
};
typePrinter.PushContext(ContextKind);
typePrinter.PushScope(ScopeKind);
var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter);
result = new TypePrinterResult(typeName) { TypeMap = typeMap };

3
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -126,8 +126,9 @@ namespace CppSharp.Generators.CSharp @@ -126,8 +126,9 @@ namespace CppSharp.Generators.CSharp
var names = new List<string> { mapped.OriginalName };
foreach (TypePrintScopeKind kind in Enum.GetValues(typeof(TypePrintScopeKind)))
{
var cppTypePrinter = new CppTypePrinter(context) { ScopeKind = kind };
var cppTypePrinter = new CppTypePrinter(context);
cppTypePrinter.PushContext(TypePrinterContextKind.Native);
cppTypePrinter.PushScope(kind);
names.Add(mapped.Visit(cppTypePrinter));
}
foreach (var name in names.Where(context.TypeMaps.TypeMaps.ContainsKey))

4
src/Generator/Generators/NAPI/NAPISources.cs

@ -113,14 +113,14 @@ namespace CppSharp.Generators.Cpp @@ -113,14 +113,14 @@ namespace CppSharp.Generators.Cpp
var cTypePrinter = new CppTypePrinter(context)
{
PrintFlavorKind = CppTypePrintFlavorKind.C,
ScopeKind = TypePrintScopeKind.Local
};
cTypePrinter.PushScope(TypePrintScopeKind.Local);
var functionName = cTypePrinter.VisitDeclaration(decl).ToString();
if (scope == TypePrintScopeKind.Local)
return functionName;
cTypePrinter.ScopeKind = scope;
cTypePrinter.PushScope(scope);
var qualifiedParentName = cTypePrinter.VisitDeclaration(decl.Namespace).ToString();
// HACK: CppTypePrinter code calls into decl.QualifiedName, which does not take into

21
src/Generator/Generators/TypePrinter.cs

@ -49,6 +49,7 @@ namespace CppSharp.Generators @@ -49,6 +49,7 @@ namespace CppSharp.Generators
{
private readonly Stack<TypePrinterContextKind> contexts;
private readonly Stack<MarshalKind> marshalKinds;
private readonly Stack<TypePrintScopeKind> scopeKinds;
public TypePrinterContextKind ContextKind => contexts.Peek();
@ -56,30 +57,28 @@ namespace CppSharp.Generators @@ -56,30 +57,28 @@ namespace CppSharp.Generators
public MarshalKind MarshalKind => marshalKinds.Peek();
public TypePrintScopeKind ScopeKind = TypePrintScopeKind.GlobalQualified;
public TypePrintScopeKind ScopeKind => scopeKinds.Peek();
public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified;
public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed)
{
contexts = new Stack<TypePrinterContextKind>();
marshalKinds = new Stack<MarshalKind>();
scopeKinds = new Stack<TypePrintScopeKind>();
PushContext(contextKind);
PushMarshalKind(MarshalKind.Unknown);
PushScope(TypePrintScopeKind.GlobalQualified);
}
public void PushContext(TypePrinterContextKind contextKind)
{
contexts.Push(contextKind);
}
public void PushContext(TypePrinterContextKind kind) => contexts.Push(kind);
public TypePrinterContextKind PopContext() => contexts.Pop();
public void PushMarshalKind(MarshalKind marshalKind)
{
marshalKinds.Push(marshalKind);
}
public void PushMarshalKind(MarshalKind kind) => marshalKinds.Push(kind);
public MarshalKind PopMarshalKind() => marshalKinds.Pop();
public void PushScope(TypePrintScopeKind kind) => scopeKinds.Push(kind);
public TypePrintScopeKind PopScope() => scopeKinds.Pop();
public Parameter Parameter;
#region Dummy implementations

2
src/Generator/Passes/SymbolsCodeGenerator.cs

@ -18,12 +18,12 @@ namespace CppSharp.Passes @@ -18,12 +18,12 @@ namespace CppSharp.Passes
{
cppTypePrinter = new CppTypePrinter(Context)
{
ScopeKind = TypePrintScopeKind.Qualified,
ResolveTypedefs = true,
ResolveTypeMaps = false
};
cppTypePrinter.PushContext(TypePrinterContextKind.Native);
cppTypePrinter.PushScope(TypePrintScopeKind.Qualified);
}
public override void Process()

3
src/Generator/Types/TypeMapDatabase.cs

@ -110,8 +110,9 @@ namespace CppSharp.Types @@ -110,8 +110,9 @@ namespace CppSharp.Types
new[] { TypePrintScopeKind.Local, TypePrintScopeKind.Qualified })
{
typePrinter.ResolveTypedefs = resolveTypeDefs;
typePrinter.ScopeKind = typePrintScopeKind;
typePrinter.PushScope(typePrintScopeKind);
var typeName = type.Visit(typePrinter);
typePrinter.PopScope();
if (FindTypeMap(typeName, out typeMap))
{
typeMap.Type = type;

Loading…
Cancel
Save