Class DeclaredFunction<T extends org.antlr.v4.runtime.ParserRuleContext>

java.lang.Object
org.daiitech.naftah.builtin.lang.Declaration
org.daiitech.naftah.builtin.lang.DeclaredFunction<T>
Type Parameters:
T - the concrete type of body context

public final class DeclaredFunction<T extends org.antlr.v4.runtime.ParserRuleContext> extends Declaration
Represents a function declaration in the Naftah scripting language.

This class encapsulates the ANTLR parse-tree context of a function declaration and exposes its semantic components, including the function name, parameters, body, return type, and asynchronous modifier.

A DeclaredFunction is a concrete, immutable declaration with respect to its identity and scope; however, some derived properties (such as resolved parameter and return types) may be computed lazily during later compilation phases.

Author:
Chakib Daii
  • Field Details

    • originalContext

      private final NaftahParser.FunctionDeclarationContext originalContext
      The original ANTLR parse context for the function declaration.
    • name

      private final String name
      The name of the function.
    • implementationName

      private final String implementationName
      The name of the implementation.
    • async

      private final boolean async
      Indicates whether the function is declared as asynchronous.
    • parametersContext

      private final NaftahParser.ParameterDeclarationListContext parametersContext
      The parse context for the parameter declaration list.
    • body

      private final T extends org.antlr.v4.runtime.ParserRuleContext body
      The parse context for the function body block.
    • returnTypeContext

      private final NaftahParser.ReturnTypeContext returnTypeContext
      The parse context for the return type.
    • parameters

      private List<DeclaredParameter> parameters
      The list of parameters declared for the function.
    • returnType

      private JavaType returnType
      The resolved return type of the function.
  • Constructor Details

    • DeclaredFunction

      private DeclaredFunction(int depth, NaftahParser.FunctionDeclarationContext originalContext, String implementationName)
      Creates a DeclaredFunction from its parsed function declaration.

      This constructor initializes the semantic representation of a function using information extracted from the ANTLR parse tree. It captures the function's name, async modifier, parameters, return type, and body, while retaining the original parse context for later analysis or code generation.

      This constructor is private and is intended to be used by factory methods within this class.

      Parameters:
      depth - The lexical scope depth at which the function is declared.
      originalContext - The ANTLR parse-tree context representing the function declaration.
      implementationName - The internal or generated name used to identify this function during compilation or code generation.
  • Method Details

    • of

      public static <T extends org.antlr.v4.runtime.ParserRuleContext> DeclaredFunction<T> of(int depth, NaftahParser.FunctionDeclarationContext originalContext, String implementationName)
      Creates a DeclaredFunction from a parsed function declaration.

      This factory method constructs a semantic representation of a function using the provided ANTLR parse-tree context. It delegates initialization to the private constructor and associates the function with its lexical scope depth and internal implementation name.

      Parameters:
      depth - The lexical scope depth at which the function is declared.
      originalContext - The ANTLR parse-tree context representing the function declaration.
      implementationName - The internal or generated name used to identify this function during compilation or code generation.
      Returns:
      A newly created DeclaredFunction instance.
    • getOriginalContext

      public NaftahParser.FunctionDeclarationContext getOriginalContext()
      Returns the original function declaration context.
      Returns:
      the function declaration context
    • getName

      public String getName()
      Returns the name of the function.
      Returns:
      the function name
    • getImplementationName

      public String getImplementationName()
      Returns the internal implementation name of this function.

      This name is used internally (for example, during name mangling, linking, or code generation) and may differ from the function’s declared source-level name.

      Returns:
      The internal implementation name associated with this function.
    • isAsync

      public boolean isAsync()
      Returns whether this function is asynchronous.

      An asynchronous function executes in its own task and must be awaited if its result is required before proceeding.

      Returns:
      true if the function is asynchronous; false otherwise
    • getParametersContext

      public NaftahParser.ParameterDeclarationListContext getParametersContext()
      Returns the context for the parameter declaration list.
      Returns:
      the parameters context
    • getParameters

      public List<DeclaredParameter> getParameters()
      Returns the list of declared parameters.
      Returns:
      the list of parameters
    • setParameters

      public void setParameters(List<DeclaredParameter> parameters)
      Sets the list of declared parameters.
      Parameters:
      parameters - the parameters to set
    • getBody

      public T getBody()
      Returns the context for the function body block.
      Returns:
      the function body context
    • getReturnTypeContext

      public NaftahParser.ReturnTypeContext getReturnTypeContext()
      Returns the context for the return type.
      Returns:
      the return type context
    • getReturnType

      public JavaType getReturnType()
      Returns the resolved return type of the function.
      Returns:
      the return type
    • setReturnType

      public void setReturnType(JavaType returnType)
      Sets the resolved return type of the function.
      Parameters:
      returnType - the return type to set
    • toString

      public String toString()
      Returns a string representation of the declared function in Arabic.
      Overrides:
      toString in class Object
      Returns:
      a formatted string representation of the function