From 6ee308a712c579ac2e998f4b3513979857b4287a Mon Sep 17 00:00:00 2001 From: Ruben Date: Wed, 2 Oct 2024 17:06:30 +0200 Subject: [PATCH] feat: add Constraints for Records in Parameterlist --- .../typeinference/typeAlgo/TYPE.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index c763cfef..55984ef1 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typeinference.typeAlgo; import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.parser.SourceLoc; +import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -9,7 +11,10 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.util.BiRelation; +import org.antlr.v4.runtime.Token; import java.util.*; @@ -79,8 +84,46 @@ public class TYPE { if(m.block == null)return new ConstraintSet(); //Abstrakte Methoden generieren keine Constraints TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, m); TYPEStmt methodScope = new TYPEStmt(blockInfo); + ConstraintSet constraintSet = new ConstraintSet(); + m.getParameterList().getFormalparalist().forEach(el -> { + if(el instanceof RecordPattern){ + constraintSet.addAll(addRecursiveParameterConstraints((RecordPattern) el, blockInfo)); + + } + }); m.block.accept(methodScope); - return methodScope.getConstraints(); + constraintSet.addAll(methodScope.getConstraints()); + return constraintSet; + } + + public ConstraintSet addRecursiveParameterConstraints(RecordPattern recordPattern, TypeInferenceBlockInformation blockInformation){ + ConstraintSet constraintSet = new ConstraintSet(); + + var subPatternList = recordPattern.getSubPattern(); + + int counter = 0; + for(Pattern el : subPatternList){ + + if(el instanceof RecordPattern){ + constraintSet.addAll(addRecursiveParameterConstraints((RecordPattern) el, blockInformation)); + }else{ + var allClasses = blockInformation.getAvailableClasses(); + RefTypeOrTPHOrWildcardOrGeneric type; + + for (ClassOrInterface allClass : allClasses) { + var typename = recordPattern.getType().toString(); + var className = allClass.getClassName().getClassName(); + if(className.equals(typename)){ + type = allClass.getConstructors().get(0).getParameterList().getParameterAt(counter).getType(); + constraintSet.addUndConstraint(new Pair(el.getType(), type, PairOperator.SMALLERDOT, new SourceLoc(blockInformation.getCurrentClass().getFileName(), el.getOffset().getLine()))); + } + } + + } + counter++; + } + + return constraintSet; } private ConstraintSet getConstraintsConstructor(Constructor m, TypeInferenceInformation info, ClassOrInterface currentClass) {