void genJavaClassBodyComponents(PrintWriter writer, int depth) {
    for (Iterator it = components.iterator(); it.hasNext(); ) {
      TypeNode tn = (TypeNode) it.next();

      tn.genJavaDeclaration(writer, depth);
    }
  }
 String javaParams() {
   StringBuffer sb = new StringBuffer();
   for (Iterator it = components.iterator(); it.hasNext(); ) {
     TypeNode tn = (TypeNode) it.next();
     sb.append(tn.javaParam());
     if (it.hasNext()) {
       sb.append(", ");
     }
   }
   return sb.toString();
 }
 void genJavaWritingClassBody(PrintWriter writer, int depth, String className) {
   genJavaClassBodyComponents(writer, depth);
   writer.println();
   indent(writer, depth);
   writer.println(className + "(" + javaParams() + ") {");
   for (Iterator it = components.iterator(); it.hasNext(); ) {
     TypeNode tn = (TypeNode) it.next();
     indent(writer, depth + 1);
     writer.println("this." + tn.name() + " = " + tn.name() + ";");
   }
   indent(writer, depth);
   writer.println("}");
 }
Beispiel #4
0
  public Type childExpectedType(Expr child, AscriptionVisitor av) {
    TypeSystem ts = av.typeSystem();

    if (child == expr) {
      if (castType.type().isReference()) {
        return ts.Object();
      } else if (castType.type().isNumeric()) {
        return ts.Double();
      } else if (castType.type().isBoolean()) {
        return ts.Boolean();
      }
    }

    return child.type();
  }
Beispiel #5
0
  /** Type check the expression. */
  public Node typeCheck(TypeChecker tc) throws SemanticException {
    TypeSystem ts = tc.typeSystem();

    if (!ts.isCastValid(expr.type(), castType.type())) {
      throw new SemanticException(
          "Cannot cast the expression of type \""
              + expr.type()
              + "\" to type \""
              + castType.type()
              + "\".",
          position());
    }

    return type(castType.type());
  }
Beispiel #6
0
  public Object constantValue() {
    Object v = expr.constantValue();

    if (v == null) {
      return null;
    }

    if (v instanceof Boolean) {
      if (castType.type().isBoolean()) return v;
    }

    if (v instanceof String) {
      TypeSystem ts = castType.type().typeSystem();
      if (castType.type().equals(ts.String())) return v;
    }

    if (v instanceof Double) {
      double vv = ((Double) v).doubleValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Float) {
      float vv = ((Float) v).floatValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Number) {
      long vv = ((Number) v).longValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    if (v instanceof Character) {
      char vv = ((Character) v).charValue();

      if (castType.type().isDouble()) return new Double((double) vv);
      if (castType.type().isFloat()) return new Float((float) vv);
      if (castType.type().isLong()) return new Long((long) vv);
      if (castType.type().isInt()) return new Integer((int) vv);
      if (castType.type().isChar()) return new Character((char) vv);
      if (castType.type().isShort()) return new Short((short) vv);
      if (castType.type().isByte()) return new Byte((byte) vv);
    }

    // not a constant
    return null;
  }
Beispiel #7
0
 public boolean isConstant() {
   return expr.isConstant() && castType.type().isPrimitive();
 }
  protected void setUp() {
    //                d.on ();

    // Create a time manager to synchronize back tracking
    BTTimeManager timeMan = new BTTimeManager();

    // Create the symbol table
    rt_symtab = new RT_Symbol_Table(timeMan);

    // Create a virtual machine state .
    vms =
        new VMState(
            timeMan, boStatic, toStatic, boHeap, toHeap, boStack, toStack, boScratch, toScratch,
            rt_symtab);

    Cpp_StatementManager sm = new Cpp_StatementManager();
    if (dm == null) dm = new Cpp_DeclarationManager(cem, sm, vms);

    // Prepare the VMState
    vms.setProperty("ASTUtilities", new Cpp_ASTUtilities());
    vms.setProperty("DatumUtilities", new DatumUtilities());

    // create the expression for 5
    five = new ConstInt(ctyInt, "5", 5);

    // create the expression for 1
    one = new ConstInt(ctyInt, "1", 1);

    // create the expression for x
    String xid = "x";
    x_sn = new Cpp_ScopedName(xid);
    x = new ExpId(new TyRef(tyInt), xid, x_sn);

    // create the expression for 5.0
    ffive = new ConstFloat(ctyFloat, "5.0", 5.0);

    // create the expression for 1.0
    fone = new ConstFloat(ctyFloat, "1.0", 1.0);

    // create the expression for y
    String yid = "y";
    y_sn = new Cpp_ScopedName(yid);
    y = new ExpId(new TyRef(tyFloat), yid, y_sn);

    tyPointer = new TyPointer();
    tyPointer.addToEnd(tyInt);
    // create the expression for p
    String pid = "p";
    p = new ExpId(new TyRef(tyPointer), pid, new Cpp_ScopedName(pid));

    tyArray = new TyArray();
    tyArray.addToEnd(tyInt);
    // create the expression for a
    String aid = "a";
    a = new ExpId(new TyRef(tyArray), aid, new Cpp_ScopedName(aid));

    String idC = "C";
    sn_C = new Cpp_ScopedName(idC);
    tyClass = new TyClass(idC, sn_C, null);
    // create the expression for c
    String cid = "c";
    c_sn = new Cpp_ScopedName(cid);
    c = new ExpId(new TyRef(tyClass), cid, c_sn);

    String bid = "b";
    b = new ExpId(new TyRef(tyBool), bid, new Cpp_ScopedName(bid));

    _true = new ConstInt(tyBool, "true", 1);

    // create a string constant
    str_const = (ConstStr) Literals.make_string_const("\"blah\"", vms);
    // array of char
    tyArrayChar = new TyArray();
    tyArrayChar.addToEnd(tyChar);
    // create the expression for a
    String acid = "ac";
    ac = new ExpId(new TyRef(tyArrayChar), acid, new Cpp_ScopedName(acid));

    // reference to int
    tyRefInt = new TyRef(tyInt);

    // create the id expression for rint
    String riid = "ri";
    r_sn = new Cpp_ScopedName(riid);
    rint = new ExpId(new TyRef(tyRefInt), riid, r_sn);

    // constant reference to int
    ctyRefInt = new TyRef(ctyInt);
    ctyRefInt.setAttributes(Cpp_Specifiers.CVQ_CONST);

    // create the id expression for rint
    String criid = "cri";
    crint = new ExpId(new TyRef(ctyRefInt), criid, new Cpp_ScopedName(criid));

    // function taking no parameters and returning int
    tyFun = new TyFun(new Vector(), false);
    tyFun.addToEnd(tyInt);

    // create the following scope and declaration space
    // int x;
    // int &r;
    // int foo ();
    // class C {
    // 	float y;
    // 	int bar ();
    // };
    // C c;
    // C *pc;

    Cpp_SpecifierSet sps = new Cpp_SpecifierSet();
    NodeList nl = new NodeList();
    Declaration decl;

    // int x
    decl = new Declaration(VARIABLE_LF, x_sn, null, sps, tyInt);
    decl.setDefinition(decl);
    decl.getCategory().set(dm.type_extractor.categorizeType(tyInt));
    st.addDeclaration(decl);

    // int &r
    decl = new Declaration(VARIABLE_LF, r_sn, null, sps, tyRefInt);
    decl.setDefinition(decl);
    decl.getCategory().set(dm.type_extractor.categorizeType(tyRefInt));
    st.addDeclaration(decl);

    // int foo ()
    foo_sn = new Cpp_ScopedName("foo");
    st.addDefiningDeclaration(foo_sn, REG_FN_LF, tyFun);

    // class C
    ClassHead chc = new ClassHead(ClassHead.CLASS, sn_C, new Vector());
    Declaration c_decl = st.addDefiningDeclaration(chc);

    st.enterScope(c_decl);

    // float y
    decl = new Declaration(VARIABLE_LF, y_sn, null, sps, tyFloat);
    decl.setDefinition(decl);
    decl.getCategory().set(dm.type_extractor.categorizeType(tyFloat));
    st.addDeclaration(decl);

    // int bar ()
    bar_sn = new Cpp_ScopedName("bar");
    st.addDefiningDeclaration(bar_sn, MEM_FN_LF, tyFun);

    st.exitScope();

    // C c
    decl = new Declaration(VARIABLE_LF, c_sn, null, sps, tyClass);
    decl.setDefinition(c_decl);
    decl.getCategory().set(dm.type_extractor.categorizeClass(decl));
    st.addDeclaration(decl);

    tyPointerClass = new TyPointer();
    tyPointerClass.addToEnd(tyClass);
    // C *pc
    String pcid = "pc";
    pc_sn = new Cpp_ScopedName(pcid);
    pc = new ExpId(new TyRef(tyPointer), pcid, pc_sn);
    decl = new Declaration(VARIABLE_LF, pc_sn, null, sps, tyPointerClass);
    decl.setDefinition(decl);
    decl.getCategory().set(dm.type_extractor.categorizeType(tyPointerClass));
    st.addDeclaration(decl);
  }
 void genJavaWrites(PrintWriter writer, int depth) {
   for (Iterator it = components.iterator(); it.hasNext(); ) {
     TypeNode tn = (TypeNode) it.next();
     tn.genJavaWrite(writer, depth, tn.name());
   }
 }