Operand.java

  1. package Hoot.Runtime.Values;

  2. import Hoot.Runtime.Faces.Named;
  3. import Hoot.Runtime.Faces.Selector;
  4. import Hoot.Runtime.Faces.Resulting;
  5. import Hoot.Runtime.Names.TypeName;
  6. import Hoot.Runtime.Emissions.Item;
  7. import Hoot.Runtime.Emissions.Emission;
  8. import static Hoot.Runtime.Behaviors.HootRegistry.*;
  9. import Hoot.Runtime.Emissions.NamedItem;

  10. /**
  11.  * An operand. Many methods get overridden by subclasses.
  12.  *
  13.  * @author nik <nikboyd@sonic.net>
  14.  * @see "Copyright 2010,2021 Nikolas S Boyd."
  15.  * @see "Permission is granted to copy this work provided this copyright statement is retained in all copies."
  16.  */
  17. public abstract class Operand extends NamedItem implements Resulting {

  18.     protected Operand(Item container) { super(container); }
  19.     protected Operand(NamedItem container) { super(container); }
  20.     public Operand cleanTerm() { this.clean(); return this; }
  21.     public Operand makeAssigned() { return this; }

  22.     protected boolean isResult = false;
  23.     public Operand makeResult() { this.isResult = true; return this; }
  24.     @Override public boolean isResult() { return this.isResult; }

  25.     public boolean hasCascades() { return false; }
  26.     public boolean hasOnlyValue() { return false; }

  27.     @Override public boolean isFramed() { return false; }
  28.     public boolean isLiteral() { return false; }
  29.     public boolean isConstruct() { return false; }
  30.     public boolean isMessage() { return false; }
  31.     public boolean isNest() { return false; }
  32. //    public boolean isReference() { return false; }

  33.     public boolean producesPredicate() { return false; }
  34.     public boolean throwsException() { return false; }
  35.     public boolean needsStatement() { return true; }
  36.     public boolean refersToMetaclass() { return false; }
  37.     public boolean receiverNeedsTerm() { return false; }
  38.     public boolean selfIsPrimary() { return false; }

  39.     public Named asReference() { return (Named)this; }
  40.     public TypeName typeResolver() { return RootType(); }
  41.     public Class resolvedType() { return RootClass(); }
  42.     public String resolvedTypeName() { return typeResolver().fullName(); }
  43.     public boolean resolvesToPrimitive() { return typeResolver().isPrimitiveType(); }

  44.     /**
  45.      * @return whether the receiver consumes an (operand)
  46.      * @param operand a potentially consumable operand
  47.      */
  48.     public boolean consumes(Operand operand) { return false; }

  49.     /**
  50.      * @return whether a (selector) can be optimized against this operand
  51.      * @param selector a method selector
  52.      */
  53.     public boolean optimizes(Selector selector) {
  54.         if (selector.isOptimized()) return true;
  55.         TypeName resolver = typeResolver();
  56.         if (RootType().matches(resolver)) return false;
  57.         if (BehaviorType().matches(resolver)) return false;
  58.         return true; }

  59.     @Override public Emission emitItem() { return emitOperand(); }
  60.     @Override public Emission emitOptimized() { return emitOperand(); }
  61.     @Override public Emission emitPrimitive() { return emitOperand(); }

  62.     public Emission emitTerm() { return emitTerm(emitOperand()); }
  63.     public Emission emitResult() { return emitResult(emitItem("Object"), emitOperand()); }
  64.     public Emission emitOperand() { return emitEmpty(); } // override this!
  65.     public Emission emitStatement() { return emitStatement(emitOperand()); }
  66.     public Emission emitBooleanTerm() { return emitCast(BooleanType().fullName(), emitOperand()); }
  67.     public Emission emitReceiver() { return (receiverNeedsTerm()) ? emitTerm() : emitOperand(); }

  68. } // Operand