Skip to content

Commit efa11ab

Browse files
committed
Simplified type maps by unlinking them from declarations.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 55053fd commit efa11ab

17 files changed

+71
-156
lines changed

src/AST/CppTypePrinter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ public virtual string VisitTypedefDecl(TypedefDecl typedef)
444444
return typedef.OriginalName;
445445

446446
var originalNamespace = typedef.OriginalNamespace.Visit(this);
447-
return originalNamespace == "::" ? typedef.OriginalName :
448-
$"{originalNamespace}::{typedef.OriginalName}";
447+
return string.IsNullOrEmpty(originalNamespace) ||
448+
originalNamespace == "::" ?
449+
typedef.OriginalName : $"{originalNamespace}::{typedef.OriginalName}";
449450
}
450451

451452
public virtual string VisitTypeAliasDecl(TypeAlias typeAlias)

src/Generator/AST/Utils.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ public static bool CheckTypeForSpecialization(Type type, Declaration container,
9696
if (specialization == null)
9797
return true;
9898

99-
if (IsSpecializationNeeded(container, typeMaps, internalOnly, specialization))
99+
if (IsSpecializationNeeded(container, typeMaps, internalOnly,
100+
type, specialization))
100101
return false;
101102

102103
if (!internalOnly)
103104
{
104105
if (IsSpecializationSelfContained(specialization, container))
105106
return true;
106107

107-
if (IsMappedToPrimitive(typeMaps, type, specialization))
108+
if (IsMappedToPrimitive(typeMaps, type))
108109
return true;
109110
}
110111

@@ -161,11 +162,11 @@ private static bool UnsupportedTemplateArgument(
161162
}
162163

163164
private static bool IsSpecializationNeeded(Declaration container,
164-
ITypeMapDatabase typeMaps, bool internalOnly,
165+
ITypeMapDatabase typeMaps, bool internalOnly, Type type,
165166
ClassTemplateSpecialization specialization)
166167
{
167168
TypeMap typeMap;
168-
typeMaps.FindTypeMap(specialization, out typeMap);
169+
typeMaps.FindTypeMap(type, out typeMap);
169170

170171
return (!internalOnly && (((specialization.Ignore ||
171172
specialization.TemplatedDecl.TemplatedClass.Ignore) && typeMap == null) ||
@@ -204,11 +205,10 @@ private static bool IsSpecializationSelfContained(
204205
return false;
205206
}
206207

207-
public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps,
208-
Type type, Declaration declaration)
208+
public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type)
209209
{
210210
TypeMap typeMap;
211-
if (!typeMaps.FindTypeMap(declaration, out typeMap))
211+
if (!typeMaps.FindTypeMap(type, out typeMap))
212212
return false;
213213

214214
var typePrinterContext = new TypePrinterContext { Type = type };

src/Generator/Generators/CLI/CLIHeaders.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ public void GenerateClassGenericMethods(Class @class)
333333

334334
var function = functionTemplate.TemplatedFunction;
335335

336-
var typePrinter = new CLITypePrinter(Context)
337-
{
338-
Declaration = template
339-
};
336+
var typePrinter = new CLITypePrinter(Context);
340337
typePrinter.PushContext(TypePrinterContextKind.Template);
341338

342339
var retType = function.ReturnType.Visit(typePrinter);

src/Generator/Generators/CLI/CLIMarshal.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
237237
var decl = typedef.Declaration;
238238

239239
TypeMap typeMap;
240-
if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling)
240+
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
241+
typeMap.DoesMarshalling)
241242
{
242243
typeMap.Type = typedef;
243244
typeMap.CLIMarshalToManaged(Context);
@@ -298,7 +299,7 @@ public override bool VisitClassDecl(Class @class)
298299
instance += "&";
299300

300301
instance += Context.ReturnVarName;
301-
var needsCopy = !(Context.Declaration is Field);
302+
var needsCopy = Context.MarshalKind != MarshalKind.NativeField;
302303

303304
if (@class.IsRefType && needsCopy)
304305
{
@@ -608,7 +609,8 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
608609
var decl = typedef.Declaration;
609610

610611
TypeMap typeMap;
611-
if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling)
612+
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
613+
typeMap.DoesMarshalling)
612614
{
613615
typeMap.CLIMarshalToNative(Context);
614616
return typeMap.IsValueType;
@@ -689,14 +691,15 @@ public override bool VisitClassDecl(Class @class)
689691

690692
private void MarshalRefClass(Class @class)
691693
{
694+
var type = Context.Parameter.Type.Desugar();
692695
TypeMap typeMap;
693-
if (Context.Context.TypeMaps.FindTypeMap(@class, out typeMap) && typeMap.DoesMarshalling)
696+
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
697+
typeMap.DoesMarshalling)
694698
{
695699
typeMap.CLIMarshalToNative(Context);
696700
return;
697701
}
698702

699-
var type = Context.Parameter.Type.Desugar();
700703
var method = Context.Function as Method;
701704
if (type.IsReference() && (method == null ||
702705
// redundant for comparison operators, they are handled in a special way

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ private void GenerateFunctionTemplate(FunctionTemplate template)
289289

290290
var function = template.TemplatedFunction;
291291

292-
var typePrinter = new CLITypePrinter(Context)
293-
{
294-
Declaration = template
295-
};
292+
var typePrinter = new CLITypePrinter(Context);
296293
typePrinter.PushContext(TypePrinterContextKind.Template);
297294

298295
var retType = function.ReturnType.Visit(typePrinter);
@@ -485,12 +482,11 @@ private void GeneratePropertyGetter<T>(T decl, Class @class, string name, Type t
485482

486483
var ctx = new MarshalContext(Context, CurrentIndentation)
487484
{
488-
Declaration = decl,
489485
ArgName = decl.Name,
490486
ReturnVarName = variable,
491487
ReturnType = decl.QualifiedType
492488
};
493-
489+
ctx.PushMarshalKind(MarshalKind.NativeField);
494490
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
495491
decl.Visit(marshal);
496492

@@ -691,10 +687,9 @@ private void GenerateStructMarshaling(Class @class, string nativeVar)
691687
ArgName = property.Name,
692688
ReturnVarName = nativeField,
693689
ReturnType = property.QualifiedType,
694-
Declaration = property.Field,
695690
ParameterIndex = paramIndex++
696691
};
697-
692+
ctx.PushMarshalKind(MarshalKind.NativeField);
698693
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
699694
property.Visit(marshal);
700695

src/Generator/Generators/CLI/CLITypePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
197197
var decl = typedef.Declaration;
198198

199199
TypeMap typeMap = null;
200-
if (TypeMapDatabase.FindTypeMap(decl, out typeMap))
200+
if (TypeMapDatabase.FindTypeMap(decl.Type, out typeMap))
201201
{
202202
typeMap.Type = typedef;
203203
var typePrinterContext = new TypePrinterContext { Type = typedef };

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
5858
return true;
5959
}
6060

61-
public override bool VisitDeclaration(Declaration decl)
62-
{
63-
TypeMap typeMap;
64-
if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling)
65-
{
66-
typeMap.CSharpMarshalToManaged(Context);
67-
return false;
68-
}
69-
70-
return true;
71-
}
61+
public override bool VisitDeclaration(Declaration decl) => true;
7262

7363
public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
7464
{
@@ -431,17 +421,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
431421
return true;
432422
}
433423

434-
public override bool VisitDeclaration(Declaration decl)
435-
{
436-
TypeMap typeMap;
437-
if (Context.Context.TypeMaps.FindTypeMap(decl, out typeMap) && typeMap.DoesMarshalling)
438-
{
439-
typeMap.CSharpMarshalToNative(Context);
440-
return false;
441-
}
442-
443-
return true;
444-
}
424+
public override bool VisitDeclaration(Declaration decl) => true;
445425

446426
public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
447427
{

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
910910
ctx.PushMarshalKind(MarshalKind.NativeField);
911911

912912
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
913-
ctx.Declaration = field;
913+
ctx.PushMarshalKind(MarshalKind.NativeField);
914+
ctx.ReturnType = field.QualifiedType;
914915

915916
param.Visit(marshal);
916917

@@ -1188,14 +1189,13 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
11881189
// IntPtr ensures that non-copying object constructor is invoked.
11891190
Class typeClass;
11901191
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType &&
1191-
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type, typeClass))
1192+
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type))
11921193
returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})";
11931194
}
11941195

11951196
var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
11961197
{
11971198
ArgName = field.Name,
1198-
Declaration = field,
11991199
ReturnVarName = returnVar,
12001200
ReturnType = returnType
12011201
};
@@ -2734,16 +2734,17 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
27342734
var indirectRetType = originalFunction.Parameters.First(
27352735
parameter => parameter.Kind == ParameterKind.IndirectReturnType);
27362736

2737-
Class retClass;
2738-
indirectRetType.Type.Desugar().TryGetClass(out retClass);
2737+
Type type = indirectRetType.Type.Desugar();
27392738

27402739
TypeMap typeMap;
27412740
string construct = null;
2742-
if (Context.TypeMaps.FindTypeMap(retClass, out typeMap))
2741+
if (Context.TypeMaps.FindTypeMap(type, out typeMap))
27432742
construct = typeMap.CSharpConstruct();
27442743

27452744
if (construct == null)
27462745
{
2746+
Class retClass;
2747+
type.TryGetClass(out retClass);
27472748
var @class = retClass.OriginalClass ?? retClass;
27482749
WriteLine($@"var {Helpers.ReturnIdentifier} = new {
27492750
TypePrinter.PrintNative(@class)}();");
@@ -2752,10 +2753,10 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
27522753
{
27532754
if (string.IsNullOrWhiteSpace(construct))
27542755
{
2755-
var typePrinterContext = new TypePrinterContext
2756-
{
2757-
Type = indirectRetType.Type.Desugar()
2758-
};
2756+
var typePrinterContext = new TypePrinterContext
2757+
{
2758+
Type = indirectRetType.Type.Desugar()
2759+
};
27592760

27602761
WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
27612762
Helpers.ReturnIdentifier);

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
3131
return string.Empty;
3232

3333
TypeMap typeMap;
34-
if (TypeMapDatabase.FindTypeMap(tag.Declaration, out typeMap))
34+
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
3535
{
3636
typeMap.Type = tag;
3737

@@ -271,7 +271,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
271271
var decl = typedef.Declaration;
272272

273273
TypeMap typeMap;
274-
if (TypeMapDatabase.FindTypeMap(decl, out typeMap))
274+
if (TypeMapDatabase.FindTypeMap(decl.Type, out typeMap))
275275
{
276276
typeMap.Type = typedef;
277277

src/Generator/Generators/TypePrinter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public void PushMarshalKind(MarshalKind marshalKind)
5050
}
5151

5252
public MarshalKind PopMarshalKind() => marshalKinds.Pop();
53-
54-
public Declaration Declaration;
5553
public Parameter Parameter;
5654

5755
#region Dummy implementations

0 commit comments

Comments
 (0)