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

wurstscript / WurstScript / 271

29 Sep 2025 12:12PM UTC coverage: 64.649% (+2.4%) from 62.222%
271

Pull #1096

circleci

Frotty
Merge branch 'perf-improvements' of https://github.com/wurstscript/WurstScript into perf-improvements
Pull Request #1096: Perf improvements

18202 of 28155 relevant lines covered (64.65%)

0.65 hits per line

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

89.86
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/types/WurstTypeClass.java
1
package de.peeeq.wurstscript.types;
2

3
import com.google.common.collect.ImmutableList;
4
import de.peeeq.wurstscript.ast.*;
5
import de.peeeq.wurstscript.jassIm.ImExprOpt;
6
import de.peeeq.wurstscript.jassIm.JassIm;
7
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
8
import io.vavr.control.Option;
9
import org.eclipse.jdt.annotation.Nullable;
10

11
import java.util.ArrayList;
12
import java.util.List;
13

14

15
public class WurstTypeClass extends WurstTypeClassOrInterface {
16

17
    private final ClassDef classDef;
18

19

20
    public WurstTypeClass(ClassDef classDef, List<WurstTypeBoundTypeParam> typeParameters, boolean staticRef) {
21
        super(typeParameters, staticRef);
1✔
22
        if (classDef == null) throw new IllegalArgumentException();
1✔
23
        this.classDef = classDef;
1✔
24
    }
1✔
25

26
    @Override
27
    VariableBinding matchAgainstSupertypeIntern(WurstType obj, @Nullable Element location, VariableBinding mapping, VariablePosition variablePosition) {
28
        VariableBinding superMapping = super.matchAgainstSupertypeIntern(obj, location, mapping, variablePosition);
1✔
29
        if (superMapping != null) {
1✔
30
            return superMapping;
1✔
31
        }
32
        // check module instantiations:
33
        if (obj instanceof WurstTypeModuleInstanciation) {
1✔
34
            WurstTypeModuleInstanciation mi = (WurstTypeModuleInstanciation) obj;
1✔
35
            @Nullable ClassDef nearestClass = mi.getDef().attrNearestClassDef();
1✔
36
            if (nearestClass == this.classDef) {
1✔
37
                return extendMapping(mapping, getTypeArgBinding(), location);
1✔
38
            }
39
        }
40
        return null;
1✔
41

42

43
    }
44

45
    private VariableBinding extendMapping(VariableBinding m1, VariableBinding m2, Element location) {
46
        for (TypeParamDef t : m2.keys()) {
1✔
47
            Option<WurstTypeBoundTypeParam> currentVal = m1.get(t);
1✔
48
            WurstTypeBoundTypeParam m2Val = m2.get(t).get();
1✔
49
            if (currentVal.isDefined()) {
1✔
50
                WurstTypeBoundTypeParam m1Val = currentVal.get();
×
51
                if (!m1Val.equalsType(m2Val, location)) {
×
52
                    // no match
53
                    return null;
×
54
                }
55
            } else {
×
56
                m1 = m1.set(t, m2Val);
1✔
57
            }
58
        }
1✔
59
        return m1;
1✔
60
    }
61

62
    public ImmutableList<WurstTypeInterface> implementedInterfaces() {
63
        classDef.getImplementsList().forEach(i -> {
1✔
64
            if(!(i.attrTyp() instanceof WurstTypeInterface)) {
1✔
65
                @Nullable NameDef nameDef = i.tryGetNameDef();
1✔
66
                if(nameDef != null) {
1✔
67
                    i.addError("<" + nameDef.getName() + "> is not an interface.");
×
68
                } else {
69
                    classDef.getNameId().addError("Expecting interface name after `implements`");
1✔
70
                }
71
            }
72
        });
1✔
73
        List<WurstTypeInterface> result = new ArrayList<>();
1✔
74
        for (TypeExpr i : classDef.getImplementsList()) {
1✔
75
            if (i == null) continue;
1✔
76

77
            WurstType t = i.attrTyp();
1✔
78
            if (t instanceof WurstTypeInterface) {
1✔
79
                WurstTypeInterface wti = (WurstTypeInterface) t.setTypeArgs(getTypeArgBinding());
1✔
80
                if (wti.level() < level()) {
1✔
81
                    result.add(wti);
1✔
82
                }
83
            }
84
        }
1✔
85
        return ImmutableList.copyOf(result);
1✔
86
    }
87

88
    /**
89
     * A type for the class extended by this class (or null if none)
90
     */
91
    public @Nullable WurstTypeClass extendedClass() {
92
        OptTypeExpr extendedClass = classDef.getExtendedClass();
1✔
93
        if (extendedClass instanceof NoTypeExpr) {
1✔
94
            return null;
1✔
95
        }
96
        WurstType t = extendedClass.attrTyp();
1✔
97
        if (t instanceof WurstTypeClass) {
1✔
98
            WurstTypeClass ct = (WurstTypeClass) t;
1✔
99
            if (ct.level() >= level()) {
1✔
100
                // cyclic dependency
101
                return null;
×
102
            }
103
            return (WurstTypeClass) ct.setTypeArgs(getTypeArgBinding());
1✔
104
        }
105
        return null;
1✔
106
    }
107

108
    @Override
109
    public ClassDef getDef() {
110
        return classDef;
1✔
111
    }
112

113
    @Override
114
    public ImmutableList<? extends WurstTypeClassOrInterface> directSupertypes() {
115
        WurstTypeClass ec = extendedClass();
1✔
116
        if (ec == null) {
1✔
117
            return implementedInterfaces();
1✔
118
        } else {
119
            ImmutableList.Builder<WurstTypeClassOrInterface> builder = ImmutableList.builder();
1✔
120
            builder.add(ec);
1✔
121
            builder.addAll(implementedInterfaces());
1✔
122
            return builder.build();
1✔
123
        }
124
    }
125

126
    public ClassDef getClassDef() {
127
        return classDef;
1✔
128
    }
129

130
    @Override
131
    public String getName() {
132
        return getDef().getName() + printTypeParams();
1✔
133
    }
134

135
    @Override
136
    public WurstType dynamic() {
137
        return new WurstTypeClass(getClassDef(), getTypeParameters(), false);
1✔
138
    }
139

140
    @Override
141
    public WurstType replaceTypeVars(List<WurstTypeBoundTypeParam> newTypes) {
142
        return new WurstTypeClass(classDef, newTypes, isStaticRef());
1✔
143
    }
144

145

146
    @Override
147
    public ImExprOpt getDefaultValue(ImTranslator tr) {
148
        return JassIm.ImIntVal(0);
×
149
    }
150

151
    public @Nullable TypeDef lookupInnerType(String typeName) {
152
        return getDef().getInnerClasses()
1✔
153
            .stream()
1✔
154
            .filter(ic -> ic.getName().equals(typeName))
1✔
155
            .findFirst()
1✔
156
            .orElse(null);
1✔
157
    }
158

159

160
}
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