44
44
* @author Pavel Talanov
45
45
*/
46
46
//TODO: write tests on calling backing fields as functions
47
- //TODO: refactor call parameters to be used across the class
47
+ //TODO: think of the refactoring the class
48
48
public final class CallTranslator extends AbstractTranslator {
49
49
50
50
private static class CallParameters {
@@ -96,13 +96,15 @@ public CallParameters(@Nullable JsExpression receiver, @NotNull JsExpression fun
96
96
97
97
@ NotNull
98
98
/*package*/ JsExpression translate () {
99
+ if (isExpressionAsFunction ()) {
100
+ return expressionAsFunctionCall ();
101
+ }
99
102
if (isIntrinsic ()) {
100
103
return intrinsicInvocation ();
101
104
}
102
105
if (isConstructor ()) {
103
106
return constructorCall ();
104
107
}
105
- //NOTE: treat native extension function calls as usual calls
106
108
if (isNativeExtensionFunctionCall ()) {
107
109
return nativeExtensionCall ();
108
110
}
@@ -112,13 +114,17 @@ public CallParameters(@Nullable JsExpression receiver, @NotNull JsExpression fun
112
114
if (isExtensionFunction ()) {
113
115
return extensionFunctionCall ();
114
116
}
115
- return methodCall ();
117
+ return methodCall (callParameters ( resolveThisObject ( /*just get qualifier if null*/ true )) );
116
118
}
117
119
118
- @ NotNull
119
- private JsExpression nativeExtensionCall () {
120
- receiver = getExtensionFunctionCallReceiver ();
121
- return methodCall ();
120
+ private boolean isExpressionAsFunction () {
121
+ return callee != null ;
122
+ }
123
+
124
+ private JsExpression expressionAsFunctionCall () {
125
+ assert callee != null ;
126
+ CallParameters expressionAsFunctionParameters = new CallParameters (null , callee );
127
+ return methodCall (expressionAsFunctionParameters );
122
128
}
123
129
124
130
private boolean isIntrinsic () {
@@ -155,6 +161,12 @@ private boolean isNativeExtensionFunctionCall() {
155
161
return AnnotationsUtils .isNativeObject (descriptor ) && isExtensionFunction ();
156
162
}
157
163
164
+ @ NotNull
165
+ private JsExpression nativeExtensionCall () {
166
+ receiver = getExtensionFunctionCallReceiver ();
167
+ return methodCall (callParameters (resolveThisObject (/*just get qualifier if null = */ true )));
168
+ }
169
+
158
170
private boolean isExtensionFunctionLiteral () {
159
171
boolean isLiteral = descriptor instanceof VariableAsFunctionDescriptor
160
172
|| descriptor instanceof ExpressionAsFunctionDescriptor ;
@@ -187,7 +199,7 @@ private JsInvocation generateCallMethodInvocation() {
187
199
JsNameRef callMethodNameRef = AstUtil .newQualifiedNameRef ("call" );
188
200
JsInvocation callMethodInvocation = new JsInvocation ();
189
201
callMethodInvocation .setQualifier (callMethodNameRef );
190
- setQualifier (callMethodInvocation , callParameters ().functionReference );
202
+ setQualifier (callMethodInvocation , callParameters (resolveThisObject ( /*just get qualifier if null*/ true ) ).functionReference );
191
203
return callMethodInvocation ;
192
204
}
193
205
@@ -226,8 +238,9 @@ private JsExpression getExtensionFunctionCallReceiver() {
226
238
@ NotNull
227
239
private JsExpression constructExtensionFunctionCall (@ NotNull JsExpression receiver ) {
228
240
List <JsExpression > argumentList = generateExtensionCallArgumentList (receiver );
229
- JsExpression functionReference = callParameters ().functionReference ;
230
- setQualifier (functionReference , callParameters ().receiver );
241
+ CallParameters callParameters = callParameters (resolveThisObject (/*just get qualifier if null*/ true ));
242
+ JsExpression functionReference = callParameters .functionReference ;
243
+ setQualifier (functionReference , callParameters .receiver );
231
244
return newInvocation (functionReference , argumentList );
232
245
}
233
246
@@ -241,8 +254,7 @@ private List<JsExpression> generateExtensionCallArgumentList(@NotNull JsExpressi
241
254
}
242
255
243
256
@ NotNull
244
- private JsExpression methodCall () {
245
- final CallParameters callParameters = callParameters ();
257
+ private JsExpression methodCall (@ NotNull final CallParameters callParameters ) {
246
258
return callType .constructCall (callParameters .receiver , new CallType .CallConstructor () {
247
259
@ NotNull
248
260
@ Override
@@ -257,14 +269,9 @@ public JsExpression construct(@Nullable JsExpression receiver) {
257
269
}
258
270
259
271
@ NotNull
260
- private CallParameters callParameters () {
261
- // TODO: this check corresponds to expression as function, make it clearer
262
- if (callee != null ) {
263
- return new CallParameters (null , callee );
264
- }
265
- JsExpression thisObject = resolveThisObject (/*just get qualifier if null*/ true );
272
+ private CallParameters callParameters (@ Nullable JsExpression receiver ) {
266
273
JsExpression functionReference = functionReference ();
267
- return new CallParameters (thisObject , functionReference );
274
+ return new CallParameters (receiver , functionReference );
268
275
}
269
276
270
277
@ NotNull
0 commit comments