Browse Source

Introduce a scope kind stack for type printers.

Just like we already have for other context data.
pull/1581/head
Joao Matos 5 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
exprCxxUnit.Visit(exprSubclassVisitor); exprCxxUnit.Visit(exprSubclassVisitor);
ExprClasses = exprSubclassVisitor.Classes; ExprClasses = exprSubclassVisitor.Classes;
CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context) CodeGeneratorHelpers.CppTypePrinter = new CppTypePrinter(driver.Context);
{ ScopeKind = TypePrintScopeKind.Local }; CodeGeneratorHelpers.CppTypePrinter.PushScope(TypePrintScopeKind.Local);
GenerateStmt(driver.Context); GenerateStmt(driver.Context);
GenerateExpr(driver.Context); GenerateExpr(driver.Context);
@ -501,7 +501,7 @@ namespace CppSharp
: base(context) : base(context)
{ {
Declarations = declarations; Declarations = declarations;
TypePrinter.ScopeKind = TypePrintScopeKind.Local; TypePrinter.PushScope(TypePrintScopeKind.Local);
TypePrinter.PrintModuleOutputNamespace = false; TypePrinter.PrintModuleOutputNamespace = false;
} }
@ -1438,7 +1438,7 @@ namespace CppSharp
public override void Process() public override void Process()
{ {
Context.Options.GeneratorKind = GeneratorKind.CPlusPlus; Context.Options.GeneratorKind = GeneratorKind.CPlusPlus;
CTypePrinter.ScopeKind = TypePrintScopeKind.Local; CTypePrinter.PushScope(TypePrintScopeKind.Local);
GenerateFilePreamble(CommentKind.BCPL); GenerateFilePreamble(CommentKind.BCPL);
NewLine(); NewLine();
@ -1755,12 +1755,9 @@ namespace CppSharp
public static string GetQualifiedName(Declaration decl, public static string GetQualifiedName(Declaration decl,
TypePrinter typePrinter) TypePrinter typePrinter)
{ {
var scopeKind = typePrinter.ScopeKind; typePrinter.PushScope(TypePrintScopeKind.Qualified);
typePrinter.ScopeKind = TypePrintScopeKind.Qualified;
var qualifiedName = decl.Visit(typePrinter).Type; var qualifiedName = decl.Visit(typePrinter).Type;
typePrinter.PopScope();
typePrinter.ScopeKind = scopeKind;
qualifiedName = CleanClangNamespaceFromName(qualifiedName); qualifiedName = CleanClangNamespaceFromName(qualifiedName);
@ -1905,10 +1902,9 @@ namespace CppSharp
if (iteratorType.IsPointer()) if (iteratorType.IsPointer())
iteratorType = iteratorType.GetFinalPointee(); iteratorType = iteratorType.GetFinalPointee();
var scopeKind = typePrinter.ScopeKind; typePrinter.PushScope(TypePrintScopeKind.Qualified);
typePrinter.ScopeKind = TypePrintScopeKind.Qualified;
var iteratorTypeName = iteratorType.Visit(typePrinter).Type; var iteratorTypeName = iteratorType.Visit(typePrinter).Type;
typePrinter.ScopeKind = scopeKind; typePrinter.PopScope();
iteratorTypeName = CleanClangNamespaceFromName(iteratorTypeName); iteratorTypeName = CleanClangNamespaceFromName(iteratorTypeName);

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

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

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

@ -353,14 +353,20 @@ namespace CppSharp.Generators.Cpp
public override string GetMethodIdentifier(Function function, public override string GetMethodIdentifier(Function function,
TypePrinterContextKind context = TypePrinterContextKind.Managed) TypePrinterContextKind context = TypePrinterContextKind.Managed)
{ {
var method = function as Method; string id;
if (method != null) if (function is Method method)
{ {
var @class = method.Namespace as Class; 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) public override bool VisitMethodDecl(Method method)

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

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

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

@ -126,8 +126,9 @@ namespace CppSharp.Generators.CSharp
var names = new List<string> { mapped.OriginalName }; var names = new List<string> { mapped.OriginalName };
foreach (TypePrintScopeKind kind in Enum.GetValues(typeof(TypePrintScopeKind))) 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.PushContext(TypePrinterContextKind.Native);
cppTypePrinter.PushScope(kind);
names.Add(mapped.Visit(cppTypePrinter)); names.Add(mapped.Visit(cppTypePrinter));
} }
foreach (var name in names.Where(context.TypeMaps.TypeMaps.ContainsKey)) 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
var cTypePrinter = new CppTypePrinter(context) var cTypePrinter = new CppTypePrinter(context)
{ {
PrintFlavorKind = CppTypePrintFlavorKind.C, PrintFlavorKind = CppTypePrintFlavorKind.C,
ScopeKind = TypePrintScopeKind.Local
}; };
cTypePrinter.PushScope(TypePrintScopeKind.Local);
var functionName = cTypePrinter.VisitDeclaration(decl).ToString(); var functionName = cTypePrinter.VisitDeclaration(decl).ToString();
if (scope == TypePrintScopeKind.Local) if (scope == TypePrintScopeKind.Local)
return functionName; return functionName;
cTypePrinter.ScopeKind = scope; cTypePrinter.PushScope(scope);
var qualifiedParentName = cTypePrinter.VisitDeclaration(decl.Namespace).ToString(); var qualifiedParentName = cTypePrinter.VisitDeclaration(decl.Namespace).ToString();
// HACK: CppTypePrinter code calls into decl.QualifiedName, which does not take into // 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
{ {
private readonly Stack<TypePrinterContextKind> contexts; private readonly Stack<TypePrinterContextKind> contexts;
private readonly Stack<MarshalKind> marshalKinds; private readonly Stack<MarshalKind> marshalKinds;
private readonly Stack<TypePrintScopeKind> scopeKinds;
public TypePrinterContextKind ContextKind => contexts.Peek(); public TypePrinterContextKind ContextKind => contexts.Peek();
@ -56,30 +57,28 @@ namespace CppSharp.Generators
public MarshalKind MarshalKind => marshalKinds.Peek(); public MarshalKind MarshalKind => marshalKinds.Peek();
public TypePrintScopeKind ScopeKind = TypePrintScopeKind.GlobalQualified; public TypePrintScopeKind ScopeKind => scopeKinds.Peek();
public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified; public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified;
public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed) public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed)
{ {
contexts = new Stack<TypePrinterContextKind>(); contexts = new Stack<TypePrinterContextKind>();
marshalKinds = new Stack<MarshalKind>(); marshalKinds = new Stack<MarshalKind>();
scopeKinds = new Stack<TypePrintScopeKind>();
PushContext(contextKind); PushContext(contextKind);
PushMarshalKind(MarshalKind.Unknown); PushMarshalKind(MarshalKind.Unknown);
PushScope(TypePrintScopeKind.GlobalQualified);
} }
public void PushContext(TypePrinterContextKind contextKind) public void PushContext(TypePrinterContextKind kind) => contexts.Push(kind);
{
contexts.Push(contextKind);
}
public TypePrinterContextKind PopContext() => contexts.Pop(); public TypePrinterContextKind PopContext() => contexts.Pop();
public void PushMarshalKind(MarshalKind marshalKind) public void PushMarshalKind(MarshalKind kind) => marshalKinds.Push(kind);
{
marshalKinds.Push(marshalKind);
}
public MarshalKind PopMarshalKind() => marshalKinds.Pop(); public MarshalKind PopMarshalKind() => marshalKinds.Pop();
public void PushScope(TypePrintScopeKind kind) => scopeKinds.Push(kind);
public TypePrintScopeKind PopScope() => scopeKinds.Pop();
public Parameter Parameter; public Parameter Parameter;
#region Dummy implementations #region Dummy implementations

2
src/Generator/Passes/SymbolsCodeGenerator.cs

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

3
src/Generator/Types/TypeMapDatabase.cs

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

Loading…
Cancel
Save