Skip to content

Commit

Permalink
Generate BuiltinMethods from simple method and constructor signatures (
Browse files Browse the repository at this point in the history
…#3444)

A low-hanging fruit where we can automate the generation of many
@BuiltinMethod nodes simply from the runtime's methods signatures.
This change introduces another annotation, @Builtin, to distinguish from
@BuiltinType and @BuiltinMethod processing. @Builtin processing will
always be the first stage of processing and its output will be fed to
the latter.

Note that the return type of Array.length() is changed from `int` to
`long` because we probably don't want to add a ton of specializations
for the former (see comparator nodes for details) and it is fine to cast
it in a small number of places.

Progress is visible in the number of deleted hardcoded classes.

This is an incremental step towards #181499077.

# Important Notes
This process does not attempt to cover all cases. Not yet, at least.
We only handle simple methods and constructors (see removed `Array` boilerplate methods).
  • Loading branch information
hubertp authored May 12, 2022
1 parent 4f3a768 commit a2dae60
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 114 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@
- [Frgaal compiler integration to allow for latest Java constructs][3421]
- [Support for Chrome developer tools --inspect option][3432]
- [Move Builtin Types and Methods definitions to stdlib][3363]
- [Reduce boilerplate by generating BuiltinMethod nodes from simple method
signatures][3444]

[3227]: https://github.com/enso-org/enso/pull/3227
[3248]: https://github.com/enso-org/enso/pull/3248
Expand All @@ -216,6 +218,7 @@
[3421]: https://github.com/enso-org/enso/pull/3421
[3432]: https://github.com/enso-org/enso/pull/3432
[3363]: https://github.com/enso-org/enso/pull/3363
[3444]: https://github.com/enso-org/enso/pull/3444

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ lazy val searcher = project
lazy val `interpreter-dsl` = (project in file("lib/scala/interpreter-dsl"))
.settings(
version := "0.1",
frgaalJavaCompilerSetting,
libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
"org.netbeans.api" % "org-openide-util-lookup" % "RELEASE130"
Expand Down Expand Up @@ -1221,7 +1222,8 @@ lazy val runtime = (project in file("engine/runtime"))
.settings(
(Compile / javacOptions) ++= Seq(
"-s",
(Compile / sourceManaged).value.getAbsolutePath
(Compile / sourceManaged).value.getAbsolutePath,
"-Xlint:unchecked"
),
addCompilerPlugin(
"org.typelevel" %% "kind-projector" % kindProjectorVersion cross CrossVersion.full
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Object doAtomThis(VirtualFrame frame, Atom _this, Object that) {

Object runSort(Comparator<Object> compare, Array _this, Context context) {
doSort(_this.getItems(), compare);
LoopNode.reportLoopCount(this, _this.length());
LoopNode.reportLoopCount(this, (int) _this.length());
return context.getBuiltins().nothing().newInstance();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Object doWarning(Object _this, WithWarnings value, Array warnings) {
if (warnings.length() == 0) {
return value.getValue();
}
Warning[] warningsCast = new Warning[warnings.length()];
Warning[] warningsCast = new Warning[(int) warnings.length()];
System.arraycopy(warnings.getItems(), 0, warningsCast, 0, warningsCast.length);
return new WithWarnings(value.getValue(), warningsCast);
}
Expand All @@ -35,7 +35,7 @@ Object doOther(Object _this, Object value, Array warnings) {
if (warnings.length() == 0) {
return value;
}
Warning[] warningsCast = new Warning[warnings.length()];
Warning[] warningsCast = new Warning[(int) warnings.length()];
System.arraycopy(warnings.getItems(), 0, warningsCast, 0, warningsCast.length);
return new WithWarnings(value, warningsCast);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private Map<String, Map<String, Class<BuiltinRootNode>>> readBuiltinMethodsMetad
throw new CompilerError("Invalid builtin metadata in : " + line);
}
try {
@SuppressWarnings("unchecked")
Class<BuiltinRootNode> clazz = (Class<BuiltinRootNode>) Class.forName(builtinMeta[1]);
String builtinMethodOwner = builtinName[0];
String builtinMethodName = builtinName[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.Context;
import org.enso.interpreter.runtime.callable.UnresolvedConversion;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
Expand Down Expand Up @@ -37,6 +39,7 @@ public Array(Object... items) {
*
* @param size the size of the created array.
*/
@Builtin(pkg = "mutable", description = "Creates an uninitialized array of a given size.")
public Array(long size) {
this.items = new Object[(int) size];
}
Expand Down Expand Up @@ -72,8 +75,23 @@ public Object readArrayElement(long index) throws InvalidArrayIndexException {
}

/** @return the size of this array */
public int length() {
return this.items.length;
@Builtin(pkg = "mutable", description = "Returns the size of this array.")
public long length() {
return this.getItems().length;
}

/** @return an empty array */
@Builtin(pkg = "mutable", description = "Creates an empty Array")
public static Object empty() {
return new Array();
}

/** @return an identity array */
@Builtin(
pkg = "mutable",
description = "Identity on arrays, implemented for protocol completeness.")
public Object toArray() {
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.enso.interpreter.dsl;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** An annotation denoting a method that will auto-generate a BuiltinMethod node. */
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.SOURCE)
public @interface Builtin {
/** @return the name of the subpackage for the generated method node. */
String pkg() default "";

/** @return a custom name, by default it uses the name of the annotated element. */
String name() default "";

/** @return a short description of this method. */
String description() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.*;
Expand Down Expand Up @@ -76,9 +77,10 @@ public final boolean process(Set<? extends TypeElement> annotations, RoundEnviro
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
} finally {
cleanup();
}
cleanup();
return true;
} else {
return handleProcess(annotations, roundEnv);
Expand Down
Loading

0 comments on commit a2dae60

Please sign in to comment.