nitj: implement instance initialization and ANewExprs
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 6 Jul 2015 16:49:20 +0000 (12:49 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 21 Jul 2015 21:23:21 +0000 (17:23 -0400)
Attributes are not handled in this commit.

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/compiler/java_compiler.nit

index 85613cf..0933a36 100644 (file)
@@ -754,6 +754,17 @@ class JavaCompilerVisitor
                # TODO return array_instance(varargs, elttype)
        end
 
+       # Nit instances
+
+       # Generate a alloc-instance + init-attributes
+       fun init_instance(mtype: MClassType): RuntimeVariable do
+               var recv = new_recv(mtype)
+               var mclass = mtype.mclass
+               add("{recv} = new RTVal({mclass.rt_name}.get{mclass.rt_name}());")
+               # TODO init attributes
+               return recv
+       end
+
        # Utils
 
        # Display a info message
@@ -1139,6 +1150,31 @@ redef class ASendExpr
        end
 end
 
+redef class ANewExpr
+       redef fun expr(v)
+       do
+               var mtype = self.recvtype
+               assert mtype != null
+
+               if mtype.mclass.name == "NativeArray" then
+                       # TODO handle native arrays
+                       v.info("NOT YET IMPLEMENTED new NativeArray")
+               end
+
+               var recv = v.init_instance(mtype)
+
+               var callsite = self.callsite
+               if callsite == null then return recv
+
+               var args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, self.n_args.n_exprs)
+               var res2 = v.compile_callsite(callsite, args)
+               if res2 != null then
+                       return res2
+               end
+               return recv
+       end
+end
+
 redef class ASelfExpr
        redef fun expr(v) do return v.frame.as(not null).arguments.first
 end