1717package com .duy .pascal .backend .ast .function_declaretion .builtin ;
1818
1919
20- import com .duy .pascal .backend .parse_exception .ParsingException ;
21- import com .duy .pascal .backend .linenumber .LineInfo ;
22- import com .duy .pascal .backend .pascaltypes .ArgumentType ;
23- import com .duy .pascal .backend .pascaltypes .DeclaredType ;
24- import com .duy .pascal .backend .pascaltypes .JavaClassBasedType ;
25- import com .duy .pascal .backend .pascaltypes .RuntimeType ;
20+ import com .duy .pascal .backend .ast .codeunit .RuntimeExecutableCodeUnit ;
2621import com .duy .pascal .backend .ast .expressioncontext .CompileTimeContext ;
2722import com .duy .pascal .backend .ast .expressioncontext .ExpressionContext ;
2823import com .duy .pascal .backend .ast .instructions .Executable ;
24+ import com .duy .pascal .backend .ast .runtime_value .VariableContext ;
25+ import com .duy .pascal .backend .ast .runtime_value .references .PascalReference ;
2926import com .duy .pascal .backend .ast .runtime_value .value .FunctionCall ;
3027import com .duy .pascal .backend .ast .runtime_value .value .RuntimeValue ;
31- import com .duy .pascal .backend .ast .runtime_value .references .PascalReference ;
32- import com .duy .pascal .backend .ast .runtime_value .VariableContext ;
33- import com .duy .pascal .backend .ast .codeunit .RuntimeExecutableCodeUnit ;
28+ import com .duy .pascal .backend .linenumber .LineInfo ;
29+ import com .duy .pascal .backend .parse_exception .ParsingException ;
30+ import com .duy .pascal .backend .pascaltypes .ArgumentType ;
31+ import com .duy .pascal .backend .pascaltypes .DeclaredType ;
32+ import com .duy .pascal .backend .pascaltypes .JavaClassBasedType ;
33+ import com .duy .pascal .backend .pascaltypes .PointerType ;
34+ import com .duy .pascal .backend .pascaltypes .RuntimeType ;
3435import com .duy .pascal .backend .runtime_exception .RuntimePascalException ;
3536
3637/**
@@ -43,16 +44,18 @@ public class CastObjectFunction implements IMethodDeclaration {
4344 new RuntimeType (new JavaClassBasedType (Object .class ), false )};
4445
4546 @ Override
46- public String getName () {
47+ public String getName () {
4748 return "cast" ;
4849 }
4950
5051 @ Override
5152 public FunctionCall generateCall (LineInfo line , RuntimeValue [] arguments ,
52- ExpressionContext f ) throws ParsingException {
53+ ExpressionContext f ) throws ParsingException {
5354 RuntimeValue pointer = arguments [0 ];
5455 RuntimeValue value = arguments [1 ];
55- return new InstanceObjectCall (pointer , value , line );
56+ PointerType declType = (PointerType ) pointer .getType (f ).declType ;
57+ Class <?> storageClass = declType .pointedToType .getStorageClass ();
58+ return new InstanceObjectCall (pointer , value , storageClass , line );
5659 }
5760
5861 @ Override
@@ -77,12 +80,14 @@ public String description() {
7780
7881 private class InstanceObjectCall extends FunctionCall {
7982 private RuntimeValue value ;
83+ private Class <?> storageClass ;
8084 private LineInfo line ;
8185 private RuntimeValue pointer ;
8286
83- InstanceObjectCall (RuntimeValue pointer , RuntimeValue value , LineInfo line ) {
87+ InstanceObjectCall (RuntimeValue pointer , RuntimeValue value , Class <?> storageClass , LineInfo line ) {
8488 this .value = value ;
8589 this .pointer = pointer ;
90+ this .storageClass = storageClass ;
8691 this .line = line ;
8792 }
8893
@@ -105,13 +110,13 @@ public Object compileTimeValue(CompileTimeContext context) {
105110 @ Override
106111 public RuntimeValue compileTimeExpressionFold (CompileTimeContext context )
107112 throws ParsingException {
108- return new InstanceObjectCall (pointer , value , line );
113+ return new InstanceObjectCall (pointer , value , storageClass , line );
109114 }
110115
111116 @ Override
112117 public Executable compileTimeConstantTransform (CompileTimeContext c )
113118 throws ParsingException {
114- return new InstanceObjectCall (pointer , value , line );
119+ return new InstanceObjectCall (pointer , value , storageClass , line );
115120 }
116121
117122 @ Override
@@ -129,13 +134,8 @@ public Object getValueImpl(VariableContext f, RuntimeExecutableCodeUnit<?> main)
129134 //indexOf value of arg 2
130135 Object value = this .value .getValue (f , main );
131136
132- //indexOf value of variable
133- Object o = pointer .get ();
134- //indexOf class of variable
135- Class <?> aClass = o .getClass ();
136-
137137 //cast object to type of variable
138- Object casted = aClass .cast (value );
138+ Object casted = storageClass .cast (value );
139139
140140 //set value
141141 pointer .set (casted );
0 commit comments