Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying builtins type system support #7344

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
import org.enso.interpreter.dsl.AcceptsError;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.type.Types;

@BuiltinMethod(
type = "Meta",
name = "get_qualified_type_name",
description = "Returns a qualified type name of the given value.",
autoRegister = false)
public class GetQualifiedTypeNameNode extends Node {
private @Child TypeOfNode typeOfNode = TypeOfNode.build();

Object execute(@AcceptsError Object value) {
var typeName = Types.getName(value);
if (typeName == null) {
return EnsoContext.get(this).getBuiltins().nothing();
var maybeType = switch (value) {
case Type type -> type;
default -> typeOfNode.execute(value);
};
if (maybeType instanceof Type type) {
return Text.create(type.getQualifiedName().toString());
}

return Text.create(typeName);
return EnsoContext.get(this).getBuiltins().nothing();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.node.expression.builtin.meta.TypeOfNode;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.type.Types;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.type.TypesGen;

@GenerateUncached
Expand Down Expand Up @@ -72,10 +73,11 @@ String doDisplay(
throw new IllegalStateException("Receiver declares a meta object, but does not return it.");
}
} else {
// In case we forgot to handle some of the builtin types, the following
// piece of code will handle that.
String typeName = Types.getName(value);
return typeName != null ? typeName : "a polyglot object";
if (TypeOfNode.getUncached().execute(value) instanceof Type type) {
return type.getQualifiedName().toString();
} else {
return "a polyglot object";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.interpreter.runtime.builtin;

import com.oracle.truffle.api.CompilerDirectives;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -17,8 +16,9 @@
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.enso.compiler.context.CompilerContext;

import org.enso.compiler.Passes;
import org.enso.compiler.context.CompilerContext;
import org.enso.compiler.context.FreshNameSupply;
import org.enso.compiler.core.CompilerError;
import org.enso.compiler.phase.BuiltinsIrBuilder;
Expand Down Expand Up @@ -50,13 +50,13 @@
import org.enso.interpreter.runtime.builtin.Error;
import org.enso.interpreter.runtime.builtin.Number;
import org.enso.interpreter.runtime.builtin.System;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.type.TypesFromProxy;
import org.enso.pkg.QualifiedName;

import com.oracle.truffle.api.CompilerDirectives;

/** Container class for static predefined atoms, methods, and their containing scope. */
public final class Builtins {

Expand Down Expand Up @@ -636,17 +636,6 @@ public Module getModule() {
return module;
}

/**
* Convert from type-system type names to types.
*
* @param typeName the fully qualified type name of a builtin
* @return the associated {@link Atom} if it exists,
* and {@code null} otherwise
*/
public Type fromTypeSystem(String typeName) {
return TypesFromProxy.fromTypeSystem(this, typeName);
}

private record LoadedBuiltinMethod(Method meth, boolean isStatic, boolean isAutoRegister) {
Optional<BuiltinFunction> toFunction(EnsoLanguage language, boolean isStaticInstance) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.data.*;
import org.enso.interpreter.runtime.data.Array;
import org.enso.interpreter.runtime.data.ArrayOverBuffer;
import org.enso.interpreter.runtime.data.ArrayProxy;
import org.enso.interpreter.runtime.data.EnsoDate;
import org.enso.interpreter.runtime.data.EnsoDateTime;
import org.enso.interpreter.runtime.data.EnsoDuration;
import org.enso.interpreter.runtime.data.EnsoFile;
import org.enso.interpreter.runtime.data.EnsoTimeOfDay;
import org.enso.interpreter.runtime.data.EnsoTimeZone;
import org.enso.interpreter.runtime.data.ManagedResource;
import org.enso.interpreter.runtime.data.Ref;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.data.Vector;
import org.enso.interpreter.runtime.data.hash.EnsoHashMap;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.error.*;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.error.PanicSentinel;
import org.enso.interpreter.runtime.error.Warning;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.polyglot.data.TypeGraph;
Expand Down Expand Up @@ -107,68 +122,6 @@ public static void extractArguments(Object[] arguments) throws ArityException {
}
}

/**
* Return a type of the given object as a string.
*
* @param value an object of interest.
* @return the string representation of object's type.
*/
public static String getName(Object value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if (TypesGen.isLong(value) || TypesGen.isEnsoBigInteger(value)) {
return ConstantsGen.INTEGER;
} else if (TypesGen.isDouble(value)) {
return ConstantsGen.DECIMAL;
} else if (TypesGen.isBoolean(value)) {
return ConstantsGen.BOOLEAN;
} else if (TypesGen.isText(value)) {
return ConstantsGen.TEXT;
} else if (TypesGen.isFunction(value)) {
return ConstantsGen.FUNCTION;
} else if (value instanceof Atom atom) {
return atom.getConstructor().getQualifiedTypeName().toString();
} else if (value instanceof AtomConstructor cons) {
return cons.getQualifiedName().toString();
} else if (value instanceof Type t) {
return t.getQualifiedName().toString();
} else if (TypesGen.isDataflowError(value)) {
return ConstantsGen.ERROR;
} else if (TypesGen.isUnresolvedSymbol(value) || TypesGen.isUnresolvedConversion(value)) {
return Constants.UNRESOLVED_SYMBOL;
} else if (TypesGen.isManagedResource(value)) {
return ConstantsGen.MANAGED_RESOURCE;
} else if (TypesGen.isArray(value) || TypesGen.isArrayOverBuffer(value) || TypesGen.isArrayProxy(value)) {
return ConstantsGen.ARRAY;
} else if (TypesGen.isVector(value)) {
return ConstantsGen.VECTOR;
} else if (TypesGen.isEnsoDate(value)) {
return ConstantsGen.DATE;
} else if (TypesGen.isEnsoDateTime(value)) {
return ConstantsGen.DATE_TIME;
} else if (TypesGen.isEnsoTimeOfDay(value)) {
return ConstantsGen.TIME_OF_DAY;
} else if (TypesGen.isEnsoDuration(value)) {
return ConstantsGen.DURATION;
} else if (TypesGen.isEnsoTimeZone(value)) {
return ConstantsGen.TIME_ZONE;
} else if (TypesGen.isEnsoFile(value)) {
return ConstantsGen.FILE;
} else if (TypesGen.isModuleScope(value)) {
return Constants.MODULE_SCOPE;
} else if (TypesGen.isRef(value)) {
return ConstantsGen.REF;
} else if (TypesGen.isPanicException(value)) {
return ConstantsGen.PANIC;
} else if (TypesGen.isPanicSentinel(value)) {
return ConstantsGen.PANIC;
} else if (TypesGen.isWarning(value)) {
return ConstantsGen.WARNING;
} else if (value instanceof WithWarnings) {
return getName(((WithWarnings) value).getValue());
} else {
return null;
}
}

/** Check if the given type is a panic. */
public static boolean isPanic(String typeName) {
return ConstantsGen.PANIC.equals(typeName);
Expand Down Expand Up @@ -227,9 +180,7 @@ public static <A, B> Pair<A, B> extractArguments(Object[] arguments, Class<A> cl
return new Pair<>((A) arguments[0], (B) arguments[1]);
}

/**
* @return the language type hierarchy
*/
/** @return the language type hierarchy */
public static TypeGraph getTypeHierarchy() {
return typeHierarchy;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.processing.Filer;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
Expand Down Expand Up @@ -474,7 +475,7 @@ private void generateCheckedArgumentRead(
out.println(" var builtins = EnsoContext.get(bodyNode).getBuiltins();");
out.println(" var ensoTypeName = org.enso.interpreter.runtime.type.ConstantsGen.getEnsoTypeName(\"" + builtinName + "\");");
out.println(" var error = (ensoTypeName != null)");
out.println(" ? builtins.error().makeTypeError(builtins.fromTypeSystem(ensoTypeName), arguments[arg"
out.println(" ? builtins.error().makeTypeError(ensoTypeName, arguments[arg"
+ arg.getPosition()
+ "Idx], \""
+ varName
Expand Down