Class AggregateFactory

java.lang.Object
org.ek9lang.compiler.support.AggregateFactory

public class AggregateFactory extends Object
Support for taking one aggregate and manipulating the methods and the like to be applied to another aggregate. This is typically used in the phase 3 where we have one aggregate with a set of methods and what another aggregate to have those methods but in some cases we want to alter the return types via covariance. It also has more general uses in creating operators and methods for specific types or generic types of T. This is a factory of sorts, not in the pure OO sense; but still a factory.
  • Field Details

  • Constructor Details

    • AggregateFactory

      public AggregateFactory()
    • AggregateFactory

      public AggregateFactory(Ek9Types ek9Types)
  • Method Details

    • getEk9Types

      public Ek9Types getEk9Types()
    • addSyntheticConstructorIfRequired

      public void addSyntheticConstructorIfRequired(IAggregateSymbol aggregateSymbol)
      Add a synthetic constructor, if a constructor is not present.
    • addSyntheticConstructorIfRequired

      public void addSyntheticConstructorIfRequired(IAggregateSymbol aggregateSymbol, List<ISymbol> constructorArguments)
      Add a synthetic constructor, if a constructor is not present.
    • addConstructorIfRequired

      public void addConstructorIfRequired(IAggregateSymbol aggregateSymbol, List<ISymbol> constructorArguments, boolean synthetic)
      Add a new constructor if not present, marked as synthetic.
    • addNonAbstractMethods

      public void addNonAbstractMethods(IAggregateSymbol from, IAggregateSymbol to)
      Takes the 'from' aggregate obtains all the methods that are not abstract that have a return the type 'from' and adds them to the 'to' aggregate. It makes new constructors for each of the constructors the 'from' has. So take not note all methods get copied across only those with return type of 'from' which is then transformed in to return type 'to'. In general this method is used for defining types that extend another type, but we just want constructors and co-variance returns. Though it could be used in conjunction with other methods to alter the to aggregate.
      Parameters:
      from - The aggregate to get the methods from
      to - The aggregate to add the methods to.
    • cloneMethodWithNewType

      public MethodSymbol cloneMethodWithNewType(MethodSymbol method, IAggregateSymbol to)
    • addConstructor

      public MethodSymbol addConstructor(IAggregateSymbol t)
      Create a new constructor for the aggregate with no params.
      Parameters:
      t - The aggregate type to add the constructor to.
    • addConstructor

      public MethodSymbol addConstructor(AggregateSymbol t, Optional<ISymbol> s)
      Add another constructor to type t, but passing in an s as the value in the construction.
      Parameters:
      t - The aggregate type to add the constructor to.
      s - The argument - arg with a symbol type to be passed in as a construction parameter.
    • addConstructor

      public MethodSymbol addConstructor(AggregateSymbol t, ISymbol s)
      Add a constructor to the type aggregate with a particular parameter.
    • createGenericT

      public AggregateSymbol createGenericT(String name, IScope scope)
      Create a generic parameter of specific name. We use the name createGenericT, so it is obvious what we are doing here. These are used in generic classes/functions, and we provide a number of operators that a developer would reasonably expect. So our generic types and functions will compile and all will look OK. But when it comes to use with a concrete type we have to check the actual operators that are supported by those concrete types - this is done in the IR phase. Where are the generic type/functions get checked in the resolve phase.
      Parameters:
      name - - The name of the generic type parameter
      scope - - The scope it should go in.
    • updateToConstrainBy

      public void updateToConstrainBy(IAggregateSymbol t, IAggregateSymbol constrainingType)
      This is the idea where a 'T' is constrained to only be a type or a subtype of that type.
    • getAllPossibleDefaultOperators

      public List<MethodSymbol> getAllPossibleDefaultOperators(IAggregateSymbol aggregate)
      Provides all the possible default operators. Does not add them to the aggregate, but does create them with the aggregate as the enclosing scope.
    • getDefaultOperator

      public Optional<MethodSymbol> getDefaultOperator(IAggregateSymbol aggregate, String operator)
      If the operator provided can be defaulted then a Method symbol with the correct signature will be returned. Otherwise, the return Optional will be empty (normally indicating an error).
    • addAllSyntheticOperators

      public void addAllSyntheticOperators(IAggregateSymbol t)
      Add all possible synthetic operators to the aggregate. This is useful for creation conceptual types like 'T'. Because the semantics of the operators is fixed and known it means it is possible to write code in a generic class that uses these operator. Only when the generic type is parameterized with types are the operators on those types checked for existence. If not present then errors of emitted.
    • addPublicMethod

      public MethodSymbol addPublicMethod(AggregateSymbol aggregate, String methodName, List<ISymbol> methodParameters, Optional<ISymbol> returnType)
      Just add a method to an aggregate with the name and parameters and return type. The methodParameters can be empty if there are none.
    • addEnumerationMethods

      public void addEnumerationMethods(AggregateSymbol enumerationSymbol)
      Adds all the appropriate methods for a type that is an 'Enumeration'. This allows a developer to use built-in methods on the 'Enumeration' type.
    • addPurePublicReturnSameTypeMethod

      public void addPurePublicReturnSameTypeMethod(IAggregateSymbol aggregate, String methodName)
    • createPurePublicReturnSameTypeMethod

      public MethodSymbol createPurePublicReturnSameTypeMethod(IAggregateSymbol aggregateSymbol, String methodName)
    • addComparatorOperator

      public void addComparatorOperator(IAggregateSymbol aggregateSymbol, IAggregateSymbol arg0, String comparatorType, Optional<ISymbol> returnType)
      Adds a form of a comparison operator.
    • addComparatorOperator

      public MethodSymbol addComparatorOperator(IAggregateSymbol aggregateSymbol, String comparatorType, Optional<ISymbol> returnType)
      Adds a form of a comparison operator.
    • createPureAcceptSameTypeOperatorAndReturnType

      public MethodSymbol createPureAcceptSameTypeOperatorAndReturnType(IAggregateSymbol aggregateSymbol, String comparatorType, Optional<ISymbol> returnType)
      Creates a pure method with an argument the same as the main type.
    • createPureArgumentOperatorAndReturnType

      public MethodSymbol createPureArgumentOperatorAndReturnType(IAggregateSymbol aggregateSymbol, IAggregateSymbol arg0, String comparatorType, Optional<ISymbol> returnType)
      Creates a pure method with an argument of a different type as the main type.
    • addPurePublicSimpleOperator

      public MethodSymbol addPurePublicSimpleOperator(IAggregateSymbol aggregateSymbol, String methodName, Optional<ISymbol> returnType)
      Adds a simple operator, that is 'pure' (no side effects) and accepts no parameters, but just returns a value.
    • createToJsonSimpleOperator

      public MethodSymbol createToJsonSimpleOperator(IAggregateSymbol aggregateSymbol)
      Create operator for json.
    • createPurePublicSimpleOperator

      public MethodSymbol createPurePublicSimpleOperator(IAggregateSymbol aggregateSymbol, String methodName, Optional<ISymbol> returnType)
      Just creates a public operator with the name specified.
    • createOperator

      public MethodSymbol createOperator(IAggregateSymbol aggregateSymbol, String operatorType, boolean isPure)
      Create an operator of the name supplied. The accepts a single argument of the same type as the aggregateSymbol It also returns a value that is the same as the aggregateSymbol.
    • createMutatorOperator

      public MethodSymbol createMutatorOperator(IAggregateSymbol aggregateSymbol, String operatorType)
      Create an operator of the name supplied. The accepts a single argument of the same type as the aggregateSymbol It also returns a value that is the same as the aggregateSymbol.
    • resolveVoid

      public Optional<ISymbol> resolveVoid(IScope scope)
      Resolve Void from cached ek9 types of full scope hierarchy resolution.
    • resolveBoolean

      public Optional<ISymbol> resolveBoolean(IScope scope)
      Resolve Boolean from cached ek9 types of full scope hierarchy resolution.
    • resolveInteger

      public Optional<ISymbol> resolveInteger(IScope scope)
      Resolve Integer from cached ek9 types of full scope hierarchy resolution.
    • resolveString

      public Optional<ISymbol> resolveString(IScope scope)
      Resolve String from cached ek9 types of full scope hierarchy resolution.
    • resolveJson

      public Optional<ISymbol> resolveJson(IScope scope)
      Resolve JSON from cached ek9 types of full scope hierarchy resolution.