/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

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