Class SyntheticMethodCallBuilder

java.lang.Object
org.ek9lang.compiler.phase7.synthesis.support.SyntheticMethodCallBuilder

public final class SyntheticMethodCallBuilder extends Object
Fluent builder for generating method and constructor call IR instructions.

Replaces the 6+ overloaded generateMethodCall methods and generateConstructorCall variants with a self-documenting fluent API.

Method call examples:

// No-arg method call
methodCall()
    .on(target, targetType)
    .method("_isSet")
    .returning(booleanType)
    .into(resultVar)
    .build(ctx);

// Single-arg method call
methodCall()
    .on(target, targetType)
    .method("_eq")
    .arg(otherVar, otherType)
    .returning(booleanType)
    .into(resultVar)
    .build(ctx);

// Trait call (invokeinterface)
methodCall()
    .on(fieldVar, traitType)
    .method("_isSet")
    .returning(booleanType)
    .traitCall()
    .into(resultVar)
    .build(ctx);

// Void method call (no result variable)
methodCall()
    .on(target, targetType)
    .method("_unSet")
    .returning(voidType)
    .buildVoid(ctx);

// Super call (uses invokespecial, not invokevirtual)
methodCall()
    .onSuper(superType)
    .method("_eq")
    .arg(paramVar, superType)
    .returning(booleanType)
    .into(resultVar)
    .build(ctx);

// Static call
methodCall()
    .onStatic(stringType)
    .method("_of")
    .arg(primitiveVar, "java.lang.String")
    .returning(stringType)
    .into(resultVar)
    .build(ctx);

Constructor call examples:

// No-arg constructor
constructorCall()
    .type(booleanType)
    .into(resultVar)
    .build(ctx);

// Constructor with args
constructorCall()
    .type(bitsType)
    .arg(emptyStringVar, stringType)
    .into(resultVar)
    .build(ctx);

The builder handles CALL + RETAIN + SCOPE_REGISTER automatically, skipping memory management for primitive return types (boolean, int, void).

  • Method Details

    • methodCall

      public static SyntheticMethodCallBuilder methodCall()
      Create a builder for a method call.
      Returns:
      A new builder configured for method calls
    • constructorCall

      public static SyntheticMethodCallBuilder constructorCall()
      Create a builder for a constructor call.
      Returns:
      A new builder configured for constructor calls
    • on

      public SyntheticMethodCallBuilder on(String target, String targetType)
      Set the target object and its type for an instance method call.

      The call will use invokevirtual unless traitCall() or onSuper(String) is used.

      Parameters:
      target - The variable to call the method on (e.g., "this", "param", a temp)
      targetType - The fully qualified type name of the target
      Returns:
      This builder
    • onSuper

      public SyntheticMethodCallBuilder onSuper(String superType)
      Set the target as "super" for a super method call.

      Super calls use invokespecial (not invokevirtual) to bypass virtual dispatch.

      Parameters:
      superType - The fully qualified type name of the super class
      Returns:
      This builder
    • onStatic

      public SyntheticMethodCallBuilder onStatic(String staticType)
      Set up for a static method call (no target object).
      Parameters:
      staticType - The fully qualified type name containing the static method
      Returns:
      This builder
    • type

      public SyntheticMethodCallBuilder type(String typeName)
      Set the type for a constructor call.
      Parameters:
      typeName - The fully qualified type name to construct
      Returns:
      This builder
    • method

      public SyntheticMethodCallBuilder method(String name)
      Set the method name to call.
      Parameters:
      name - The method name (e.g., "_eq", "_cmp", "_isSet")
      Returns:
      This builder
    • arg

      public SyntheticMethodCallBuilder arg(String argVar, String argType)
      Add a single argument to the method call.
      Parameters:
      argVar - The argument variable name
      argType - The fully qualified type of the argument
      Returns:
      This builder
    • args

      public SyntheticMethodCallBuilder args(List<String> argVars, List<String> argTypes)
      Add multiple arguments to the method call.
      Parameters:
      argVars - The argument variable names
      argTypes - The fully qualified types of the arguments
      Returns:
      This builder
    • returning

      public SyntheticMethodCallBuilder returning(String type)
      Set the return type of the method.
      Parameters:
      type - The fully qualified return type name
      Returns:
      This builder
    • into

      Set the variable to store the result into.
      Parameters:
      var - The result variable name
      Returns:
      This builder
    • traitCall

      public SyntheticMethodCallBuilder traitCall()
      Mark this as a trait call (uses invokeinterface instead of invokevirtual).
      Returns:
      This builder
    • build

      public List<IRInstr> build(SynthesisScope ctx)
      Build the IR instructions for this call using a SynthesisScope context.

      Generates CALL + RETAIN + SCOPE_REGISTER for non-primitive return types. For primitive return types (boolean, int), only CALL is generated.

      Parameters:
      ctx - The synthesis scope providing debugInfo and scopeId
      Returns:
      List of IR instructions for the call
    • build

      public List<IRInstr> build(DebugInfo debugInfo, String scopeId)
      Build the IR instructions for this call using explicit debugInfo and scopeId.
      Parameters:
      debugInfo - Debug information for the instructions
      scopeId - Current scope ID for memory management
      Returns:
      List of IR instructions for the call
    • buildVoid

      public List<IRInstr> buildVoid(SynthesisScope ctx)
      Build the IR instructions for a void method call using a SynthesisScope context.

      Equivalent to setting resultVar to null and building. No RETAIN or SCOPE_REGISTER is emitted.

      Parameters:
      ctx - The synthesis scope providing debugInfo and scopeId
      Returns:
      List of IR instructions for the void call