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

Fix bugs in name mangling #1816

Merged
merged 19 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
10 changes: 5 additions & 5 deletions .github/workflows/check-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
- id: do
run: |
wget https://raw.githubusercontent.com/lf-lang/lingua-franca/master/.github/scripts/check-diff.sh
source check-diff.sh "core/src/main/java/generator/c\|/home/peter/lingua-franca/core/src/main/resources/lib/c\|core/src/main/resources/lib/platform\|test/C" C
source check-diff.sh "core/src/main/kotlin/generator/cpp\|core/src/main/resources/lib/cpp\|test/Cpp" CPP
source check-diff.sh "core/src/main/java/generator/python\|core/src/main/resources/lib/py\|test/Python" PY
source check-diff.sh "core/src/main/kotlin/generator/rust\|core/src/main/java/generator/rust\|core/src/main/resources/lib/rs\|test/Rust" RS
source check-diff.sh "core/src/main/kotlin/generator/ts\|core/src/main/java/generator/ts\|core/src/main/resources/lib/ts\|test/TypeScript" TS
source check-diff.sh "core/src/main/java/org/lflang/generator/c\|core/src/main/resources/lib/c\|core/src/main/resources/lib/platform\|test/C" C
source check-diff.sh "core/src/main/kotlin/org/lflang/generator/cpp\|core/src/main/resources/lib/cpp\|test/Cpp" CPP
source check-diff.sh "core/src/main/java/org/lflang/generator/python\|core/src/main/resources/lib/py\|test/Python" PY
source check-diff.sh "core/src/main/kotlin/org/lflang/generator/rust\|core/src/main/java/org/lflang/generator/rust\|core/src/main/resources/lib/rs\|test/Rust" RS
source check-diff.sh "core/src/main/kotlin/org/lflang/generator/ts\|core/src/main/java/org/lflang/generator/ts\|core/src/main/resources/lib/ts\|test/TypeScript" TS
source check-diff.sh "util/tracing" TRACING
shell: bash
10 changes: 10 additions & 0 deletions core/src/integrationTest/java/org/lflang/tests/RuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void runGenericTests() {
false);
}

@Test
public void runGenericsTests() {
runTestsForTargets(
Message.DESC_GENERICS,
TestCategory.GENERICS::equals,
Configurators::noChanges,
TestLevel.EXECUTION,
false);
}

@Test
public void runTargetSpecificTests() {
runTestsForTargets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void runGenericTests() {
super.runGenericTests();
}

@Test
@Override
public void runGenericsTests() {
super.runGenericsTests();
}

@Test
@Override
public void runTargetSpecificTests() {
Expand Down
36 changes: 24 additions & 12 deletions core/src/main/java/org/lflang/generator/ReactorInstance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/** A data structure for a reactor instance. */

/*************
* Copyright (c) 2019-2022, The University of California at Berkeley.
*
Expand Down Expand Up @@ -85,14 +83,25 @@
*/
public class ReactorInstance extends NamedInstance<Instantiation> {

/**
* Create a new instantiation hierarchy that starts with the given top-level reactor.
*
* @param reactor The top-level reactor.
* @param reporter The error reporter.
*/
public ReactorInstance(Reactor reactor, ErrorReporter reporter, List<Reactor> reactors) {
this(ASTUtils.createInstantiation(reactor), null, reporter, -1, reactors);
assert !reactors.isEmpty();
}

/**
* Create a new instantiation hierarchy that starts with the given top-level reactor.
*
* @param reactor The top-level reactor.
* @param reporter The error reporter.
*/
public ReactorInstance(Reactor reactor, ErrorReporter reporter) {
this(ASTUtils.createInstantiation(reactor), null, reporter, -1);
this(ASTUtils.createInstantiation(reactor), null, reporter, -1, List.of());
}

/**
Expand All @@ -104,7 +113,7 @@ public ReactorInstance(Reactor reactor, ErrorReporter reporter) {
* @param desiredDepth The depth to which to go, or -1 to construct the full hierarchy.
*/
public ReactorInstance(Reactor reactor, ErrorReporter reporter, int desiredDepth) {
this(ASTUtils.createInstantiation(reactor), null, reporter, desiredDepth);
this(ASTUtils.createInstantiation(reactor), null, reporter, desiredDepth, List.of());
}

/**
Expand All @@ -116,7 +125,7 @@ public ReactorInstance(Reactor reactor, ErrorReporter reporter, int desiredDepth
* @param reporter The error reporter.
*/
public ReactorInstance(Reactor reactor, ReactorInstance parent, ErrorReporter reporter) {
this(ASTUtils.createInstantiation(reactor), parent, reporter, -1);
this(ASTUtils.createInstantiation(reactor), parent, reporter, -1, List.of());
}

//////////////////////////////////////////////////////
Expand Down Expand Up @@ -790,12 +799,19 @@ protected void createWatchdogInstances() {
* @param desiredDepth The depth to which to expand the hierarchy.
*/
public ReactorInstance(
Instantiation definition, ReactorInstance parent, ErrorReporter reporter, int desiredDepth) {
Instantiation definition,
ReactorInstance parent,
ErrorReporter reporter,
int desiredDepth,
List<Reactor> reactors) {
super(definition, parent);
this.reporter = reporter;
this.reactorDeclaration = definition.getReactorClass();
this.reactorDefinition = ASTUtils.toDefinition(reactorDeclaration);
this.tpr = new TypeParameterizedReactor(definition, parent == null ? null : parent.tpr);
this.tpr =
parent == null
? new TypeParameterizedReactor(definition, reactors)
: new TypeParameterizedReactor(definition, parent.tpr);

// check for recursive instantiation
var currentParent = parent;
Expand Down Expand Up @@ -845,7 +861,7 @@ public ReactorInstance(
// Instantiate children for this reactor instance.
// While doing this, assign an index offset to each.
for (Instantiation child : ASTUtils.allInstantiations(reactorDefinition)) {
var childInstance = new ReactorInstance(child, this, reporter, desiredDepth);
var childInstance = new ReactorInstance(child, this, reporter, desiredDepth, reactors);
this.children.add(childInstance);
}

Expand Down Expand Up @@ -879,10 +895,6 @@ public ReactorInstance(
}
}

public TypeParameterizedReactor getTypeParameterizedReactor() {
return this.tpr;
}

//////////////////////////////////////////////////////
//// Private methods.

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ private void generateReactorDefinitions() throws IOException {
var generatedReactors = new LinkedHashSet<TypeParameterizedReactor>();
if (this.main != null) {
generateReactorChildren(this.main, generatedReactors);
generateReactorClass(this.main.getTypeParameterizedReactor());
generateReactorClass(new TypeParameterizedReactor(this.mainDef, reactors));
}
// do not generate code for reactors that are not instantiated
}
Expand Down Expand Up @@ -2095,7 +2095,8 @@ private void createMainReactorInstance() {
if (this.mainDef != null) {
if (this.main == null) {
// Recursively build instances.
this.main = new ReactorInstance(toDefinition(mainDef.getReactorClass()), errorReporter);
this.main =
new ReactorInstance(toDefinition(mainDef.getReactorClass()), errorReporter, reactors);
var reactionInstanceGraph = this.main.assignLevels();
if (reactionInstanceGraph.nodeCount() > 0) {
errorReporter.reportError("Main reactor has causality cycles. Skipping code generation.");
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

This file has some undocumented methods.

Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package org.lflang.generator.c;

import com.google.common.collect.ImmutableMap;
import java.util.Comparator;
import java.util.HashMap;
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;
import org.lflang.generator.CodeBuilder;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Reactor;
import org.lflang.lf.Type;
import org.lflang.lf.*;

/**
* A reactor class combined with concrete type arguments bound to its type parameters.
*
* @param reactor The syntactic reactor class definition
* @param typeArgs The type arguments associated with this particular variant of the reactor class.
*/
public record TypeParameterizedReactor(Reactor reactor, Map<String, Type> typeArgs) {
/** A reactor class combined with concrete type arguments bound to its type parameters. */
public class TypeParameterizedReactor {
/** The syntactic reactor class definition. */
private final Reactor reactor;
/** The type arguments associated with this particular variant of the reactor class. */
private final Map<String, Type> typeArgs;

private static final Map<TypeParameterizedReactor, String> uniqueNames = new HashMap<>();
private static final Map<String, Integer> nameCounts = new HashMap<>();
private final List<String> typeParams;
private final ImmutableMap<String, Map<URI, Integer>> nameMap;

/**
* Construct the TPR corresponding to the given instantiation which syntactically appears within
Expand All @@ -30,19 +32,68 @@ public record TypeParameterizedReactor(Reactor reactor, Map<String, Type> typeAr
* permitted instead of types in this TPR.
*/
public TypeParameterizedReactor(Instantiation i, TypeParameterizedReactor parent) {
this(
ASTUtils.toDefinition(i.getReactorClass()),
addTypeArgs(i, ASTUtils.toDefinition(i.getReactorClass()), parent));
this(i, parent, parent.nameMap);
}

public TypeParameterizedReactor(Instantiation i, List<Reactor> reactors) {
this(i, null, getNameMap(reactors));
}

/**
* Return a map from reactor names and URIs to integers such that no two reactor names with
* different URIs map to the same integer.
*/
private static Map<String, Map<URI, Integer>> getNameMap(List<Reactor> reactors) {
Map<String, Map<URI, Integer>> nameMap = new HashMap<>();
Map<String, Integer> countMap = new HashMap<>();
var sortedReactors =
reactors.stream()
.sorted(Comparator.comparing(a -> a.eResource().getURI().toString()))
.toList();
for (var reactor : sortedReactors) {
var def = ASTUtils.toDefinition(reactor);
var name = def.getName().toLowerCase();
if (nameMap.containsKey(name)) {
nameMap.get(name).put(def.eResource().getURI(), countMap.get(name));
countMap.put(name, countMap.get(name));
} else {
nameMap.put(name, new HashMap<>());
nameMap.get(name).put(def.eResource().getURI(), 0);
countMap.put(name, 1);
}
}
return nameMap;
}

/** Return a name that is unique to the given {@code Reactor}. */
private String uniqueName(Reactor def) {
var name = def.getName().toLowerCase();
var number = Objects.requireNonNull(nameMap.get(name)).get(def.eResource().getURI());
return name + (number == 0 ? "" : number);
}

/**
* Construct a {@code TypeParameterizedReactor} corresponding to the reactor class of the
* instantiation {@code i} within the parent {@code parent} and with the given mapping of
* definition names and URIs to integers.
*/
private TypeParameterizedReactor(
Instantiation i, TypeParameterizedReactor parent, Map<String, Map<URI, Integer>> nameMap) {
reactor = ASTUtils.toDefinition(i.getReactorClass());
var definition = ASTUtils.toDefinition(i.getReactorClass());
typeParams = definition.getTypeParms().stream().map(TypeParm::getLiteral).toList();
typeArgs = addTypeArgs(i, parent, typeParams);
this.nameMap = ImmutableMap.copyOf(nameMap);
}

/** Return a mapping from type parameters to type arguments. */
private static Map<String, Type> addTypeArgs(
Instantiation instantiation, Reactor r, TypeParameterizedReactor parent) {
Instantiation instantiation, TypeParameterizedReactor parent, List<String> typeParams) {
HashMap<String, Type> ret = new HashMap<>();
if (instantiation.getTypeArgs() != null) {
for (int i = 0; i < r.getTypeParms().size(); i++) {
for (int i = 0; i < typeParams.size(); i++) {
var arg = instantiation.getTypeArgs().get(i);
ret.put(
r.getTypeParms().get(i).getLiteral(), parent == null ? arg : parent.resolveType(arg));
ret.put(typeParams.get(i), parent == null ? arg : parent.resolveType(arg));
}
}
return ret;
Expand Down Expand Up @@ -94,18 +145,13 @@ public InferredType resolveType(InferredType t) {
* Return a name that is unique to this TypeParameterizedReactor (up to structural equality) and
* that is prefixed with exactly one underscore and that does not contain any upper-case letters.
*/
public synchronized String uniqueName() {
String name = reactor.getName().toLowerCase();
if (uniqueNames.containsKey(this)) return uniqueNames.get(this);
if (nameCounts.containsKey(name)) {
int currentCount = nameCounts.get(name);
nameCounts.put(name, currentCount + 1);
uniqueNames.put(this, "_" + name + currentCount);
return uniqueName();
}
nameCounts.put(name, 1);
uniqueNames.put(this, "_" + name);
return uniqueName();
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
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.joining("_"));
}

@Override
Expand All @@ -119,4 +165,8 @@ public boolean equals(Object obj) {
&& reactor.equals(other.reactor)
&& typeArgs.equals(other.typeArgs);
}

public Reactor reactor() {
return reactor;
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/validation/LFValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private Type portTypeIfResolvable(VarRef port) {
var portType = ((Port) port.getVariable()).getType();
return port.getContainer() == null
? portType
: new TypeParameterizedReactor(port.getContainer(), null).resolveType(portType);
: new TypeParameterizedReactor(port.getContainer(), List.of()).resolveType(portType);
}

@Check(CheckType.FAST)
Expand Down
1 change: 1 addition & 0 deletions core/src/testFixtures/java/org/lflang/tests/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static class Message {
/* Descriptions of collections of tests. */
public static final String DESC_SERIALIZATION = "Run serialization tests.";
public static final String DESC_GENERIC = "Run generic tests.";
public static final String DESC_GENERICS = "Run generics tests.";
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
public static final String DESC_TYPE_PARMS = "Run tests for reactors with type parameters.";
public static final String DESC_MULTIPORT = "Run multiport tests.";
public static final String DESC_AS_FEDERATED = "Run non-federated tests in federated mode.";
Expand Down