<commit_before>package som.primitives;

import som.compiler.ClassBuilder.ClassDefinitionId;
import som.interpreter.nodes.ISpecialSend;
import som.interpreter.nodes.nary.UnaryExpressionNode;
import som.vmobjects.SAbstractObject;
import som.vmobjects.SClass;
import som.vmobjects.SObject.SImmutableObject;
import som.vmobjects.SObject.SMutableObject;
import som.vmobjects.SObjectWithoutFields;

import com.oracle.truffle.api.dsl.Specialization;


// This isn't a primitive anymore, because we do all this in the magic of the
//  primary factory methods, which are generated in assemblePrimaryFactoryMethod()
// TODO: figure out where we need to do that, or whether we could actually do it
// in the language
// @GenerateNodeFactory
//    @Primitive("instantiate:")
public abstract class NewObjectPrim extends UnaryExpressionNode implements ISpecialSend {
  private final ClassDefinitionId classId;

  public NewObjectPrim(final ClassDefinitionId classId) {
    this.classId = classId;
  }

  @Override
  public ClassDefinitionId getLexicalClass() {
    return classId;
  }

  @Override
  public boolean isSuperSend() { return false; }

  @Specialization(guards = {"receiver.hasFields()", "receiver.hasOnlyImmutableFields()" })
  public final SAbstractObject doClassWithOnlyImmutableFields(final SClass receiver) {
    return new SImmutableObject(receiver);
  }

  @Specialization(guards = {"receiver.hasFields()", "!receiver.hasOnlyImmutableFields()" })
  public final SAbstractObject doClassWithFields(final SClass receiver) {
    return new SMutableObject(receiver);
  }

  @Specialization(guards = "!receiver.hasFields()")
  public final SAbstractObject doClassWithoutFields(final SClass receiver) {
    return new SObjectWithoutFields(receiver);
  }
}<commit_msg>Cache the class that is to be instantiated to enable constant folding<commit_after>package som.primitives;

import som.compiler.ClassBuilder.ClassDefinitionId;
import som.interpreter.nodes.ISpecialSend;
import som.interpreter.nodes.nary.UnaryExpressionNode;
import som.vmobjects.SAbstractObject;
import som.vmobjects.SClass;
import som.vmobjects.SObject.SImmutableObject;
import som.vmobjects.SObject.SMutableObject;
import som.vmobjects.SObjectWithoutFields;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;


// This isn't a primitive anymore, because we do all this in the magic of the
//  primary factory methods, which are generated in assemblePrimaryFactoryMethod()
// TODO: figure out where we need to do that, or whether we could actually do it
// in the language
// @GenerateNodeFactory
//    @Primitive("instantiate:")
public abstract class NewObjectPrim extends UnaryExpressionNode implements ISpecialSend {
  private final ClassDefinitionId classId;

  public NewObjectPrim(final ClassDefinitionId classId) {
    this.classId = classId;
  }

  @Override
  public ClassDefinitionId getLexicalClass() {
    return classId;
  }

  @Override
  public boolean isSuperSend() { return false; }

  @Specialization(guards = {"cachedRcvr.hasFields()",
      "cachedRcvr.hasOnlyImmutableFields()" })
  public final SAbstractObject doClassWithOnlyImmutableFields(
      final SClass receiver, @Cached("receiver") final SClass cachedRcvr) {
    return new SImmutableObject(cachedRcvr);
  }

  @Specialization(guards = {"cachedRcvr.hasFields()", "!cachedRcvr.hasOnlyImmutableFields()" })
  public final SAbstractObject doClassWithFields(final SClass receiver,
      @Cached("receiver") final SClass cachedRcvr) {
    return new SMutableObject(cachedRcvr);
  }

  @Specialization(guards = "!cachedRcvr.hasFields()")
  public final SAbstractObject doClassWithoutFields(final SClass receiver,
      @Cached("receiver") final SClass cachedRcvr) {
    return new SObjectWithoutFields(cachedRcvr);
  }
}
{'fraction_non_alphanumeric': 0.08780231335436382, 'fraction_numerical': 0.0, 'mean_word_length': 5.427364864864865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3000432', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1084, 'n_words': 284}