Skip to content

Commit

Permalink
CompSymbols Explicit Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian- Costin Marin authored and mathias-pfeiffer committed Oct 13, 2023
1 parent 08214bf commit f0006af
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ component grammar CompSymbols extends de.monticore.symbols.BasicSymbols {
symbolrule Component =
superComponents: de.monticore.types.check.CompKindExpression*
refinements: de.monticore.types.check.CompKindExpression*
parameters: de.monticore.symbols.basicsymbols._symboltable.VariableSymbol*
;

interface symbol Subcomponent = Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,18 @@

public class ComponentSymbol extends ComponentSymbolTOP {

protected List<VariableSymbol> parameters;

public ComponentSymbol(String name) {
super(name);
}

public List<VariableSymbol> getParameters() {
return this.parameters;
}

public Optional<VariableSymbol> getParameter(@NonNull String name) {
Preconditions.checkNotNull(name);
for (VariableSymbol parameter : this.getParameters()) {
for (VariableSymbol parameter : this.getParametersList()) {
if (parameter.getName().equals(name)) return Optional.of(parameter);
}
return Optional.empty();
}

public void addParameter(@NonNull VariableSymbol parameter) {
Preconditions.checkNotNull(parameter);
Preconditions.checkArgument(this.getSpannedScope().getLocalVariableSymbols().contains(parameter));
this.parameters.add(parameter);
}

public boolean hasParameters() {
return !this.getParameters().isEmpty();
}

public List<TypeVarSymbol> getTypeParameters() {
return this.getSpannedScope().getLocalTypeVarSymbols();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public String serialize(@NonNull ComponentSymbol toSerialize, @NonNull CompSymbo

// serialize symbolrule attributes
serializeSuperComponents(toSerialize.getSuperComponentsList(), s2j);
serializeRefinements(toSerialize.getRefinementsList(), s2j);
serializeParameters(toSerialize.getParametersList(), s2j);

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

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

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

protected void serializeParameters(@NonNull ComponentSymbol paramOwner, @NonNull CompSymbolsSymbols2Json s2j) {
@Override
protected void serializeParameters(List<VariableSymbol> parameters, CompSymbolsSymbols2Json s2j) {
JsonPrinter printer = s2j.getJsonPrinter();

printer.beginArray(PARAMETERS);
paramOwner.getParameters().forEach(p -> p.accept(s2j.getTraverser()));
parameters.forEach(p -> p.accept(s2j.getTraverser()));
printer.endArray();
}

Expand All @@ -122,22 +122,23 @@ protected void serializePorts(@NonNull ComponentSymbol portOwner, @NonNull CompS
}

/**
* @param paramOwner the component which owns the parameter.
* @param paramOwnerJson the component which owns the parameters, encoded as JSON.
* @param symbolJson the component which owns the parameters, encoded as JSON.
*/
protected void deserializeParameters(@NonNull ComponentSymbol paramOwner, @NonNull JsonObject paramOwnerJson) {
@Override
protected List<VariableSymbol> deserializeParameters(@NonNull JsonObject symbolJson) {
final String varSerializeKind = VariableSymbol.class.getCanonicalName();

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

List<VariableSymbol> result = new ArrayList<>(params.size());

for (JsonElement param : params) {
String paramJsonKind = JsonDeSers.getKind(param.getAsJsonObject());
if (paramJsonKind.equals(varSerializeKind)) {
ISymbolDeSer deSer = CompSymbolsMill.globalScope().getSymbolDeSer(varSerializeKind);
VariableSymbol paramSym = (VariableSymbol) deSer.deserialize(param.getAsJsonObject());

paramOwner.getSpannedScope().add(paramSym);
paramOwner.addParameter(paramSym);
result.add(paramSym);

} else {
Log.error(String.format(
Expand All @@ -146,6 +147,8 @@ protected void deserializeParameters(@NonNull ComponentSymbol paramOwner, @NonNu
));
}
}

return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ public void bindParams() {
LinkedHashMap<VariableSymbol, ASTExpression> parameterBindings = new LinkedHashMap<>();
// We know LinkedHashMaps are ordered by insertion time. As we rely on the fact that the ordering of the
// arguments is consistent with the ordering in the map, the following iteration ensures it:
for (int i = 0; i < this.getTypeInfo().getParameters().size(); i++) {
for (int i = 0; i < this.getTypeInfo().getParametersList().size(); i++) {
if (i < parameterArguments.size()) // Deal with wrong number of parameters through cocos
if (parameterArguments.get(i) instanceof ASTAssignmentExpression
&& ((ASTAssignmentExpression) parameterArguments.get(i)).getLeft() instanceof ASTNameExpression) {
keywordExpressionMap.put(((ASTNameExpression) ((ASTAssignmentExpression) parameterArguments.get(i))
.getLeft()).getName(), parameterArguments.get(i));
} else {
parameterBindings.put(this.getTypeInfo().getParameters().get(i), parameterArguments.get(i));
parameterBindings.put(this.getTypeInfo().getParametersList().get(i), parameterArguments.get(i));
firstKeywordArgument++;
}
}

// iterate over keyword-based arguments (CoCo assures that no position-based argument occurs
// after the first keyword-based argument)
for (int j = firstKeywordArgument; j < this.getTypeInfo().getParameters().size(); j++) {
if (keywordExpressionMap.containsKey(this.getTypeInfo().getParameters().get(j).getName()) &&
!parameterBindings.containsKey(this.getTypeInfo().getParameters().get(j))) {
parameterBindings.put(this.getTypeInfo().getParameters().get(j),
keywordExpressionMap.get(this.getTypeInfo().getParameters().get(j).getName()));
for (int j = firstKeywordArgument; j < this.getTypeInfo().getParametersList().size(); j++) {
if (keywordExpressionMap.containsKey(this.getTypeInfo().getParametersList().get(j).getName()) &&
!parameterBindings.containsKey(this.getTypeInfo().getParametersList().get(j))) {
parameterBindings.put(this.getTypeInfo().getParametersList().get(j),
keywordExpressionMap.get(this.getTypeInfo().getParametersList().get(j).getName()));
}
}

Expand Down

0 comments on commit f0006af

Please sign in to comment.