1
- using System . Linq ;
1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
2
3
using CppSharp . AST ;
3
4
using CppSharp . AST . Extensions ;
4
5
@@ -21,7 +22,13 @@ public override bool VisitFunctionDecl(Function function)
21
22
if ( ! base . VisitFunctionDecl ( function ) )
22
23
return false ;
23
24
24
- if ( ! function . IsOperator || function . Parameters . Count > 1 )
25
+ // parameters and returns from a specialised interface
26
+ // must not be replaced if the templated interface uses a template parameter
27
+ Function templateInterfaceFunction = GetTemplateInterfaceFunction ( function ) ;
28
+
29
+ if ( ( ! function . IsOperator || function . Parameters . Count > 1 ) &&
30
+ ( templateInterfaceFunction == null ||
31
+ ! IsTemplateParameter ( templateInterfaceFunction . OriginalReturnType ) ) )
25
32
{
26
33
var originalReturnType = function . OriginalReturnType ;
27
34
ChangeToInterfaceType ( ref originalReturnType ) ;
@@ -30,13 +37,27 @@ public override bool VisitFunctionDecl(Function function)
30
37
31
38
if ( function . OperatorKind != CXXOperatorKind . Conversion &&
32
39
function . OperatorKind != CXXOperatorKind . ExplicitConversion )
33
- foreach ( var parameter in function . Parameters . Where (
34
- p => p . Kind != ParameterKind . OperatorParameter ) )
40
+ {
41
+ IList < Parameter > parameters = function . Parameters . Where (
42
+ p => p . Kind != ParameterKind . OperatorParameter &&
43
+ p . Kind != ParameterKind . IndirectReturnType ) . ToList ( ) ;
44
+
45
+ var templateFunctionParameters = new List < Parameter > ( ) ;
46
+ if ( templateInterfaceFunction != null )
47
+ templateFunctionParameters . AddRange (
48
+ templateInterfaceFunction . Parameters . Where (
49
+ p => p . Kind != ParameterKind . OperatorParameter &&
50
+ p . Kind != ParameterKind . IndirectReturnType ) ) ;
51
+ for ( int i = 0 ; i < parameters . Count ; i ++ )
35
52
{
36
- var qualifiedType = parameter . QualifiedType ;
53
+ if ( templateFunctionParameters . Any ( ) &&
54
+ IsTemplateParameter ( templateFunctionParameters [ i ] . QualifiedType ) )
55
+ continue ;
56
+ var qualifiedType = parameters [ i ] . QualifiedType ;
37
57
ChangeToInterfaceType ( ref qualifiedType ) ;
38
- parameter . QualifiedType = qualifiedType ;
58
+ parameters [ i ] . QualifiedType = qualifiedType ;
39
59
}
60
+ }
40
61
41
62
return true ;
42
63
}
@@ -46,12 +67,55 @@ public override bool VisitProperty(Property property)
46
67
if ( ! base . VisitProperty ( property ) )
47
68
return false ;
48
69
70
+ var templateInterfaceProperty = GetTemplateInterfaceProperty ( property ) ;
71
+
72
+ if ( templateInterfaceProperty != null &&
73
+ IsTemplateParameter ( templateInterfaceProperty . QualifiedType ) )
74
+ return false ;
75
+
49
76
var type = property . QualifiedType ;
50
77
ChangeToInterfaceType ( ref type ) ;
51
78
property . QualifiedType = type ;
52
79
return true ;
53
80
}
54
81
82
+ private static Function GetTemplateInterfaceFunction ( Function function )
83
+ {
84
+ Function templateInterfaceFunction = null ;
85
+ Class @class = function . OriginalNamespace as Class ;
86
+ if ( @class != null && @class . IsInterface )
87
+ templateInterfaceFunction = @class . Methods . First (
88
+ m => m . OriginalFunction == function . OriginalFunction ) . InstantiatedFrom ;
89
+ return templateInterfaceFunction ;
90
+ }
91
+
92
+ private static Property GetTemplateInterfaceProperty ( Property property )
93
+ {
94
+ if ( property . GetMethod != null &&
95
+ property . GetMethod . SynthKind == FunctionSynthKind . InterfaceInstance )
96
+ return null ;
97
+
98
+ Property templateInterfaceProperty = null ;
99
+ Class @class = property . OriginalNamespace as Class ;
100
+ if ( @class != null && @class . IsInterface )
101
+ {
102
+ var specialization = @class as ClassTemplateSpecialization ;
103
+ if ( specialization != null )
104
+ {
105
+ Class template = specialization . TemplatedDecl . TemplatedClass ;
106
+ templateInterfaceProperty = template . Properties . FirstOrDefault (
107
+ p => p . Name == property . Name ) ;
108
+ }
109
+ }
110
+
111
+ return templateInterfaceProperty ;
112
+ }
113
+
114
+ private static bool IsTemplateParameter ( QualifiedType type )
115
+ {
116
+ return ( type . Type . Desugar ( ) . GetFinalPointee ( ) ?? type . Type ) . Desugar ( ) is TemplateParameterType ;
117
+ }
118
+
55
119
private static void ChangeToInterfaceType ( ref QualifiedType type )
56
120
{
57
121
var finalType = ( type . Type . GetFinalPointee ( ) ?? type . Type ) . Desugar ( ) ;
0 commit comments