PageRenderTime 19ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 52 lines | 40 code | 10 blank | 2 comment | 11 complexity | 7d463d79d7cfb9b204ca99fe8c3fefc2 MD5 | raw file
 1package org.ooc.backend.cdirty;
 2
 3import java.io.IOException;
 4
 5import org.ooc.frontend.model.CoverDecl;
 6import org.ooc.frontend.model.FunctionDecl;
 7import org.ooc.frontend.model.TypeDecl;
 8import org.ooc.frontend.model.TypeParam;
 9import org.ooc.frontend.model.VariableAccess;
10import org.ooc.frontend.model.VariableDecl;
11
12public class LocalAccessWriter {
13
14	public static void write(VariableAccess variableAccess, boolean doTypeParams, CGenerator cgen) throws IOException {
15		write(variableAccess, doTypeParams, cgen, 0);
16	}
17	
18	public static void write(VariableAccess variableAccess, boolean doTypeParams, CGenerator cgen, int refOffset) throws IOException {
19		
20		if(variableAccess.getRef() instanceof TypeDecl && !(variableAccess.getRef() instanceof TypeParam)) {
21			TypeDecl ref = (TypeDecl) variableAccess.getRef();
22			if(ref instanceof CoverDecl && ((CoverDecl)ref).isAddon()) {
23				ref = ((CoverDecl)ref).getBase();
24			}
25			cgen.current.app(ref.getUnderName()).app("_class()");
26			return;
27		}
28
29		// duplicated code with MemberAccessWriter: modularize!
30		int refLevel = variableAccess.getRef().getType().getReferenceLevel();
31		refLevel += refOffset;
32		
33		if(refLevel > 0) {
34			cgen.current.app('(');
35			for(int i = 0; i < refLevel; i++) {
36				cgen.current.app('*');
37			}
38		}
39		if(variableAccess.getRef() instanceof VariableDecl) {
40			VariableDecl ref = (VariableDecl)variableAccess.getRef();
41			cgen.current.app(ref.getFullName());
42		} else if(variableAccess.getRef() instanceof FunctionDecl) {
43			// closure, man!
44			cgen.current.app(((FunctionDecl)variableAccess.getRef()).getFullName());
45		} else {
46			cgen.current.app(variableAccess.getRef().getExternName());
47		}
48		if(refLevel > 0) cgen.current.app(')');
49		
50	}
51	
52}