1313import java .util .Map ;
1414
1515public class ILStackFrame {
16-
1716 public final Either <ImFunction , ImCompiletimeExpr > f ;
1817 public final ILconst [] args ;
1918 public final WPos trace ;
2019 public final @ Nullable ILconstObject receiver ;
2120 public final Map <ImTypeVar , ImType > typeSubstitutions ;
2221
23- public ILStackFrame (ImFunction f , @ Nullable ILconstObject receiver , ILconst [] args2 , WPos trace , Map <ImTypeVar , ImType > typeSubstitutions ) {
22+ // last executed element *within this frame*
23+ public @ Nullable de .peeeq .wurstscript .jassIm .Element currentElement ;
24+
25+ public ILStackFrame (ImFunction f , @ Nullable ILconstObject receiver , ILconst [] args2 , WPos trace ,
26+ Map <ImTypeVar , ImType > typeSubstitutions ) {
2427 this .f = Either .left (f );
2528 this .receiver = receiver ;
2629 this .args = args2 ;
@@ -36,35 +39,40 @@ public ILStackFrame(ImCompiletimeExpr f, WPos trace) {
3639 this .typeSubstitutions = Collections .emptyMap ();
3740 }
3841
42+ public @ Nullable WPos getCurrentSourcePos () {
43+ if (currentElement != null ) {
44+ try {
45+ return currentElement .attrTrace ().attrSource ();
46+ } catch (Exception ignored ) {}
47+ }
48+ return trace ;
49+ }
50+
3951 public String getMessage () {
4052 StringBuilder sb = new StringBuilder ();
41- if (trace != null && !trace .isArtificial ()) {
42- String file = new File (trace .getFile ()).getName ();
43- sb .append (" ╚ " ).append (file ).append (":" ).append (trace .getLine ());
53+
54+ WPos pos = getCurrentSourcePos ();
55+ if (pos != null && !pos .isArtificial ()) {
56+ String file = new File (pos .getFile ()).getName ();
57+ sb .append (" ╚ " ).append (file ).append (":" ).append (pos .getLine ());
4458 }
59+
4560 if (f .isLeft ()) {
46- sb .append (" calling " ).append (f .getLeft ().getName ()).append ("(" );
47- boolean first = true ;
48- for (ILconst arg : args ) {
49- if (!first ) {
50- sb .append (", " );
51- }
52- sb .append (arg );
53- first = false ;
61+ sb .append (" inside call " ).append (f .getLeft ().getName ()).append ("(" );
62+ for (int i = 0 ; i < args .length ; i ++) {
63+ if (i > 0 ) sb .append (", " );
64+ sb .append (args [i ]);
5465 }
5566 sb .append (")" );
5667 } else {
5768 sb .append ("... when executing compiletime expression " );
5869 }
59-
6070 return sb .toString ();
6171 }
6272
6373 public CompileError makeCompileError () {
64- return new CompileError (trace , getMessage ());
65- }
66-
67- public WPos getTrace () {
68- return trace ;
74+ // Use current element position if available:
75+ WPos pos = getCurrentSourcePos ();
76+ return new CompileError (pos != null ? pos : trace , getMessage ());
6977 }
7078}
0 commit comments