Skip to content

Commit

Permalink
Merge pull request #2340 from lf-lang/target-property-handling2
Browse files Browse the repository at this point in the history
Fix for handling of target properties in imported files accessed through reactor class inheritance
  • Loading branch information
lhstrh authored Jun 30, 2024
2 parents 3811a1d + 45fa774 commit 87d0a3a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/org/lflang/generator/GeneratorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.core.resources.IMarker;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
// to validate, which happens in setResources().
setReactorsAndInstantiationGraph(context.getMode());

List<Resource> allResources = GeneratorUtils.getResources(reactors);
Set<Resource> allResources = GeneratorUtils.getResources(reactors);

GeneratorUtils.accommodatePhysicalActionsIfPresent(
allResources,
Expand Down Expand Up @@ -413,7 +414,7 @@ protected void checkWatchdogSupport(boolean isSupported) {
* Finds and transforms connections into forwarding reactions iff the connections have the same
* destination as other connections or reaction in mutually exclusive modes.
*/
private void transformConflictingConnectionsInModalReactors(List<Resource> resources) {
private void transformConflictingConnectionsInModalReactors(Set<Resource> resources) {
for (Resource r : resources) {
var transform = ASTUtils.findConflictingConnectionsInModalReactors(r);
if (!transform.isEmpty()) {
Expand Down
37 changes: 24 additions & 13 deletions core/src/main/java/org/lflang/generator/GeneratorUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.lflang.generator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Set;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
Expand All @@ -13,6 +12,7 @@
import org.lflang.generator.LFGeneratorContext.Mode;
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.ImportedReactor;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Reactor;
import org.lflang.lf.TargetDecl;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static TargetDecl findTargetDecl(Resource resource) {
* targetConfig}. This is a helper function for setTargetConfig. It should not be used elsewhere.
*/
public static void accommodatePhysicalActionsIfPresent(
List<Resource> resources,
Set<Resource> resources,
boolean setsKeepAliveOptionAutomatically,
TargetConfig targetConfig,
MessageReporter messageReporter) {
Expand Down Expand Up @@ -79,22 +79,33 @@ public static <T> Iterable<T> findAll(Resource resource, Class<T> nodeType) {
}

/**
* Return the resources that provide the given reactors.
* Return the resources that provide the given reactors and their ancestors.
*
* @param reactors The reactors for which to find containing resources.
* @return the resources that provide the given reactors.
*/
public static List<Resource> getResources(Iterable<Reactor> reactors) {
HashSet<Resource> visited = new HashSet<>();
List<Resource> resources = new ArrayList<>();
public static Set<Resource> getResources(Iterable<Reactor> reactors) {
Set<Resource> visited = new LinkedHashSet<>();
for (Reactor r : reactors) {
Resource resource = r.eResource();
if (!visited.contains(resource)) {
visited.add(resource);
resources.add(resource);
if (!visited.contains(r.eResource())) {
addInheritedResources(r, visited);
}
}
return visited;
}

/** Collect all resources associated with reactor through class inheritance. */
private static void addInheritedResources(Reactor reactor, Set<Resource> resources) {
resources.add(reactor.eResource());
for (var s : reactor.getSuperClasses()) {
if (!resources.contains(s)) {
if (s instanceof ImportedReactor i) {
addInheritedResources(i.getReactorClass(), resources);
} else if (s instanceof Reactor r) {
addInheritedResources(r, resources);
}
}
}
return resources;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/C/src/target/ImportedCMakeInclude.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
target C {
timeout: 1 ms
}

import Foo from "./CMakeInclude.lf"

reactor Bar extends Foo {
}

main reactor {
r = new Foo()
}

0 comments on commit 87d0a3a

Please sign in to comment.