diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.java index 9162e0ac2c3c57718b7e59c7796061fedb5d7f1c..bb328a40e236e90258c724bfd882acc0d39df6ca 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.java @@ -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(); } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Quantifier.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Quantifier.java index 1b88a32c14606a5165846fcb5e27fe0d7db53422..70ecc15e2ab29957db60aa4ba1c8cbb26e36ef38 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Quantifier.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Quantifier.java @@ -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])); } diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/CastBuilder.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/CastBuilder.java index 0f91874393b1e52bcd077ba9152f6b9ec2927d58..663081d1a9d6303b10f3cf3528ae04b665d103e1 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/CastBuilder.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/CastBuilder.java @@ -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(); + }; + } } }