Skip to content
Snippets Groups Projects
Commit fe5d7a29 authored by Adam Welc's avatar Adam Welc
Browse files

Rewritten parameter casts for make.unique builtin.

parent 5a59ff8f
Branches
No related tags found
No related merge requests found
......@@ -22,19 +22,19 @@
*/
package com.oracle.truffle.r.nodes.builtin.base;
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.*;
import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE;
import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.INTERNAL;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.r.nodes.builtin.CastBuilder;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.builtins.RBuiltin;
import com.oracle.truffle.r.runtime.data.RString;
import com.oracle.truffle.r.runtime.data.RStringVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
import com.oracle.truffle.r.runtime.data.model.RAbstractVector;
import com.oracle.truffle.r.runtime.ops.na.NACheck;
@RBuiltin(name = "make.unique", kind = INTERNAL, parameterNames = {"names", "sep"}, behavior = PURE)
......@@ -44,6 +44,13 @@ public abstract class MakeUnique extends RBuiltinNode {
private final ConditionProfile duplicatesProfile = ConditionProfile.createBinaryProfile();
private final NACheck dummyCheck = NACheck.create(); // never triggered (used for vector update)
@Override
protected void createCasts(CastBuilder casts) {
casts.arg("names").mustBe(stringValue(), RError.SHOW_CALLER, RError.Message.NOT_CHARACTER_VECTOR, "names");
casts.arg("sep").mustBe(stringValue(), RError.SHOW_CALLER, RError.Message.MUST_BE_STRING, "sep").asStringVector().mustBe(size(1), RError.SHOW_CALLER, RError.Message.MUST_BE_STRING,
"sep").findFirst();
}
@Specialization
protected RAbstractStringVector makeUnique(RAbstractStringVector names, String sep) {
if (namesProfile.profile(names.getLength() == 0 || names.getLength() == 1)) {
......@@ -91,28 +98,4 @@ public abstract class MakeUnique extends RBuiltinNode {
return s1 + sep + index;
}
@Specialization(guards = "sepIsString(sep)")
protected RAbstractStringVector makeUnique(RAbstractStringVector names, RAbstractVector sep) {
return makeUnique(names, (String) sep.getDataAtAsObject(0));
}
@SuppressWarnings("unused")
@Specialization(guards = "!sepIsString(sep)")
protected RAbstractStringVector makeUniqueWrongSep(RAbstractStringVector names, RAbstractVector sep) {
throw RError.error(this, RError.Message.MUST_BE_STRING, "sep");
}
@SuppressWarnings("unused")
@Specialization(guards = "!namesIsStringVector(names)")
protected RAbstractStringVector makeUnique(RAbstractVector names, Object sep) {
throw RError.error(this, RError.Message.NOT_CHARACTER_VECTOR, "names");
}
protected boolean namesIsStringVector(RAbstractVector names) {
return names.getElementClass() == RString.class;
}
protected boolean sepIsString(RAbstractVector sep) {
return sep.getElementClass() == RString.class && sep.getLength() == 1;
}
}
......@@ -44,8 +44,16 @@ public class TestBuiltin_makeunique extends TestBase {
assertEval("{ make.unique(c(\"a\", \"a\")) }");
assertEval("{ make.unique(c(\"a\", \"a\", \"a\")) }");
assertEval("{ make.unique(c(\"a\", \"a\"), \"_\") }");
assertEval(Output.IgnoreErrorContext, "{ make.unique(1) }");
assertEval(Output.IgnoreErrorContext, "{ make.unique(\"a\", 1) }");
assertEval(Output.IgnoreErrorContext, "{ make.unique(\"a\", character()) }");
assertEval("{ make.unique(1) }");
assertEval("{ make.unique(\"a\", 1) }");
assertEval("{ make.unique(\"a\", character()) }");
assertEval("{ .Internal(make.unique(c(7, 42), \".\")) }");
assertEval("{ .Internal(make.unique(NULL, \".\")) }");
assertEval("{ .Internal(make.unique(c(\"7\", \"42\"), 42)) }");
assertEval("{ .Internal(make.unique(c(\"7\", \"42\"), character())) }");
assertEval("{ .Internal(make.unique(c(\"7\", \"42\"), c(\".\", \".\"))) }");
assertEval("{ .Internal(make.unique(c(\"7\", \"42\"), NULL)) }");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment