diff --git a/syntax/jvm/src/main/scala/chester/syntax/core/truffle.scala b/syntax/jvm/src/main/scala/chester/syntax/core/truffle.scala index 1bee00a9..39ea37c8 100644 --- a/syntax/jvm/src/main/scala/chester/syntax/core/truffle.scala +++ b/syntax/jvm/src/main/scala/chester/syntax/core/truffle.scala @@ -17,36 +17,36 @@ import scala.collection.immutable.HashMap object truffle { type ExecuteGeneric = (VirtualFrame, Term) => Object var globalExecuteGeneric: ExecuteGeneric = (_: VirtualFrame, _: Term) => ??? - sealed trait Term extends com.oracle.truffle.api.nodes.Node with TermT[Term] derives ReadWriter { + sealed trait Term extends com.oracle.truffle.api.nodes.Node with TermT[Term] derives ReadWriter { type ThisTree <: Term final def executeGeneric(frame: VirtualFrame): Object = globalExecuteGeneric(frame, this) } case class CallingArgTerm( - value: Term, - ty: Term, - name: Option[Name] = None, - vararg: Boolean = false, - meta: OptionTermMeta - ) extends WHNF - with CallingArgTermC[Term] derives ReadWriter { + @child value: Term, + @child ty: Term, + name: Option[Name] = None, + vararg: Boolean = false, + meta: OptionTermMeta + ) extends WHNF + with CallingArgTermC[Term] derives ReadWriter { override def cons: CallingArgTermF[Term, ThisTree] = this.copy override type ThisTree = CallingArgTerm } case class Calling( - args: Vector[CallingArgTerm], - implicitly: Boolean = false, - meta: OptionTermMeta - ) extends WHNF - with CallingC[Term] derives ReadWriter { + args: Vector[CallingArgTerm], + implicitly: Boolean = false, + meta: OptionTermMeta + ) extends WHNF + with CallingC[Term] derives ReadWriter { override type ThisTree = Calling override def cons: CallingF[Term, ThisTree] = this.copy } case class FCallTerm( - f: Term, - args: Vector[Calling], - meta: OptionTermMeta - ) extends WHNF - with FCallTermC[Term] { + @child f: Term, + args: Vector[Calling], + meta: OptionTermMeta + ) extends WHNF + with FCallTermC[Term] { override type ThisTree = FCallTerm override def cons: FCallTermF[Term, ThisTree] = this.copy } @@ -54,11 +54,11 @@ object truffle { override type ThisTree <: Pat } case class Bind( - bind: LocalV, - ty: Term, - meta: OptionTermMeta - ) extends Pat - with BindC[Term] { + @child bind: LocalV, + @child ty: Term, + meta: OptionTermMeta + ) extends Pat + with BindC[Term] { override type ThisTree = Bind override def cons: BindF[Term, ThisTree] = this.copy } @@ -80,7 +80,7 @@ object truffle { case class MetaTerm(impl: HoldNotReadable[?], meta: OptionTermMeta) extends Term with MetaTermC[Term] with EffectsM with SpecialTerm { override type ThisTree = MetaTerm } - case class ListTerm(terms: Vector[Term], meta: OptionTermMeta) extends Term with ListTermC[Term] with WHNF derives ReadWriter { + case class ListTerm(@child terms: Vector[Term], meta: OptionTermMeta) extends Term with ListTermC[Term] with WHNF derives ReadWriter { override final type ThisTree = ListTerm override def cons: ListTermF[Term, ThisTree] = this.copy } @@ -92,7 +92,7 @@ object truffle { def level: Term } given aTypeF: TypeF[Term, Type] = Type(Level0, meta = None).cons - case class Type(level: Term, meta: OptionTermMeta) extends Sort with TypeC[Term] { + case class Type(@child level: Term, meta: OptionTermMeta) extends Sort with TypeC[Term] { override type ThisTree = Type override def cons: TypeF[Term, ThisTree] = this.copy } @@ -104,7 +104,7 @@ object truffle { type ThisTree <: Level } given aLevelFiniteF: LevelFiniteF[Term, LevelFinite] = LevelFinite(IntegerTerm(0, meta = None), meta = None).cons - case class LevelFinite(n: Term, meta: OptionTermMeta) extends Level with LevelFiniteC[Term] { + case class LevelFinite(@child n: Term, meta: OptionTermMeta) extends Level with LevelFiniteC[Term] { override type ThisTree = LevelFinite override def cons: LevelFiniteF[Term, ThisTree] = this.copy } @@ -112,12 +112,12 @@ object truffle { override type ThisTree = LevelUnrestricted override def cons: LevelUnrestrictedF[Term, ThisTree] = this.copy } - case class Prop(level: Term, meta: OptionTermMeta) extends Sort with PropC[Term] { + case class Prop(@child level: Term, meta: OptionTermMeta) extends Sort with PropC[Term] { override type ThisTree = Prop override def cons: PropF[Term, ThisTree] = this.copy } // fibrant types - case class FType(level: Term, meta: OptionTermMeta) extends Sort with FTypeC[Term] { + case class FType(@child level: Term, meta: OptionTermMeta) extends Sort with FTypeC[Term] { override type ThisTree = FType override def cons: FTypeF[Term, ThisTree] = this.copy } @@ -191,7 +191,7 @@ object truffle { override type ThisTree = SymbolType override def cons: SymbolTypeF[Term, ThisTree] = this.copy } - case class AnyType(level: Term, meta: OptionTermMeta) extends TypeTerm with AnyTypeC[Term] derives ReadWriter { + case class AnyType(@child level: Term, meta: OptionTermMeta) extends TypeTerm with AnyTypeC[Term] derives ReadWriter { override type ThisTree = AnyType override def cons: AnyTypeF[Term, ThisTree] = this.copy } @@ -201,75 +201,75 @@ object truffle { override def cons: NothingTypeF[Term, ThisTree] = this.copy } case class LiteralType( - literal: LiteralTerm, - meta: OptionTermMeta - ) extends TypeTerm - with LiteralTypeC[Term] { + @child literal: LiteralTerm, + meta: OptionTermMeta + ) extends TypeTerm + with LiteralTypeC[Term] { override type ThisTree = LiteralType override def cons: LiteralTypeF[Term, ThisTree] = this.copy } case class ArgTerm( - bind: LocalV, - ty: Term, - default: Option[Term] = None, - vararg: Boolean = false, - meta: OptionTermMeta - ) extends WHNF - with ArgTermC[Term] { + @child bind: LocalV, + @child ty: Term, + default: Option[Term] = None, + vararg: Boolean = false, + meta: OptionTermMeta + ) extends WHNF + with ArgTermC[Term] { override type ThisTree = ArgTerm override def cons: ArgTermF[Term, ThisTree] = this.copy } case class TelescopeTerm( - args: Vector[ArgTerm], - implicitly: Boolean = false, - meta: OptionTermMeta - ) extends WHNF - with TelescopeTermC[Term] { + args: Vector[ArgTerm], + implicitly: Boolean = false, + meta: OptionTermMeta + ) extends WHNF + with TelescopeTermC[Term] { override type ThisTree = TelescopeTerm override def cons: TelescopeTermF[Term, ThisTree] = this.copy } case class Function( - ty: FunctionType, - body: Term, - meta: OptionTermMeta - ) extends WHNF - with FunctionC[Term] { + @child ty: FunctionType, + @child body: Term, + meta: OptionTermMeta + ) extends WHNF + with FunctionC[Term] { override type ThisTree = Function override def cons: FunctionF[Term, ThisTree] = this.copy } case class FunctionType( - telescope: Vector[TelescopeTerm], - resultTy: Term, - effects: EffectsM = NoEffect, - meta: OptionTermMeta - ) extends WHNF - with FunctionTypeC[Term] { + telescope: Vector[TelescopeTerm], + @child resultTy: Term, + effects: EffectsM = NoEffect, + meta: OptionTermMeta + ) extends WHNF + with FunctionTypeC[Term] { override type ThisTree = FunctionType override def cons: FunctionTypeF[Term, ThisTree] = this.copy } case class ObjectClauseValueTerm( - key: Term, - value: Term, - meta: OptionTermMeta - ) extends WHNF - with ObjectClauseValueTermC[Term] derives ReadWriter { + @child key: Term, + @child value: Term, + meta: OptionTermMeta + ) extends WHNF + with ObjectClauseValueTermC[Term] derives ReadWriter { override type ThisTree = ObjectClauseValueTerm override def cons: ObjectClauseValueTermF[Term, ThisTree] = this.copy } case class ObjectTerm( - clauses: Vector[ObjectClauseValueTerm], - meta: OptionTermMeta - ) extends WHNF - with ObjectTermC[Term] { + clauses: Vector[ObjectClauseValueTerm], + meta: OptionTermMeta + ) extends WHNF + with ObjectTermC[Term] { override type ThisTree = ObjectTerm override def cons: ObjectTermF[Term, ThisTree] = this.copy } case class ObjectType( - fieldTypes: Vector[ObjectClauseValueTerm], - exactFields: Boolean = false, - meta: OptionTermMeta - ) extends WHNF - with ObjectTypeC[Term] { + fieldTypes: Vector[ObjectClauseValueTerm], + exactFields: Boolean = false, + meta: OptionTermMeta + ) extends WHNF + with ObjectTypeC[Term] { override type ThisTree = ObjectType override def cons: ObjectTypeF[Term, ThisTree] = this.copy } @@ -280,7 +280,7 @@ object truffle { sealed trait Constructed extends WHNF with ConstructedT[Term] derives ReadWriter { type ThisTree <: Constructed } - case class ListType(ty: Term, meta: OptionTermMeta) extends Constructed with ListTypeC[Term] with TypeTerm { + case class ListType(@child ty: Term, meta: OptionTermMeta) extends Constructed with ListTypeC[Term] with TypeTerm { override type ThisTree = ListType override def cons: ListTypeF[Term, ThisTree] = this.copy } @@ -299,7 +299,7 @@ object truffle { override type ThisTree <: Effect } case class Effects(effectss: Map[LocalV, Term] = HashMap.empty, meta: OptionTermMeta) extends WHNF with EffectsM with EffectsC[Term] - derives ReadWriter { + derives ReadWriter { override def effects = effectss override type ThisTree = Effects override def cons: EffectsF[Term, ThisTree] = this.copy @@ -312,22 +312,22 @@ object truffle { override type ThisTree <: ReferenceCall } case class LocalV( - name: Name, - ty: Term, - uniqId: UniqidOf[LocalV], - meta: OptionTermMeta - ) extends ReferenceCall - with LocalVC[Term] { + name: Name, + @child ty: Term, + uniqId: UniqidOf[LocalV], + meta: OptionTermMeta + ) extends ReferenceCall + with LocalVC[Term] { override type ThisTree = LocalV override def cons: LocalVF[Term, ThisTree] = this.copy } case class ToplevelV( - id: AbsoluteRef, - ty: Term, - uniqId: UniqidOf[ToplevelV], - meta: OptionTermMeta - ) extends ReferenceCall - with ToplevelVC[Term] { + id: AbsoluteRef, + @child ty: Term, + uniqId: UniqidOf[ToplevelV], + meta: OptionTermMeta + ) extends ReferenceCall + with ToplevelVC[Term] { override type ThisTree = ToplevelV override def cons: ToplevelVF[Term, ThisTree] = this.copy } @@ -338,35 +338,35 @@ object truffle { override type ThisTree <: StmtTerm } case class LetStmtTerm( - localv: LocalV, - value: Term, - ty: Term, - meta: OptionTermMeta - ) extends StmtTerm - with LetStmtTermC[Term] { + localv: LocalV, + @child value: Term, + @child ty: Term, + meta: OptionTermMeta + ) extends StmtTerm + with LetStmtTermC[Term] { override type ThisTree = LetStmtTerm override def cons: LetStmtTermF[Term, ThisTree] = this.copy } case class DefStmtTerm( - localv: LocalV, - value: Term, - ty: Term, - meta: OptionTermMeta - ) extends StmtTerm - with DefStmtTermC[Term] { + @child localv: LocalV, + @child value: Term, + @child ty: Term, + meta: OptionTermMeta + ) extends StmtTerm + with DefStmtTermC[Term] { override type ThisTree = DefStmtTerm override def cons: DefStmtTermF[Term, ThisTree] = this.copy } case class ExprStmtTerm( - expr: Term, - ty: Term = AnyType0, - meta: OptionTermMeta - ) extends StmtTerm - with ExprStmtTermC[Term] { + @child expr: Term, + @child ty: Term = AnyType0, + meta: OptionTermMeta + ) extends StmtTerm + with ExprStmtTermC[Term] { override type ThisTree = ExprStmtTerm override def cons: ExprStmtTermF[Term, ThisTree] = this.copy } - case class NonlocalOrLocalReturn(value: Term, meta: OptionTermMeta) extends StmtTerm with NonlocalOrLocalReturnC[Term] { + case class NonlocalOrLocalReturn(@child value: Term, meta: OptionTermMeta) extends StmtTerm with NonlocalOrLocalReturnC[Term] { override type ThisTree = NonlocalOrLocalReturn override def cons: NonlocalOrLocalReturnF[Term, ThisTree] = this.copy } @@ -379,99 +379,99 @@ object truffle { override def cons: TupleTermF[Term, ThisTree] = this.copy } case class BlockTerm( - statements: Vector[StmtTerm], - result: Term, - meta: OptionTermMeta - ) extends Uneval - with BlockTermC[Term] derives ReadWriter { + statements: Vector[StmtTerm], + @child result: Term, + meta: OptionTermMeta + ) extends Uneval + with BlockTermC[Term] derives ReadWriter { override type ThisTree = BlockTerm override def cons: BlockTermF[Term, ThisTree] = this.copy } case class Annotation( - term: Term, - ty: Option[Term], - effects: Option[EffectsM], - meta: OptionTermMeta - ) extends Uneval - with AnnotationC[Term] { + @child term: Term, + ty: Option[Term], + effects: Option[EffectsM], + meta: OptionTermMeta + ) extends Uneval + with AnnotationC[Term] { override type ThisTree = Annotation override def cons: AnnotationF[Term, ThisTree] = this.copy } case class FieldTerm( - name: Name, - ty: Term, - meta: OptionTermMeta - ) extends WHNF - with FieldTermC[Term] derives ReadWriter { + name: Name, + @child ty: Term, + meta: OptionTermMeta + ) extends WHNF + with FieldTermC[Term] derives ReadWriter { override type ThisTree = FieldTerm override def cons: FieldTermF[Term, ThisTree] = this.copy } case class RecordStmtTerm( - name: Name, - uniqId: UniqidOf[RecordStmtTerm] = Uniqid.generate[RecordStmtTerm], - fields: Vector[FieldTerm], - body: Option[BlockTerm], - meta: OptionTermMeta - ) extends TypeDefinition - with RecordStmtTermC[Term] { + name: Name, + uniqId: UniqidOf[RecordStmtTerm] = Uniqid.generate[RecordStmtTerm], + fields: Vector[FieldTerm], + body: Option[BlockTerm], + meta: OptionTermMeta + ) extends TypeDefinition + with RecordStmtTermC[Term] { override type ThisTree = RecordStmtTerm override def cons: RecordStmtTermF[Term, ThisTree] = this.copy } case class RecordConstructorCallTerm( - recordName: Name, - args: Vector[Term], - meta: OptionTermMeta - ) extends Uneval - with RecordConstructorCallTermC[Term] { + recordName: Name, + args: Vector[Term], + meta: OptionTermMeta + ) extends Uneval + with RecordConstructorCallTermC[Term] { override type ThisTree = RecordConstructorCallTerm override def cons: RecordConstructorCallTermF[Term, ThisTree] = this.copy } case class TraitStmtTerm( - name: Name, - uniqId: UniqidOf[TraitStmtTerm] = Uniqid.generate[TraitStmtTerm], - extendsClause: Option[Term] = None, - body: Option[BlockTerm] = None, - meta: OptionTermMeta - ) extends TypeDefinition - with TraitStmtTermC[Term] derives ReadWriter { + name: Name, + uniqId: UniqidOf[TraitStmtTerm] = Uniqid.generate[TraitStmtTerm], + extendsClause: Option[Term] = None, + body: Option[BlockTerm] = None, + meta: OptionTermMeta + ) extends TypeDefinition + with TraitStmtTermC[Term] derives ReadWriter { override type ThisTree = TraitStmtTerm override def cons: TraitStmtTermF[Term, ThisTree] = this.copy } case class InterfaceStmtTerm( - name: Name, - uniqId: UniqidOf[InterfaceStmtTerm] = Uniqid.generate[InterfaceStmtTerm], - extendsClause: Option[Term] = None, - body: Option[BlockTerm] = None, - meta: OptionTermMeta - ) extends TypeDefinition - with InterfaceStmtTermC[Term] derives ReadWriter { + name: Name, + uniqId: UniqidOf[InterfaceStmtTerm] = Uniqid.generate[InterfaceStmtTerm], + extendsClause: Option[Term] = None, + body: Option[BlockTerm] = None, + meta: OptionTermMeta + ) extends TypeDefinition + with InterfaceStmtTermC[Term] derives ReadWriter { override type ThisTree = InterfaceStmtTerm override def cons: InterfaceStmtTermF[Term, ThisTree] = this.copy } case class ObjectCallTerm( - objectRef: Term, - meta: OptionTermMeta - ) extends Uneval - with ObjectCallTermC[Term] { + objectRef: Term, + meta: OptionTermMeta + ) extends Uneval + with ObjectCallTermC[Term] { override type ThisTree = ObjectCallTerm override def cons: ObjectCallTermF[Term, ThisTree] = this.copy } case class ObjectTypeTerm( - objectDef: ObjectStmtTerm, - meta: OptionTermMeta - ) extends TypeTerm - with ObjectTypeTermC[Term] { + @child objectDef: ObjectStmtTerm, + meta: OptionTermMeta + ) extends TypeTerm + with ObjectTypeTermC[Term] { override type ThisTree = ObjectTypeTerm override def cons: ObjectTypeTermF[Term, ThisTree] = this.copy } case class ObjectStmtTerm( - name: Name, - uniqId: UniqidOf[ObjectStmtTerm], - extendsClause: Option[Term], - body: Option[BlockTerm], - meta: OptionTermMeta - ) extends TypeDefinition - with ObjectStmtTermC[Term] derives ReadWriter { + name: Name, + uniqId: UniqidOf[ObjectStmtTerm], + extendsClause: Option[Term], + body: Option[BlockTerm], + meta: OptionTermMeta + ) extends TypeDefinition + with ObjectStmtTermC[Term] derives ReadWriter { override type ThisTree = ObjectStmtTerm override def cons: ObjectStmtTermF[Term, ThisTree] = this.copy } diff --git a/syntax/jvm/src/main/scala/chester/syntax/core/truffleutils.scala b/syntax/jvm/src/main/scala/chester/syntax/core/truffleutils.scala new file mode 100644 index 00000000..3021f340 --- /dev/null +++ b/syntax/jvm/src/main/scala/chester/syntax/core/truffleutils.scala @@ -0,0 +1,12 @@ +package chester.syntax.core + +import com.oracle.truffle.api.nodes.Node.Child + +import scala.annotation.meta.field + +// https://github.com/b-studios/scala-graal-truffle-example/blob/c2747a6eece156f878c5b934116aaa00a2cd6311/src/main/scala/effekt/Test.scala#L79 +// We define our own alias, using Scala's meta-annotation +// (https://www.scala-lang.org/api/current/scala/annotation/meta/index.html) +// This is necessary to make sure the *field* is annotated, not the getters +// or setters. +type child = Child @field diff --git a/syntax/shared/src/main/scala/chester/syntax/core/utils.scala b/syntax/shared/src/main/scala/chester/syntax/core/utils.scala index f2ce83b1..d7b27a35 100644 --- a/syntax/shared/src/main/scala/chester/syntax/core/utils.scala +++ b/syntax/shared/src/main/scala/chester/syntax/core/utils.scala @@ -1,6 +1,5 @@ package chester.syntax.core - import scala.language.implicitConversions import cats.data.* import chester.syntax.*