Skip to content
Snippets Groups Projects
Commit c1135411 authored by Zbynek Slajchrt's avatar Zbynek Slajchrt
Browse files

Predef.typeName() “slow-path” method added

parent ae507190
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ package com.oracle.truffle.r.library.tools;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.singleElement;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.stringValue;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.typeName;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
......@@ -20,14 +21,11 @@ import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node.Child;
import com.oracle.truffle.r.nodes.builtin.CastBuilder;
import com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef;
import com.oracle.truffle.r.nodes.unary.TypeofNode;
import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RRuntime;
......@@ -39,14 +37,10 @@ public class ToolsText {
public abstract static class DoTabExpand extends RExternalBuiltinNode.Arg2 {
@Child private TypeofNode typeofNode = com.oracle.truffle.r.nodes.unary.TypeofNodeGen.create();
@Override
protected void createCasts(CastBuilder casts) {
Function<Object, String> argTypeName = arg -> typeofNode.execute(arg).getName();
casts.arg(0, "strings").defaultError(RError.NO_CALLER, RError.Message.MACRO_CAN_BE_APPLIED_TO, "STRING_ELT()", "character vector", argTypeName).mustNotBeNull().mustBe(stringValue());
casts.arg(1, "starts").defaultError(RError.NO_CALLER, RError.Message.MACRO_CAN_BE_APPLIED_TO, "INTEGER()", "integer", argTypeName).mustNotBeNull().mustBe(
casts.arg(0, "strings").defaultError(RError.NO_CALLER, RError.Message.MACRO_CAN_BE_APPLIED_TO, "STRING_ELT()", "character vector", typeName()).mustNotBeNull().mustBe(stringValue());
casts.arg(1, "starts").defaultError(RError.NO_CALLER, RError.Message.MACRO_CAN_BE_APPLIED_TO, "INTEGER()", "integer", typeName()).mustNotBeNull().mustBe(
Predef.integerValue()).asIntegerVector();
}
......
......@@ -27,8 +27,7 @@ import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.integerValue
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.logicalValue;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.size;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.toBoolean;
import java.util.function.Function;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.typeName;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
......@@ -37,9 +36,9 @@ import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.ValueProfile;
import com.oracle.truffle.r.nodes.builtin.CastBuilder;
import com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.nodes.unary.CastNode;
import com.oracle.truffle.r.nodes.unary.TypeofNode;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.RRuntime;
......@@ -57,8 +56,6 @@ public abstract class Quantifier extends RBuiltinNode {
private final BranchProfile trueBranch = BranchProfile.create();
private final BranchProfile falseBranch = BranchProfile.create();
@Child private TypeofNode typeofNode = com.oracle.truffle.r.nodes.unary.TypeofNodeGen.create();
@Children private final CastNode[] argCastNodes = new CastNode[MAX_CACHED_LENGTH];
private static final class ProfileCastNode extends CastNode {
......@@ -88,8 +85,7 @@ public abstract class Quantifier extends RBuiltinNode {
private void createArgCast(int index) {
CastBuilder argCastBuilder = new CastBuilder();
Function<Object, String> argTypeName = arg -> typeofNode.execute(arg).getName();
argCastBuilder.arg(0).allowNull().shouldBe(integerValue().or(logicalValue()).or(instanceOf(RAbstractVector.class).and(size(0))), RError.Message.COERCING_ARGUMENT, argTypeName,
argCastBuilder.arg(0).allowNull().shouldBe(integerValue().or(logicalValue()).or(instanceOf(RAbstractVector.class).and(size(0))), RError.Message.COERCING_ARGUMENT, typeName(),
"logical").asLogicalVector();
argCastNodes[index] = insert(new ProfileCastNode(argCastBuilder.getCasts()[0]));
}
......
......@@ -24,7 +24,9 @@ package com.oracle.truffle.r.nodes.builtin;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.r.nodes.builtin.casts.Filter;
import com.oracle.truffle.r.nodes.builtin.casts.Filter.AndFilter;
......@@ -73,6 +75,7 @@ import com.oracle.truffle.r.runtime.data.RLogicalVector;
import com.oracle.truffle.r.runtime.data.RMissing;
import com.oracle.truffle.r.runtime.data.RNull;
import com.oracle.truffle.r.runtime.data.RStringVector;
import com.oracle.truffle.r.runtime.data.RTypedValue;
import com.oracle.truffle.r.runtime.data.model.RAbstractComplexVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractIntVector;
......@@ -762,5 +765,17 @@ public final class CastBuilder {
public static <T> MapToValue<T, RList> emptyList() {
return new MapToValue<>(RDataFactory.createList());
}
/**
* The function returned by this method is typically used as an error message argument.
*
* @return a function returning the type name of its argument
*/
public static Function<Object, String> typeName() {
return arg -> {
CompilerAsserts.neverPartOfCompilation();
return ((RTypedValue) RRuntime.asAbstractVector(arg)).getRType().getName();
};
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment