PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 111 lines | 82 code | 24 blank | 5 comment | 31 complexity | 758e03b1b993b4b275ffeaa7933eeabb 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.FuncType;
  7import org.ooc.frontend.model.FunctionDecl;
  8import org.ooc.frontend.model.Type;
  9import org.ooc.frontend.model.VariableDecl;
 10
 11public class CoverDeclWriter {
 12
 13	public static void write(CoverDecl cover, CGenerator cgen) throws IOException {
 14		
 15		cgen.current = cgen.hw;
 16
 17		if(cover.getVersion() != null) {
 18			VersionBlockWriter.writeVersionBlockStart(cover.getVersion(), cgen);
 19		}
 20		
 21		// addons only add functions to an already imported cover, so
 22		// we don't need to struct it again, it would confuse the C compiler
 23		if(!cover.isAddon() && !cover.isExtern() && cover.getFromType() == null) {
 24			cgen.current.app("struct _").app(cover.getUnderName()).app(' ').openBlock();
 25			for(VariableDecl decl: cover.getVariables()) {
 26				cgen.current.nl();
 27				if(VariableDeclWriter.write(decl, cgen)) {
 28					cgen.current.app(';');
 29				}
 30			}
 31			cgen.current.closeBlock().app(';').nl();
 32		}
 33		
 34		if(cover.getVersion() != null) {
 35			VersionBlockWriter.writeVersionBlockEnd(cgen);
 36		}
 37		
 38		/* and now the functions */
 39		
 40		cgen.current = cgen.cw;
 41		
 42		//cgen.current.app("/* cover " + cover.getName() + " has version " +
 43		//		(cover.getVersion() != null ? cover.getVersion().getVersion().toString() : "null") + " */");
 44		if(cover.getVersion() != null) {
 45			VersionBlockWriter.writeVersionBlockStart(cover.getVersion(), cgen);
 46		}
 47		
 48		for(FunctionDecl decl: cover.getFunctions()) {
 49			if(decl.getName().equals("class") && decl.getSuffix().length() == 0) {
 50				if(!cover.isAddon()) writeClassFunction(cover, cgen);
 51			} else {
 52				decl.accept(cgen);
 53				cgen.current.nl();
 54			}
 55		}
 56		
 57		if(cover.getVersion() != null) {
 58			VersionBlockWriter.writeVersionBlockEnd(cgen);
 59		}
 60	}
 61
 62	public static void writeClassFunction(CoverDecl cover, CGenerator cgen) throws IOException {
 63		
 64		cgen.current = cgen.fw;
 65        
 66        cgen.current.nl().app("lang_types__Class *"). app(cover.getUnderName()). app("_class();");
 67        
 68        cgen.current = cgen.cw;
 69        
 70        cgen.current.nl().
 71        app("lang_types__Class *"). app(cover.getUnderName()). app("_class() {"). tab(). nl().
 72            app("static lang_types__Class _class = {"). tab(). nl().
 73                app(".size = sizeof("). app(cover.getUnderName()). app("),"). nl().
 74                app(".instanceSize = sizeof("). app(cover.getUnderName()). app("),"). nl().
 75                app(".name = \""). app(cover.getName()). app("\"").
 76            untab(). nl(). app("};").
 77            nl(). app("return &_class;").
 78        untab(). nl(). app("}").
 79        nl();
 80		
 81	}
 82	
 83	public static void writeTypedef(CoverDecl cover, CGenerator cgen) throws IOException {
 84		
 85		if(cover.getVersion() != null) {
 86			VersionBlockWriter.writeVersionBlockStart(cover.getVersion(), cgen);
 87		}
 88		
 89		if(!cover.isAddon() && !cover.isExtern()) {
 90			Type fromType = cover.getFromType();
 91			if(fromType == null) {
 92				cgen.current.nl().app("typedef struct _").app(cover.getUnderName())
 93					.app(' ').app(cover.getUnderName()).app(';');
 94			} else {
 95				cgen.current.nl().app("typedef ");
 96				if(fromType instanceof FuncType) {
 97					TypeWriter.writeFuncPointer(((FuncType) fromType).getDecl(), cover.getUnderName(), cgen);
 98				} else {
 99					TypeWriter.writeSpaced(fromType.getGroundType(), cgen, true);
100					cgen.current.app(cover.getUnderName());
101				}
102				cgen.current.app(';');
103			}
104		}
105		
106		if(cover.getVersion() != null) {
107			VersionBlockWriter.writeVersionBlockEnd(cgen);
108		}
109	}
110	
111}