Skip to content

Commit

Permalink
Merge branch 'acm-compSymbols-ports' into 'dev'
Browse files Browse the repository at this point in the history
CompSymbols Extension Ports

See merge request monticore/monticore!889
  • Loading branch information
mathias-pfeiffer committed Oct 16, 2023
2 parents 97d743d + 3b041db commit 67f95b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ component grammar CompSymbols extends de.monticore.symbols.BasicSymbols {
superComponents: de.monticore.types.check.CompKindExpression*
refinements: de.monticore.types.check.CompKindExpression*
parameters: de.monticore.symbols.basicsymbols._symboltable.VariableSymbol*
ports: de.monticore.symbols.compsymbols._symboltable.PortSymbol*
;

interface symbol Subcomponent = Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public boolean hasTypeParameter() {
return !this.getTypeParameters().isEmpty();
}

public List<PortSymbol> getPorts() {
return this.getSpannedScope().getLocalPortSymbols();
}

/**
* Returns the port of this component that matches the given name, if it
* exists. Does not consider inherited ports.
Expand All @@ -66,7 +62,7 @@ public Optional<PortSymbol> getPort(@NonNull String name) {
*/
public Optional<PortSymbol> getPort(@NonNull String name, boolean searchSuper) {
Preconditions.checkNotNull(name);
for (PortSymbol port : searchSuper ? this.getAllPorts() : this.getPorts()) {
for (PortSymbol port : searchSuper ? this.getAllPorts() : this.getPortsList()) {
if (port.getName().equals(name)) return Optional.of(port);
}
return Optional.empty();
Expand All @@ -79,7 +75,7 @@ public Optional<PortSymbol> getPort(@NonNull String name, boolean searchSuper) {
*/
public List<PortSymbol> getIncomingPorts() {
List<PortSymbol> result = new ArrayList<>();
for (PortSymbol port : this.getPorts()) {
for (PortSymbol port : this.getPortsList()) {
if (port.isIncoming()) {
result.add(port);
}
Expand Down Expand Up @@ -125,7 +121,7 @@ public Optional<PortSymbol> getIncomingPort(@NonNull String name, boolean search
*/
public List<PortSymbol> getOutgoingPorts() {
List<PortSymbol> result = new ArrayList<>();
for (PortSymbol port : this.getPorts()) {
for (PortSymbol port : this.getPortsList()) {
if (port.isOutgoing()) {
result.add(port);
}
Expand Down Expand Up @@ -174,7 +170,7 @@ public Optional<PortSymbol> getOutgoingPort(@NonNull String name, boolean search
*/
public List<PortSymbol> getPorts(boolean incoming, boolean outgoing) {
List<PortSymbol> result = new ArrayList<>();
for (PortSymbol port : this.getPorts()) {
for (PortSymbol port : this.getPortsList()) {
if (port.isIncoming() == incoming && port.isOutgoing() == outgoing) {
result.add(port);
}
Expand All @@ -188,7 +184,7 @@ public List<PortSymbol> getPorts(boolean incoming, boolean outgoing) {
* @return a {@code Set} of all ports of this component
*/
public Set<PortSymbol> getAllPorts() {
Set<PortSymbol> result = new HashSet<>(this.getPorts());
Set<PortSymbol> result = new HashSet<>(this.getPortsList());
for (CompKindExpression superComponent : this.getSuperComponentsList()) {
result.addAll(superComponent.getTypeInfo().getAllPorts());
}
Expand Down Expand Up @@ -243,10 +239,6 @@ public Set<PortSymbol> getAllPorts(boolean incoming, boolean outgoing) {
return result;
}

public boolean hasPorts() {
return !this.getPorts().isEmpty();
}

/**
* @return a {@code List} of the subcomponents of this component
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public String serialize(@NonNull ComponentSymbol toSerialize, @NonNull CompSymbo
serializeSuperComponents(toSerialize.getSuperComponentsList(), s2j);
serializeRefinements(toSerialize.getRefinementsList(), s2j);
serializeParameters(toSerialize.getParametersList(), s2j);
serializePorts(toSerialize.getPortsList(), s2j);

// Don't serialize the spanned scope (because it carries private information)
// Instead, serialize type parameters and normal parameters separately.
Expand Down Expand Up @@ -94,14 +95,12 @@ protected List<CompKindExpression> deserializeSuperComponents(JsonObject symbolJ

@Override
protected void serializeAddons(ComponentSymbol toSerialize, CompSymbolsSymbols2Json s2j) {
serializePorts(toSerialize, s2j);
serializeTypeParameters(toSerialize, s2j);
serializeSubcomponents(toSerialize, s2j);
}

@Override
protected void deserializeAddons(ComponentSymbol symbol, JsonObject symbolJson) {
deserializePorts(symbol, symbolJson);
deserializeTypeParameters(symbol, symbolJson);
}

Expand All @@ -113,11 +112,12 @@ protected void serializeParameters(List<VariableSymbol> parameters, CompSymbolsS
printer.endArray();
}

protected void serializePorts(@NonNull ComponentSymbol portOwner, @NonNull CompSymbolsSymbols2Json s2j) {
@Override
protected void serializePorts(@NonNull List<PortSymbol> ports, @NonNull CompSymbolsSymbols2Json s2j) {
JsonPrinter printer = s2j.getJsonPrinter();

printer.beginArray(PORTS);
portOwner.getPorts().forEach(p -> p.accept(s2j.getTraverser()));
ports.forEach(p -> p.accept(s2j.getTraverser()));
printer.endArray();
}

Expand Down Expand Up @@ -152,21 +152,23 @@ protected List<VariableSymbol> deserializeParameters(@NonNull JsonObject symbolJ
}

/**
* @param portOwner the component which owns the parameter.
* @param paramOwnerJson the component which owns the parameters, encoded as JSON.
* @param symbolJson the component which owns the ports, encoded as JSON.
*/
protected void deserializePorts(@NonNull ComponentSymbol portOwner, @NonNull JsonObject paramOwnerJson) {
@Override
protected List<PortSymbol> deserializePorts(@NonNull JsonObject symbolJson) {
final String portSerializeKind = PortSymbol.class.getCanonicalName();

List<JsonElement> ports = paramOwnerJson.getArrayMemberOpt(PORTS).orElseGet(Collections::emptyList);
List<JsonElement> ports = symbolJson.getArrayMemberOpt(PORTS).orElseGet(Collections::emptyList);

List<PortSymbol> result = new ArrayList<>(ports.size());

for (JsonElement port : ports) {
String portJasonKind = JsonDeSers.getKind(port.getAsJsonObject());
if (portJasonKind.equals(portSerializeKind)) {
ISymbolDeSer deSer = CompSymbolsMill.globalScope().getSymbolDeSer(portSerializeKind);
PortSymbol portSym = (PortSymbol) deSer.deserialize(port.getAsJsonObject());

portOwner.getSpannedScope().add(portSym);
result.add(portSym);

} else {
Log.error(String.format(
Expand All @@ -175,6 +177,8 @@ protected void deserializePorts(@NonNull ComponentSymbol portOwner, @NonNull Jso
));
}
}

return result;
}

protected void serializeTypeParameters(@NonNull ComponentSymbol typeParamOwner, CompSymbolsSymbols2Json s2j) {
Expand Down

0 comments on commit 67f95b5

Please sign in to comment.