PageRenderTime 213ms CodeModel.GetById 101ms app.highlight 6ms RepoModel.GetById 104ms app.codeStats 0ms

/src/org/ooc/backend/cdirty/CastWriter.java

http://github.com/nddrylliog/ooc
Java | 51 lines | 43 code | 7 blank | 1 comment | 8 complexity | 8bfdaae873024e31266f17e203c38b89 MD5 | raw file
 1package org.ooc.backend.cdirty;
 2
 3import java.io.IOException;
 4
 5import org.ooc.frontend.model.ArrayAccess;
 6import org.ooc.frontend.model.Cast;
 7import org.ooc.frontend.model.Expression;
 8import org.ooc.frontend.model.VariableAccess;
 9
10public class CastWriter {
11
12	public static void write(Cast cast, CGenerator cgen) throws IOException {
13		if(cast.getType().equals(cast.getInner().getType())) {
14			cast.getInner().accept(cgen);
15			return;
16		}
17		
18		if(cast.getInner().getType().isGeneric() && cast.getInner().getType().getPointerLevel() == 0) {
19			Expression expr = cast.getInner();
20			if(expr instanceof VariableAccess) {
21				VariableAccess access = (VariableAccess) expr;
22				cgen.current.app("*((");
23				cast.getType().accept(cgen);
24				cgen.current.app("*)(");
25				AccessWriter.write(access, false, cgen);
26				cgen.current.app("))");
27				return;
28			} else if(expr instanceof ArrayAccess) {
29				ArrayAccess access = (ArrayAccess) expr;
30				cgen.current.app("*((");
31				cast.getType().accept(cgen);
32				cgen.current.app("*) (");
33				access.getVariable().accept(cgen);
34				cgen.current.app(" + ");
35				// FIXME that's.. probably not quite right =)
36				assert(access.getIndices().size() == 1);
37				access.getIndices().getFirst().accept(cgen);
38				cgen.current.app(" * ").app(cast.getType().getMangledName());
39				cgen.current.app("_class()->size))");
40				return;
41			}
42		}
43		
44		cgen.current.app("((");
45		cast.getType().accept(cgen);
46		cgen.current.app(") (");
47		cast.getInner().accept(cgen);
48		cgen.current.app("))");
49	}
50	
51}