• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 228

29 Nov 2023 05:00PM UTC coverage: 62.48% (-0.09%) from 62.574%
228

push

circleci

web-flow
Show dialog for choosing game path, cleanup (#1083)

* show dialog for choosing game path

* cleanup code

* remove logs and refactor

* remove confusing mpq error, make some mpq loads readonly

17295 of 27681 relevant lines covered (62.48%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrPossibleFunctionSignatures.java
1
package de.peeeq.wurstscript.attributes;
2

3
import com.google.common.collect.ImmutableCollection;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.Lists;
6
import de.peeeq.wurstscript.ast.*;
7
import de.peeeq.wurstscript.attributes.names.FuncLink;
8
import de.peeeq.wurstscript.types.FunctionSignature;
9
import de.peeeq.wurstscript.types.FunctionSignature.ArgsMatchResult;
10
import de.peeeq.wurstscript.types.VariableBinding;
11
import de.peeeq.wurstscript.types.VariablePosition;
12
import de.peeeq.wurstscript.types.WurstType;
13

14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.List;
17

18
import static de.peeeq.wurstscript.attributes.GenericsHelper.givenBinding;
19
import static de.peeeq.wurstscript.attributes.GenericsHelper.typeParameters;
20

21
public class AttrPossibleFunctionSignatures {
×
22

23
    public static ImmutableCollection<FunctionSignature> calculate(FunctionCall fc) {
24
        ImmutableCollection<FuncLink> fs = fc.attrPossibleFuncDefs();
1✔
25
        ImmutableCollection.Builder<FunctionSignature> resultBuilder = ImmutableList.builder();
1✔
26
        for (FuncLink f : fs) {
1✔
27
            FunctionSignature sig = FunctionSignature.fromNameLink(f);
1✔
28

29
            OptExpr implicitParameterOpt = AttrImplicitParameter.getFunctionCallImplicitParameter(fc, f, false);
1✔
30
            if (implicitParameterOpt instanceof Expr) {
1✔
31
                Expr expr = (Expr) implicitParameterOpt;
1✔
32
                VariableBinding mapping = expr.attrTyp().matchAgainstSupertype(sig.getReceiverType(), fc, sig.getMapping(), VariablePosition.RIGHT);
1✔
33
                if (mapping == null) {
1✔
34
                    // TODO error message? Or just ignore wrong parameter type?
35
                    continue;
×
36
                } else {
37
                    sig = sig.setTypeArgs(fc, mapping);
1✔
38
                }
39
            } // TODO else check?
40

41
            VariableBinding mapping = givenBinding(fc, sig.getDefinitionTypeVariables());
1✔
42
            sig = sig.setTypeArgs(fc, mapping);
1✔
43

44
            resultBuilder.add(sig);
1✔
45
        }
1✔
46
        return findBestSignature(fc, resultBuilder.build());
1✔
47
    }
48

49
    private static ImmutableCollection<FunctionSignature> findBestSignature(StmtCall fc, ImmutableCollection<FunctionSignature> res) {
50
        ImmutableCollection.Builder<FunctionSignature> resultBuilder2 = ImmutableList.builder();
1✔
51
        List<WurstType> argTypes = AttrFuncDef.argumentTypesPre(fc);
1✔
52
        for (FunctionSignature sig : res) {
1✔
53
            FunctionSignature sig2 = sig.matchAgainstArgs(argTypes, fc);
1✔
54
            if (sig2 != null) {
1✔
55
                resultBuilder2.add(sig2);
1✔
56
            }
57
        }
1✔
58
        ImmutableCollection<FunctionSignature> res2 = resultBuilder2.build();
1✔
59
        if (res2.isEmpty()) {
1✔
60
            // no signature matches precisely --> try to match as good as possible
61
            ImmutableList<ArgsMatchResult> match3 = res.stream()
1✔
62
                    .map(sig -> sig.tryMatchAgainstArgs(argTypes, fc.getArgs(), fc))
1✔
63
                    .collect(ImmutableList.toImmutableList());
1✔
64

65
            if (match3.isEmpty()) {
1✔
66
                return ImmutableList.of();
1✔
67
            } else {
68
                // add errors from best match (minimal badness)
69
                ArgsMatchResult min = Collections.min(match3, Comparator.comparing(ArgsMatchResult::getBadness));
1✔
70
                for (CompileError c : min.getErrors()) {
1✔
71
                    fc.getErrorHandler().sendError(c);
1✔
72
                }
1✔
73

74
                return match3.stream()
1✔
75
                        .map(ArgsMatchResult::getSig)
1✔
76
                        .collect(ImmutableList.toImmutableList());
1✔
77
            }
78
        } else {
79
            return res2;
1✔
80
        }
81
    }
82

83
    public static ImmutableCollection<FunctionSignature> calculate(ExprNewObject fc) {
84
        TypeDef typeDef = fc.attrTypeDef();
1✔
85
        if (!(typeDef instanceof ClassDef)) {
1✔
86
            return ImmutableList.of();
×
87
        }
88

89
        ClassDef classDef = (ClassDef) typeDef;
1✔
90

91
        List<ConstructorDef> constructors = classDef.getConstructors();
1✔
92

93
        ImmutableList.Builder<FunctionSignature> res = ImmutableList.builder();
1✔
94
        for (ConstructorDef f : constructors) {
1✔
95
            WurstType returnType = classDef.attrTyp().dynamic();
1✔
96
            VariableBinding binding2 = givenBinding(fc, typeParameters(classDef));
1✔
97
            List<WurstType> paramTypes = Lists.newArrayList();
1✔
98
            for (WParameter p : f.getParameters()) {
1✔
99
                paramTypes.add(p.attrTyp());
1✔
100
            }
1✔
101
            List<String> pNames = FunctionSignature.getParamNames(f.getParameters());
1✔
102
            List<TypeParamDef> typeParams = classDef.getTypeParameters();
1✔
103
            VariableBinding mapping = VariableBinding.emptyMapping().withTypeVariables(typeParams);
1✔
104
            FunctionSignature sig = new FunctionSignature(f, mapping, null, "construct", paramTypes, pNames, returnType);
1✔
105
            sig = sig.setTypeArgs(fc, binding2);
1✔
106
            res.add(sig);
1✔
107
        }
1✔
108
        return findBestSignature(fc, res.build());
1✔
109
    }
110

111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc