From f0006af266f6ba1ed9480a507d32e1b75275af83 Mon Sep 17 00:00:00 2001 From: Adrian- Costin Marin Date: Fri, 13 Oct 2023 10:47:36 +0200 Subject: [PATCH] CompSymbols Explicit Parameters --- .../de/monticore/symbols/CompSymbols.mc4 | 1 + .../_symboltable/ComponentSymbol.java | 17 +------------ .../_symboltable/ComponentSymbolDeSer.java | 25 +++++++++++-------- .../types/check/CompKindExpression.java | 14 +++++------ 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/monticore-grammar/src/main/grammars/de/monticore/symbols/CompSymbols.mc4 b/monticore-grammar/src/main/grammars/de/monticore/symbols/CompSymbols.mc4 index cb24c64b67..47eed6172a 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/symbols/CompSymbols.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/symbols/CompSymbols.mc4 @@ -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; diff --git a/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbol.java b/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbol.java index b624d852ff..fa171a54c1 100644 --- a/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbol.java +++ b/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbol.java @@ -17,33 +17,18 @@ public class ComponentSymbol extends ComponentSymbolTOP { - protected List parameters; - public ComponentSymbol(String name) { super(name); } - public List getParameters() { - return this.parameters; - } - public Optional 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 getTypeParameters() { return this.getSpannedScope().getLocalTypeVarSymbols(); diff --git a/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbolDeSer.java b/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbolDeSer.java index 86377a1500..e330771edf 100644 --- a/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbolDeSer.java +++ b/monticore-grammar/src/main/java/de/monticore/symbols/compsymbols/_symboltable/ComponentSymbolDeSer.java @@ -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. @@ -92,7 +94,6 @@ protected List deserializeSuperComponents(JsonObject symbolJ @Override protected void serializeAddons(ComponentSymbol toSerialize, CompSymbolsSymbols2Json s2j) { - serializeParameters(toSerialize, s2j); serializePorts(toSerialize, s2j); serializeTypeParameters(toSerialize, s2j); serializeSubcomponents(toSerialize, s2j); @@ -100,16 +101,15 @@ protected void serializeAddons(ComponentSymbol toSerialize, CompSymbolsSymbols2 @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 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(); } @@ -122,13 +122,15 @@ 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 deserializeParameters(@NonNull JsonObject symbolJson) { final String varSerializeKind = VariableSymbol.class.getCanonicalName(); - List params = paramOwnerJson.getArrayMemberOpt(PARAMETERS).orElseGet(Collections::emptyList); + List params = symbolJson.getArrayMemberOpt(PARAMETERS).orElseGet(Collections::emptyList); + + List result = new ArrayList<>(params.size()); for (JsonElement param : params) { String paramJsonKind = JsonDeSers.getKind(param.getAsJsonObject()); @@ -136,8 +138,7 @@ protected void deserializeParameters(@NonNull ComponentSymbol paramOwner, @NonNu 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( @@ -146,6 +147,8 @@ protected void deserializeParameters(@NonNull ComponentSymbol paramOwner, @NonNu )); } } + + return result; } /** diff --git a/monticore-grammar/src/main/java/de/monticore/types/check/CompKindExpression.java b/monticore-grammar/src/main/java/de/monticore/types/check/CompKindExpression.java index 59496cec3e..be94c7c5f9 100644 --- a/monticore-grammar/src/main/java/de/monticore/types/check/CompKindExpression.java +++ b/monticore-grammar/src/main/java/de/monticore/types/check/CompKindExpression.java @@ -74,25 +74,25 @@ public void bindParams() { LinkedHashMap 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())); } }