Browse Source

Cached found type maps for faster look-ups.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1146/head
Dimitar Dobrev 7 years ago
parent
commit
02f70d0b35
  1. 55
      src/Generator/Types/TypeMapDatabase.cs

55
src/Generator/Types/TypeMapDatabase.cs

@ -50,53 +50,64 @@ namespace CppSharp.Types
public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap) public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap)
{ {
if (type != null && typeMaps.ContainsKey(type))
{
typeMap = typeMaps[type];
return typeMap.IsEnabled;
}
// We try to find type maps from the most qualified to less qualified // We try to find type maps from the most qualified to less qualified
// types. Example: '::std::vector', 'std::vector' and 'vector' // types. Example: '::std::vector', 'std::vector' and 'vector'
var typePrinter = new CppTypePrinter { PrintLogicalNames = true }; var typePrinter = new CppTypePrinter { PrintLogicalNames = true };
if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) if (FindTypeMap(decl, type, out typeMap, typePrinter))
{
typeMap.Type = type;
return true; return true;
}
typePrinter.PrintScopeKind = TypePrintScopeKind.Qualified; typePrinter.PrintScopeKind = TypePrintScopeKind.Qualified;
if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) if (FindTypeMap(decl, type, out typeMap, typePrinter))
{
typeMap.Type = type;
return true; return true;
}
typePrinter.ResolveTypedefs = true; typePrinter.ResolveTypedefs = true;
if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) if (FindTypeMap(decl, type, out typeMap, typePrinter))
{
typeMap.Type = type;
return true; return true;
}
typePrinter.ResolveTypedefs = false; typePrinter.ResolveTypedefs = false;
typePrinter.PrintScopeKind = TypePrintScopeKind.Local; typePrinter.PrintScopeKind = TypePrintScopeKind.Local;
if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) if (FindTypeMap(decl, type, out typeMap, typePrinter))
{
typeMap.Type = type;
return true; return true;
}
var specialization = decl as ClassTemplateSpecialization; var specialization = decl as ClassTemplateSpecialization;
if (specialization != null && if (specialization != null &&
FindTypeMap(specialization.TemplatedDecl.Visit(typePrinter), out typeMap)) FindTypeMap(specialization.TemplatedDecl, type, out typeMap, typePrinter))
{
typeMap.Type = type;
return true; return true;
}
var typedef = decl as TypedefDecl; var typedef = decl as TypedefDecl;
return typedef != null && FindTypeMap(typedef.Type, out typeMap); return typedef != null && FindTypeMap(typedef.Type, out typeMap);
} }
private bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap, CppTypePrinter typePrinter)
{
if (FindTypeMap(decl.Visit(typePrinter), out typeMap))
{
if (type != null && typeMap.Type == null)
{
typeMap.Type = type;
typeMaps[type] = typeMap;
}
return true;
}
return false;
}
public bool FindTypeMap(Type type, out TypeMap typeMap) public bool FindTypeMap(Type type, out TypeMap typeMap)
{ {
if (typeMaps.ContainsKey(type))
{
typeMap = typeMaps[type];
return typeMap.IsEnabled;
}
var typePrinter = new CppTypePrinter var typePrinter = new CppTypePrinter
{ {
PrintTypeQualifiers = false, PrintTypeQualifiers = false,
@ -119,6 +130,7 @@ namespace CppSharp.Types
if (FindTypeMap(type.Visit(typePrinter), out typeMap)) if (FindTypeMap(type.Visit(typePrinter), out typeMap))
{ {
typeMap.Type = type; typeMap.Type = type;
typeMaps[type] = typeMap;
return true; return true;
} }
@ -126,6 +138,7 @@ namespace CppSharp.Types
if (FindTypeMap(type.Visit(typePrinter), out typeMap)) if (FindTypeMap(type.Visit(typePrinter), out typeMap))
{ {
typeMap.Type = type; typeMap.Type = type;
typeMaps[type] = typeMap;
return true; return true;
} }
@ -157,5 +170,7 @@ namespace CppSharp.Types
{ {
return TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled; return TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled;
} }
private Dictionary<Type, TypeMap> typeMaps = new Dictionary<Type, TypeMap>();
} }
} }

Loading…
Cancel
Save