/**
   * Translates the body of the package and writes it to the output stream.
   *
   * @param out The output stream.
   * @return The output stream.
   */
  public Printer translate(Printer out) {
    // Include the header file
    out.p("#include \"").p(getFilename()).pln(".h\"").pln();

    // Print the array template specializations for the classes in this package
    out.pln("namespace __rt {").incr();
    for (JavaFile f : files) {
      for (JavaClass cls : f.getClasses()) {
        cls.translateArrayTemplate(out);
      }
    }
    out.decr().pln("}").pln();

    // Add the namespace
    for (String part : pkg) {
      out.indent().p("namespace ").p(part).pln(" {").incr();
    }

    // Print all the files in the package
    for (JavaFile f : files) {
      for (JavaClass cls : f.getClasses()) {
        cls.translate(out).pln();
      }
      out.pln();
    }

    // Close the namespace
    for (int i = 0; i < pkg.size(); i++) {
      out.decr().indent().pln("}");
    }

    // If this package contains the main file, print the main method here
    if (null != main) {
      out.pln("int main(int argc, char *argv[]) {").incr();
      out.indent().pln("__rt::Ptr<__rt::Array<String> > args = new __rt::Array<String>(argc-1);");
      out.indent().pln("for (int i = 1; i < argc; i++) {").incr();
      out.indent().pln("(*args)[i-1] = __rt::literal(argv[i]);");
      out.decr().indent().pln("}");
      out.indent();
      if (!getNamespace().equals("")) out.p(getNamespace()).p("::");
      out.p("__").p(main.getPublicClass().getName()).pln("::main$array1_String(args);");
      out.decr().pln("}");
    }

    return out;
  }
Ejemplo n.º 2
0
 /**
  * Print a tool header to the specified printer. This method prints a header documenting the tool
  * name, version, copyright, and current time. It also prints a warning not to edit the result.
  *
  * @param printer The printer.
  */
 public void printHeader(Printer printer) {
   printer.sep();
   printer.indent().pln("// This file has been generated by");
   printer.indent().p("// ").p(getName()).p(", version ").p(getVersion()).pln(',');
   printer.p("// ").p(getCopy()).pln(',');
   Date now = new Date();
   printer
       .indent()
       .p("// on ")
       .p(DateFormat.getDateInstance(DateFormat.FULL).format(now))
       .p(" at ")
       .p(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now))
       .pln('.');
   printer.indent().pln("// Edit at your own risk.");
   printer.sep();
   printer.pln();
 }
  public void print(
      Printer out) // TODO: Print the actual class (along with the already printed vtable)
      {
    if (toPrint) // TODO: Add constructors in class struct
    {
      int t, i, j;
      int dims;

      // Print the class
      out.indent();
      out.pln("struct __" + name);
      out.indent();
      out.pln("{");

      out.incr();

      out.indent();
      out.pln("__" + name + "_VT* __vptr;");
      out.pln();

      for (t = 0; t < members.size(); t++) {
        out.indent();
        out.pln(members.get(t).getType() + " " + members.get(t).getName() + ";");
      }

      out.pln();

      for (t = 0; t < constructors.size(); t++) {
        out.indent();

        out.p("__" + name + "(");

        for (i = 0; i < constructors.get(t).size(); i++) {
          dims = constructors.get(t).get(i).getDimensions();

          for (j = 0; j < dims; j++) {
            out.p("__rt::Ptr<__rt::Array<");
          }

          out.p(constructors.get(t).get(i).getType());

          for (j = 0; j < dims; j++) {
            out.p(">, __rt::array_policy>");

            if (j < dims - 1) out.p(" ");
          }

          if (i < constructors.get(t).size() - 1) out.p(", ");
        }

        out.pln(");");
      }

      out.pln();

      out.indent();
      out.pln("static __" + name + "* init(__" + name + "*);");

      out.pln();

      for (t = 0; t < methods.size(); t++) {
        ClassMethod m = methods.get(t);

        dims = m.getReturnType().getDimensions();

        out.indent();
        out.p("static ");

        for (j = 0; j < dims; j++) {
          out.p("__rt::Ptr<__rt::Array<");
        }

        out.p(m.getReturnType().getType() + " " + m.getName() + "(");

        for (j = 0; j < dims; j++) {
          out.p(">, __rt::array_policy>");

          if (j < dims - 1) out.p(" ");
        }

        if (!m.isStatic()) {
          out.p(m.getDefiningClass());

          if (m.getParameters().size() > 0) out.p(", ");
        }

        for (i = 0; i < m.getParameters().size(); i++) {
          dims = m.getParameters().get(i).getDimensions();

          for (j = 0; j < dims; j++) {
            out.p("__rt::Ptr<__rt::Array<");
          }

          out.p(m.getParameters().get(i).getType());

          for (j = 0; j < dims; j++) {
            out.p(">, __rt::array_policy>");

            if (j < dims - 1) out.p(" ");
          }

          if (i < m.getParameters().size() - 1) out.p(", ");
        }

        out.pln(");");
      }

      out.pln();

      out.indent();
      out.pln("static Class __class();");
      out.indent();
      out.pln("static __" + name + "_VT __vtable;");

      out.decr();

      out.indent();
      out.pln("};");
      out.pln();

      // Print the vtable
      out.indent();
      out.pln("struct __" + name + "_VT");
      out.indent();
      out.pln("{");

      out.incr();

      out.indent();
      out.pln("Class __isa;");
      out.indent();
      out.pln("void (*__delete)(__" + name + "*);");

      for (t = 0; t < methods.size(); t++) {
        ClassMethod m = methods.get(t);

        if (!m.isStatic()) {
          dims = m.getReturnType().getDimensions();

          out.indent();

          for (j = 0; j < dims; j++) {
            out.p("__rt::Ptr<__rt::Array<");
          }

          out.p(m.getReturnType().getType());

          for (j = 0; j < dims; j++) {
            out.p(">, __rt::array_policy>");

            if (j < dims - 1) out.p(" ");
          }

          out.p(" (*" + m.getName() + ")");

          out.p("(");

          if (m.getName().equals("getClass")) out.p(name);
          else out.p(m.getDefiningClass());

          if (m.getParameters().size() > 0) out.p(", ");

          for (i = 0; i < m.getParameters().size(); i++) {
            out.p(m.getParameters().get(i).getType());

            if (i < m.getParameters().size() - 1) out.p(", ");
          }

          out.pln(");");
        }
      }

      out.pln();

      out.indent();
      out.pln("__" + name + "_VT() :");

      out.incr();

      out.indent();
      out.pln("__isa(__" + name + "::__class()),");
      out.indent();
      out.p("__delete(__rt::__delete<__" + name + ">)");

      for (t = 0; t < methods.size(); t++) {
        ClassMethod m = methods.get(t);

        if (!m.isStatic()) {
          out.pln(",");
          out.indent();

          out.p(m.getName() + "(");

          if (m.getName().equals("getClass"))
            out.p("(Class(*)(" + name + "))&__" + m.getDefiningClass() + "::" + m.getName());
          else out.p("&__" + m.getDefiningClass() + "::" + m.getName());

          out.p(")");
        }
      }

      out.pln();

      out.decr();

      out.indent();
      out.pln("{");
      out.indent();
      out.pln("}");

      out.decr();
      out.indent();
      out.pln("};");
      out.pln();

      // Print internal __class() method
      out.indent();
      out.pln("Class __" + name + "::__class()");
      out.indent();
      out.pln("{");

      out.incr();

      out.indent();
      out.pln(
          "static Class k = new __Class(__rt::literal(\""
              + name
              + "\"), __"
              + superclass
              + "::__class());");

      out.indent();
      out.pln("return k;");

      out.decr();

      out.indent();
      out.pln("}");

      out.pln();
    }

    int k;

    for (k = 0; k < children.size(); k++) {
      children.get(k).print(out);
    }
  }
  /**
   * Writes the C++ header for the package to the specified output stream.
   *
   * @param out The output stream.
   * @return The output stream.
   */
  public Printer translateHeader(Printer out) {
    // Get any imports
    Set<JavaPackage> using = new HashSet<JavaPackage>();
    for (JavaFile file : files) {
      Set<JavaPackage> imports = file.getImports();
      for (JavaPackage i : imports) {
        if (!i.getPath().equals(getPath())) using.add(i);
      }
    }

    // Include any imported headers
    out.pln("#pragma once").pln();
    out.pln("#include <iostream>");
    out.pln("#include <sstream>").pln();
    out.pln("#include \"include/java_lang.h\"");
    for (JavaPackage i : using) {
      out.p("#include \"").p(i.getFilename()).pln(".h\"");
    }

    // Declare namespaces being used
    out.pln().pln("using namespace java::lang;");
    for (JavaPackage i : using) {
      out.p("using namespace ").p(i.getNamespace()).pln(";");
    }
    out.pln();

    // Add the namespace
    for (String part : pkg) {
      out.indent().p("namespace ").p(part).pln(" {").incr();
    }

    // Declare the class structs
    for (JavaFile file : files) {
      file.orderClasses();
      for (JavaClass cls : file.getClasses()) {
        String className = cls.getName();
        out.indent().p("struct __").p(className).pln(";");
        out.indent().p("struct __").p(className).pln("_VT;");
        out.pln()
            .indent()
            .p("typedef __rt::Ptr<__")
            .p(className)
            .p("> ")
            .p(className)
            .pln(";")
            .pln();
      }
    }

    // Print header structs
    for (JavaFile file : files) {
      for (JavaClass cls : file.getClasses()) {
        cls.translateHeader(out).pln();
      }
    }

    // Close the namespace
    for (int i = 0; i < pkg.size(); i++) {
      out.decr().indent().pln("}");
    }

    return out;
  }