• 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

78.85
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/types/WurstTypeNamedScope.java
1
package de.peeeq.wurstscript.types;
2

3
import com.google.common.collect.ImmutableCollection;
4
import com.google.common.collect.ImmutableMultimap;
5
import com.google.common.collect.Lists;
6
import de.peeeq.wurstscript.ast.*;
7
import de.peeeq.wurstscript.attributes.names.DefLink;
8
import de.peeeq.wurstscript.attributes.names.FuncLink;
9
import de.peeeq.wurstscript.attributes.names.Visibility;
10
import org.eclipse.jdt.annotation.NonNull;
11
import org.eclipse.jdt.annotation.Nullable;
12

13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.stream.Stream;
18

19
public abstract class WurstTypeNamedScope extends WurstType {
20

21
    private final boolean isStaticRef;
22
    // TODO change this to a list of TypeParamDef and add typeMapping?
23
    private final List<WurstTypeBoundTypeParam> typeParameters;
24

25

26

27
    public WurstTypeNamedScope(List<WurstTypeBoundTypeParam> typeParameters, boolean isStaticRef) {
1✔
28
        this.isStaticRef = isStaticRef;
1✔
29
        this.typeParameters = typeParameters;
1✔
30
    }
1✔
31

32
    public WurstTypeNamedScope(List<WurstTypeBoundTypeParam> typeParameters) {
1✔
33
        this.isStaticRef = false;
1✔
34
        this.typeParameters = typeParameters;
1✔
35
    }
1✔
36

37

38
    public WurstTypeNamedScope(boolean isStaticRef) {
1✔
39
        this.isStaticRef = isStaticRef;
1✔
40
        this.typeParameters = Collections.emptyList();
1✔
41
    }
1✔
42

43
    @Override
44
    public String getName() {
45
        NamedScope def = getDef();
×
46
        if (def == null) {
×
47
            return "not found";
×
48
        }
49
        return def.getName();
×
50
    }
51

52
    public abstract @Nullable NamedScope getDef();
53

54
    @Override
55
    public String getFullName() {
56
        return getName();
×
57
    }
58

59
    @Override
60
    public boolean isStaticRef() {
61
        return isStaticRef;
1✔
62
    }
63

64
    @Override
65
    VariableBinding matchAgainstSupertypeIntern(WurstType obj, @Nullable Element location, VariableBinding mapping, VariablePosition variablePosition) {
66
        if (obj instanceof WurstTypeNamedScope) {
1✔
67
            WurstTypeNamedScope other = (WurstTypeNamedScope) obj;
1✔
68
            if (other.getDef() == this.getDef()) {
1✔
69
                return matchTypeParams(getTypeParameters(), other.getTypeParameters(), location, mapping, variablePosition);
1✔
70
            }
71
        }
72
        return null;
1✔
73
    }
74

75
    public List<WurstTypeBoundTypeParam> getTypeParameters() {
76
        return typeParameters;
1✔
77
    }
78

79

80
    protected String printTypeParams() {
81
        if (typeParameters.size() == 0) {
1✔
82
            return "";
1✔
83
        }
84
        StringBuilder s = new StringBuilder("<");
1✔
85
        for (int i = 0; i < typeParameters.size(); i++) {
1✔
86
            if (i > 0) {
1✔
87
                s.append(", ");
1✔
88
            }
89
            s.append(typeParameters.get(i).getName());
1✔
90
        }
91
        return s + ">";
1✔
92
    }
93

94

95

96
    @Override
97
    public VariableBinding getTypeArgBinding() {
98
        VariableBinding res = VariableBinding.emptyMapping();
1✔
99
        for (WurstTypeBoundTypeParam tp : typeParameters) {
1✔
100
            res = res.set(tp.getTypeParamDef(), tp);
1✔
101
        }
1✔
102
        return res;
1✔
103
    }
104

105
    @Override
106
    public WurstType setTypeArgs(@NonNull VariableBinding typeParamBounds) {
107
        List<WurstTypeBoundTypeParam> newTypes = Lists.newArrayList();
1✔
108
        for (WurstTypeBoundTypeParam t : typeParameters) {
1✔
109
            newTypes.add(t.setTypeArgs(typeParamBounds));
1✔
110
        }
1✔
111
        return replaceTypeVars(newTypes);
1✔
112
    }
113

114
    abstract public WurstType replaceTypeVars(List<WurstTypeBoundTypeParam> newTypes);
115

116
    public WurstType replaceTypeVarsUsingTypeArgs(TypeExprList typeArgs) {
117
        if (typeArgs.isEmpty()) {
1✔
118
            // TODO replace with unknown types?
119
            return this;
×
120
        }
121
        List<WurstTypeBoundTypeParam> typeParams = new ArrayList<>();
1✔
122

123
        if (typeArgs.size() != typeParameters.size()) {
1✔
124
            typeArgs.addError("Expected " + typeParameters.size() + " type arguments, but got " + typeArgs.size());
×
125
        }
126

127
        for (int i = 0; i < typeArgs.size() && i < typeParameters.size(); i++) {
1✔
128
            WurstTypeBoundTypeParam tp = typeParameters.get(i);
1✔
129
            TypeParamDef tpDef = tp.getTypeParamDef();
1✔
130
            TypeExpr typeArg = typeArgs.get(i);
1✔
131
            WurstType baseType = typeArg.attrTyp().dynamic();
1✔
132
            typeParams.add(new WurstTypeBoundTypeParam(tpDef, baseType, typeArg));
1✔
133
        }
134

135
//                List<WurstType> newTypes = node.getTypeArgs().stream()
136
//                                .map((TypeExpr te) -> te.attrTyp().dynamic())
137
//                                .collect(Collectors.toList());
138

139
        return replaceTypeVars(typeParams);
1✔
140
    }
141

142

143
    protected VariableBinding matchTypeParams(List<WurstTypeBoundTypeParam> list, List<WurstTypeBoundTypeParam> list2, @Nullable Element location, VariableBinding mapping, VariablePosition variablePosition) {
144

145
        if (list.size() != list2.size()) {
1✔
146
            return null;
×
147
        }
148
        for (int i = 0; i < list.size(); i++) {
1✔
149
            WurstType thisTp = list.get(i).normalize();
1✔
150
            WurstType otherTp = list2.get(i).normalize();
1✔
151
            mapping = thisTp.matchTypes(otherTp, location, mapping, variablePosition);
1✔
152
            if (mapping == null) {
1✔
153
                return null;
1✔
154
            }
155
        }
156
        return mapping;
1✔
157
    }
158

159
    @Override
160
    public boolean allowsDynamicDispatch() {
161
        // dynamic dispatch is possible if this is not a static reference
162
        return !isStaticRef();
1✔
163
    }
164

165
    /**
166
     * get the name links available in this class or interface
167
     * This includes inherited names
168
     */
169
    public ImmutableMultimap<String, DefLink> nameLinks() {
170
        ImmutableMultimap<String, DefLink> res = getDef().attrNameLinks();
1✔
171
        VariableBinding binding = getTypeArgBinding();
1✔
172
        if (!binding.isEmpty()) {
1✔
173
            // OPT maybe cache this
174
            ImmutableMultimap.Builder<String, DefLink> resBuilder = ImmutableMultimap.builder();
1✔
175
            for (Map.Entry<String, DefLink> e : res.entries()) {
1✔
176
                resBuilder.put(e.getKey(), e.getValue().withTypeArgBinding(getDef(), binding));
1✔
177
            }
1✔
178
            return resBuilder.build();
1✔
179
        }
180
        return res;
1✔
181
    }
182

183
    public ImmutableCollection<DefLink> nameLinks(String name) {
184
        return nameLinks().get(name);
1✔
185
    }
186

187
    @Override
188
    public void addMemberMethods(Element node, String name,
189
                                 List<FuncLink> result) {
190
        for (DefLink defLink : nameLinks(name)) {
1✔
191
            if (defLink instanceof FuncLink) {
1✔
192
                FuncLink f = (FuncLink) defLink;
1✔
193
                if (f.getVisibility().isPublic()) {
1✔
194
                    result.add(f);
1✔
195
                } else if (f.getVisibility().isInherited()) {
1✔
196
                    // for protected members:
197
                    NamedScope def = getDef();
1✔
198
                    if (def != null && node.attrNearestPackage() != def.attrNearestPackage()) {
1✔
199
                        // if in different package, check if we are in a subclass:
200
                        ClassDef nearestClass = node.attrNearestClassDef();
1✔
201
                        if (nearestClass == null
1✔
202
                                || !nearestClass.attrTypC().isSubtypeOf(this, node)) {
1✔
203
                            // if not in a subclass, change to not visible
204
                            f = f.withVisibility(Visibility.PROTECTED_OTHER);
1✔
205
                        }
206
                    }
207
                    result.add(f);
1✔
208
                }
209
            }
210
        }
1✔
211
    }
1✔
212

213
    @Override
214
    public Stream<FuncLink> getMemberMethods(Element node) {
215
        return nameLinks().values().stream()
×
216
                .filter(n -> {
×
217
                    WurstType receiverType = n.getReceiverType();
×
218
                    return n instanceof FuncLink
×
219
                            && receiverType != null;
220
                }).map(n -> (FuncLink) n);
×
221
    }
222

223
    @Override
224
    public boolean isNestedInside(WurstType other) {
225
        if (other instanceof WurstTypeNamedScope) {
×
226
            WurstTypeNamedScope wtns = (WurstTypeNamedScope) other;
×
227
            NamedScope scope = wtns.getDef();
×
228
            Element node = this.getDef();
×
229
            while (node != null) {
×
230
                if (node == scope) {
×
231
                    return true;
×
232
                }
233
                node = node.getParent();
×
234
            }
235
        }
236
        return false;
×
237
    }
238

239
    @Override
240
    protected boolean isNullable() {
241
        return true;
1✔
242
    }
243

244

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