Skip to content

Commit 2f6acbb

Browse files
committed
simplified use of .dynamic()
1 parent 4465a7d commit 2f6acbb

File tree

17 files changed

+49
-31
lines changed

17 files changed

+49
-31
lines changed

de.peeeq.wurstscript/parserspec/wurstscript.parseq

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ HasReadVariables.attrReadVariables
382382
implemented by de.peeeq.wurstscript.attributes.ReadVariables.calculate
383383

384384

385+
FunctionDefinition.attrReturnTyp()
386+
returns de.peeeq.wurstscript.types.WurstType
387+
implemented by de.peeeq.wurstscript.attributes.AttrReturnTyp.calculate
388+
385389
OptTypeExpr.attrTyp
386390
returns de.peeeq.wurstscript.types.WurstType
387391
implemented by de.peeeq.wurstscript.attributes.AttrTypeExprType.normalizedType
@@ -690,7 +694,7 @@ StmtForEach.attrGetNextFunc
690694
implemented by de.peeeq.wurstscript.attributes.AttrForEachStatement.calcGetNext
691695

692696
StmtForEach.attrCloseFunc
693-
returns java.util.Optional<de.peeeq.wurstscript.attributes.names.NameLink>
697+
returns java.util.Optional<de.peeeq.wurstscript.attributes.names.FuncLink>
694698
implemented by de.peeeq.wurstscript.attributes.AttrForEachStatement.calcClose
695699

696700

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/hotdoc/HotdocGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ private <T extends FunctionDefinition> void documentFuncs(List<T> funcs, StringW
211211
first = false;
212212
}
213213
descr.append(")");
214-
if (f.getReturnTyp().attrTyp().isVoid()) {
214+
if (f.attrReturnTyp().isVoid()) {
215215

216216
} else {
217217
descr.append(" returns ");
218-
descr.append(f.getReturnTyp().attrTyp());
218+
descr.append(f.attrReturnTyp());
219219
}
220220

221221
context.put("name", descr);

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/GetCompletions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ private CompletionItem makeFunctionCompletion(FunctionDefinition f) {
574574
completion.setDetail(getFunctionDescriptionShort(f));
575575
completion.setDocumentation(HoverInfo.descriptionString(f));
576576
completion.setInsertText(replacementString);
577-
completion.setSortText(ratingToString(calculateRating(f.getName(), f.getReturnTyp().attrTyp().dynamic())));
577+
completion.setSortText(ratingToString(calculateRating(f.getName(), f.attrReturnTyp())));
578578
// TODO use call signature instead for generics
579579
// completion.set
580580

@@ -598,7 +598,7 @@ private void addParamSnippet(String replacementString, WParameters params, Compl
598598

599599
private String getFunctionDescriptionShort(FunctionDefinition f) {
600600
String displayString = "(" + Utils.getParameterListText(f) + ")";
601-
WurstType returnType = f.getReturnTyp().attrTyp();
601+
WurstType returnType = f.attrReturnTyp();
602602
if (!(returnType instanceof WurstTypeVoid)) {
603603
displayString += " returns " + returnType;
604604
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/HoverInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public List<Either<String, MarkedString>> description(FunctionDefinition f) {
7474
}
7575

7676
String params = getParameterString(f);
77-
String returnTypeHtml = type(f.getReturnTyp().attrTyp());
77+
String returnTypeHtml = type(f.attrReturnTyp());
7878
String functionDescription = "";
7979

8080
String funcName = f.getName();

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrExprExpectedType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static WurstType calculate(Expr expr) {
7373
StmtReturn stmtReturn = (StmtReturn) parent;
7474
FunctionImplementation nearestFuncDef = stmtReturn.attrNearestFuncDef();
7575
if (nearestFuncDef != null) {
76-
return nearestFuncDef.getReturnTyp().attrTyp();
76+
return nearestFuncDef.attrReturnTyp();
7777
}
7878
} else if (parent instanceof SwitchCase) {
7979
SwitchCase sc = (SwitchCase) parent;

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrExprType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ private static WurstType handleOperatorOverloading(ExprBinary term) {
333333
.getOverloadingFuncName());
334334
return WurstTypeUnknown.instance();
335335
}
336-
return def.getReturnTyp().attrTyp().dynamic();
336+
return def.attrReturnTyp();
337337
}
338338

339339
private static WurstType requireEqualTypes(ExprBinary term,

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrForEachStatement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public static Optional<FuncLink> calcGetNext(StmtForEach forEach) {
5252
return nextFunc;
5353
}
5454

55-
public static Optional<NameLink> calcClose(StmtForEach forEach) {
55+
public static Optional<FuncLink> calcClose(StmtForEach forEach) {
5656
if(forEach instanceof StmtForFrom) {
5757
return Optional.empty();
5858
}
5959
WurstType iteratorType = calcItrType(forEach);
6060

6161
// find 'close' function:
62-
ImmutableCollection<NameLink> close = forEach.getIn().lookupMemberFuncs(iteratorType, "close", false);
62+
ImmutableCollection<FuncLink> close = forEach.getIn().lookupMemberFuncs(iteratorType, "close", false);
6363
// find the 'close' function without parameters
64-
Optional<NameLink> closeFunc = close.stream().filter(nl -> nl.getParameterTypes().isEmpty()).findFirst();
64+
Optional<FuncLink> closeFunc = close.stream().filter(nl -> nl.getParameterTypes().isEmpty()).findFirst();
6565
if (!closeFunc.isPresent()) {
6666
forEach.getIn().addError("Target of for-loop <" + forEach.getIn().attrTyp().getName() + " doesn't provide a proper close() function");
6767
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package de.peeeq.wurstscript.attributes;
2+
3+
import de.peeeq.wurstscript.ast.FunctionDefinition;
4+
import de.peeeq.wurstscript.ast.TupleDef;
5+
import de.peeeq.wurstscript.types.WurstType;
6+
7+
/**
8+
*
9+
*/
10+
public class AttrReturnTyp {
11+
public static WurstType calculate(FunctionDefinition f) {
12+
return f.getReturnTyp().attrTyp().dynamic();
13+
}
14+
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrVarDefType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static WurstType calculate(NativeType n) {
132132
}
133133

134134
public static WurstType calculate(FunctionDefinition f) {
135-
return f.getReturnTyp().attrTyp();
135+
return f.attrReturnTyp();
136136
}
137137

138138
public static WurstType calculate(TypeParamDef t) {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/CheckHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static Optional<String> checkIfIsRefinement(Map<TypeParamDef, WurstTypeBo
2727
return Optional.of("Function " + funcName + " must be static.");
2828
}
2929
// check returntype
30-
WurstType f_type = getRealType(f, typeParamMapping, f.getReturnTyp().attrTyp());
31-
WurstType of_type = getRealType(f, typeParamMapping, of.getReturnTyp().attrTyp());
30+
WurstType f_type = getRealType(f, typeParamMapping, f.attrReturnTyp());
31+
WurstType of_type = getRealType(f, typeParamMapping, of.attrReturnTyp());
3232
if (!f_type.isSubtypeOf(of_type, f)) {
3333
return Optional.of(errorMessage + funcName + ": The return type is " + f_type +
3434
" but it should be " + of_type + ".");

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/DescriptionHtml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static String description(FunctionDefinition f) {
2222
String comment = f.attrComment();
2323
comment = comment.replaceAll("\n", "<br />");
2424
String params = getParameterString(f);
25-
String returnTypeHtml = htmlType(f.getReturnTyp().attrTyp());
25+
String returnTypeHtml = htmlType(f.attrReturnTyp());
2626
String functionDescription;
2727
if (f.attrComment().length() > 1) {
2828
functionDescription = comment;

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/names/FuncLink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static FuncLink create(FunctionDefinition func, WScope definedIn) {
3333
List<WurstType> lparameterTypes = func.getParameters().stream()
3434
.map(WParameter::attrTyp)
3535
.collect(Collectors.toList());
36-
WurstType lreturnType = func.getReturnTyp().attrTyp().dynamic();
36+
WurstType lreturnType = func.attrReturnTyp();
3737
WurstType lreceiverType = calcReceiverType(definedIn, func);
3838
return new FuncLink(visibiliy, definedIn, typeParams, lreceiverType, func, lparameterTypes, lreturnType);
3939
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/FuncSkeleton.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void create(ConstructorDef constr, ImTranslator translator, ImFunc
1414

1515
public static void create(ExtensionFuncDef funcDef, ImTranslator translator, ImFunction f) {
1616
// return type:
17-
f.setReturnType(funcDef.getReturnTyp().attrTyp().imTranslateType());
17+
f.setReturnType(funcDef.attrReturnTyp().imTranslateType());
1818
// parameters
1919
ImVar thisVar = translator.getThisVar(funcDef);
2020
thisVar.setType(funcDef.getExtendedType().attrTyp().imTranslateType());
@@ -24,7 +24,7 @@ public static void create(ExtensionFuncDef funcDef, ImTranslator translator, ImF
2424

2525
public static void create(FuncDef funcDef, ImTranslator translator, ImFunction f) {
2626
// return type:
27-
f.setReturnType(funcDef.getReturnTyp().attrTyp().imTranslateType());
27+
f.setReturnType(funcDef.attrReturnTyp().imTranslateType());
2828
// parameters
2929
if (funcDef.attrIsDynamicClassMember()) {
3030
ImVar thisVar = translator.getThisVar(funcDef);
@@ -37,7 +37,7 @@ public static void create(InitBlock initBlock, ImTranslator translator, ImFuncti
3737
}
3838

3939
public static void create(NativeFunc funcDef, ImTranslator translator, ImFunction f) {
40-
f.setReturnType(funcDef.getReturnTyp().attrTyp().imTranslateType());
40+
f.setReturnType(funcDef.attrReturnTyp().imTranslateType());
4141
ImHelper.translateParameters(funcDef.getParameters(), f.getParameters(), translator);
4242
}
4343

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/OverrideUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void addOverride(
6868
argFromIndexFuncs.add(null);
6969
}
7070

71-
WurstType retType = superMethod.getReturnTyp().attrTyp();
71+
WurstType retType = superMethod.attrReturnTyp();
7272
if (retType instanceof WurstTypeTypeParam) {
7373
WurstTypeTypeParam bt = (WurstTypeTypeParam) retType;
7474
TypeParamDef tpDef = bt.getDef();

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/StmtTranslation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ public static ImStmt translate(StmtForIn s, ImTranslator t, ImFunction f) {
184184
result.add(ImLoop(s, imBody));
185185

186186
// close iterator
187-
Optional<NameLink> closeFunc = s.attrCloseFunc();
188-
closeFunc.ifPresent(nameLink -> result.add(JassIm.ImFunctionCall(s, t.getFuncFor((TranslatedToImFunction) nameLink.getNameDef()), JassIm.ImExprs(JassIm
187+
Optional<FuncLink> closeFunc = s.attrCloseFunc();
188+
closeFunc.ifPresent(nameLink -> result.add(JassIm.ImFunctionCall(s, t.getFuncFor(nameLink.getDef()), JassIm.ImExprs(JassIm
189189
.ImVarAccess(iteratorVar)), false, CallType.NORMAL)));
190190

191191
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/types/FunctionSignature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public FunctionSignature setTypeArgs(Element context, Map<TypeParamDef, WurstTyp
5656

5757

5858
public static FunctionSignature forFunctionDefinition(@Nullable FunctionDefinition f) {
59-
// return new FunctionSignature(def.attrReceiverType(), def.attrParameterTypes(), def.getReturnTyp().attrTyp());
59+
// return new FunctionSignature(def.attrReceiverType(), def.attrParameterTypes(), def.attrReturnTyp());
6060
if (f == null) {
6161
return FunctionSignature.empty;
6262
}
63-
WurstType returnType = f.getReturnTyp().attrTyp().dynamic();
63+
WurstType returnType = f.attrReturnTyp();
6464
if (f instanceof TupleDef) {
6565
TupleDef tupleDef = (TupleDef) f;
6666
returnType = tupleDef.attrTyp().dynamic();

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/validation/WurstValidator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ private boolean equalSignatures(FuncDef f, FuncDef g) {
490490
if (f.getParameters().size() != g.getParameters().size()) {
491491
return false;
492492
}
493-
if (!f.getReturnTyp().attrTyp().equalsType(g.getReturnTyp().attrTyp(), f)) {
493+
if (!f.attrReturnTyp().equalsType(g.attrReturnTyp(), f)) {
494494
return false;
495495
}
496496
for (int i = 0; i < f.getParameters().size(); i++) {
@@ -1119,7 +1119,7 @@ private void visit(ExprFunctionCall stmtCall) {
11191119
ExprFuncRef exprFuncRef = (ExprFuncRef) firstArg;
11201120
FunctionDefinition f = exprFuncRef.attrFuncDef();
11211121
if (f != null) {
1122-
if (!(f.getReturnTyp().attrTyp() instanceof WurstTypeBool) && !(f.getReturnTyp().attrTyp() instanceof WurstTypeVoid)) {
1122+
if (!(f.attrReturnTyp() instanceof WurstTypeBool) && !(f.attrReturnTyp() instanceof WurstTypeVoid)) {
11231123
firstArg.addError("Functions passed to Filter or Condition must return boolean or nothing.");
11241124
}
11251125
}
@@ -1226,7 +1226,7 @@ private void visit(StmtReturn s) {
12261226
}
12271227

12281228
private void checkReturnInFunc(StmtReturn s, FunctionImplementation func) {
1229-
WurstType returnType = func.getReturnTyp().attrTyp().dynamic();
1229+
WurstType returnType = func.attrReturnTyp();
12301230
if (s.getReturnedObj() instanceof Expr) {
12311231
Expr returned = (Expr) s.getReturnedObj();
12321232
if (returnType.isSubtypeOf(WurstTypeVoid.instance(), s)) {
@@ -1391,11 +1391,11 @@ private void checkTypeBinding(HasTypeArgs e) {
13911391
if (toIndexF.getParameters().size() != 1) {
13921392
toIndexF.addError("Must have exactly one parameter");
13931393

1394-
} else if (!toIndexF.getParameters().get(0).attrTyp().dynamic().equalsType(typ, e)) {
1394+
} else if (!toIndexF.getParameters().get(0).attrTyp().equalsType(typ, e)) {
13951395
toIndexF.addError("Parameter must be of type " + typ);
13961396
}
13971397

1398-
WurstType returnType = toIndexF.getReturnTyp().attrTyp().dynamic();
1398+
WurstType returnType = toIndexF.attrReturnTyp();
13991399
if (!returnType.equalsType(WurstTypeInt.instance(), e)) {
14001400
toIndexF.addError("Return type must be of type int " + " but was " + returnType);
14011401
}
@@ -1410,12 +1410,12 @@ private void checkTypeBinding(HasTypeArgs e) {
14101410
if (fromIndexF.getParameters().size() != 1) {
14111411
fromIndexF.addError("Must have exactly one parameter");
14121412

1413-
} else if (!fromIndexF.getParameters().get(0).attrTyp().dynamic()
1413+
} else if (!fromIndexF.getParameters().get(0).attrTyp()
14141414
.equalsType(WurstTypeInt.instance(), e)) {
14151415
fromIndexF.addError("Parameter must be of type int");
14161416
}
14171417

1418-
WurstType returnType = fromIndexF.getReturnTyp().attrTyp().dynamic();
1418+
WurstType returnType = fromIndexF.attrReturnTyp();
14191419
if (!returnType.equalsType(typ, e)) {
14201420
fromIndexF.addError("Return type must be of type " + typ + " but was " + returnType);
14211421
}

0 commit comments

Comments
 (0)