Skip to content
New issue

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

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

Already on GitHub? # to your account

Escape special characters in type arguments. #2186

Merged
merged 2 commits into from
Feb 7, 2024
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 @@ -6,7 +6,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.eclipse.emf.common.util.URI;
import org.lflang.InferredType;
import org.lflang.ast.ASTUtils;
Expand Down Expand Up @@ -101,16 +100,35 @@ private static Map<String, Type> addTypeArgs(

/** Return the name of the reactor given its type arguments. */
public String getName() {
// FIXME: Types that are not just a single token need to be escaped or hashed
return reactor.getName()
+ typeArgs.values().stream().map(ASTUtils::toOriginalText).collect(Collectors.joining("_"));
return reactor.getName() + argsString();
}

/** Return a string representation of the type args of this. */
public String argsString() {
return typeArgs.values().stream()
.map(ASTUtils::toOriginalText)
.collect(Collectors.joining("_"));
var stringRepresentation = new StringBuilder();
int hash = 0;
var first = false;
for (var key : typeParams) {
if (!first) {
stringRepresentation.append('_');
}
var value = typeArgs.get(key);
var valueString = ASTUtils.toOriginalText(value);
for (int idx = 0; idx < valueString.length(); idx++) {
var c = valueString.charAt(idx);
if (Character.isLetterOrDigit(c)) {
stringRepresentation.append(c);
} else {
hash = hash * 31 + idx;
hash = hash * 31 + c;
}
}
}
if (hash != 0) {
stringRepresentation.append('_');
stringRepresentation.append(Integer.toHexString(hash));
}
return stringRepresentation.toString();
}

/** #define type names as concrete types. */
Expand Down Expand Up @@ -154,11 +172,7 @@ public InferredType resolveType(InferredType t) {
*/
public String uniqueName() {
var resolved = ASTUtils.toDefinition(reactor);
return "_"
+ uniqueName(resolved)
+ typeParams.stream()
.map(it -> typeArgs.get(it).getId()) // FIXME: may be more than just an ID
.collect(Collectors.joining("_"));
return "_" + uniqueName(resolved) + argsString();
}

@Override
Expand Down
25 changes: 25 additions & 0 deletions test/C/src/target/TypeSanitization.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
target CCpp

preamble {=
#include <memory>
#include <string>
=}

reactor TestGeneric<T> {
input test_input: T

reaction(test_input) {=
if (test_input->is_present)
{
lf_print("Got the test input");
}
=}
}

main reactor {
test = new TestGeneric<std::shared_ptr<std::string>>()

reaction(startup) -> test.test_input {=
lf_set(test.test_input, std::make_shared<std::string>(*(new std::string("test"))));
=}
}
Loading