Exemple #1
0
  public Node typeCheck(TypeChecker tc) throws SemanticException {

    ETValueHolder vh = (ETValueHolder) ((csTypeSystem_c) tc.typeSystem()).getValueHolder();
    if (!vh.isDynamicValue(((CSObjectType) obj.type()).getAliasName(), ETTYPE.MODE))
      throw new SemanticException("Using argument must be dynamic", obj.position());
    return this;
  }
  public Node typeCheck(TypeChecker tc) throws SemanticException {
    JL5Call_c n = null;
    JL5TypeSystem ts = (JL5TypeSystem) tc.typeSystem();
    JL5Context c = (JL5Context) tc.context();
    ReferenceType targetType = null;
    List<Type> explicitTypeArgs = null;
    List<Type> paramTypes = new ArrayList<Type>();

    if (typeArguments != null && !typeArguments.isEmpty()) {
      explicitTypeArgs = new ArrayList<Type>();
      if (target() == null) {
        // should not actually happen. grammar doesn't allow it
        throw new SemanticException(
            "Explicit target required when using explicit type arguments", position());
      }
      for (Iterator it = typeArguments().iterator(); it.hasNext(); ) {
        explicitTypeArgs.add(((TypeNode) it.next()).type());
      }
    }

    for (Iterator i = this.arguments().iterator(); i.hasNext(); ) {
      Expr e = (Expr) i.next();
      paramTypes.add(e.type());
    }

    JL5MethodInstance mi;
    // JLS 15.12.1
    if (target == null) {
      return typeCheckNullTarget(tc, paramTypes, explicitTypeArgs);
    } else {
      targetType = this.findTargetType();
      mi = ts.findJL5Method(targetType, name, paramTypes, explicitTypeArgs, c);
    }

    boolean staticContext = (this.target instanceof TypeNode);

    if (staticContext && !mi.flags().isStatic()) {
      throw new SemanticException(
          "Cannot call non-static method "
              + this.name
              + " of "
              + targetType
              + " in static "
              + "context.",
          this.position());
    }

    if (this.target instanceof Special
        && ((Special) this.target).kind() == Special.SUPER
        && mi.flags().isAbstract()) {
      throw new SemanticException(
          "Cannot call an abstract method " + "of the super class", this.position());
    }

    n = (JL5Call_c) this.methodInstance(mi).type(mi.returnType());
    // n.checkConsistency(c);
    return n;
  }
 public ConstructorCall mangledCall(ConstructorCall cc, AJNodeFactory nf, AJTypeSystem ts) {
   if (flags().isPrivate() || flags.isPackage()) {
     Expr nl = nf.NullLit(cc.position());
     nl = nl.type(mangleType);
     List args = new LinkedList(cc.arguments());
     args.add(nl);
     ConstructorCall nc = (ConstructorCall) cc.arguments(args);
     return nc.constructorInstance(mangled());
   } else return cc;
 }
Exemple #4
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());
  }
Exemple #5
0
  public List throwTypes(TypeSystem ts) {
    if (expr.type().isReference()) {
      return Collections.singletonList(ts.ClassCastException());
    }

    return Collections.EMPTY_LIST;
  }
Exemple #6
0
  public List acceptCFG(CFGBuilder v, List succs) {
    v.visitCFGList(inits, (cond != null ? cond.entry() : body.entry()));

    if (cond != null) {
      if (condIsConstantTrue()) {
        v.visitCFG(cond, body.entry());
      } else {
        v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, body.entry(), FlowGraph.EDGE_KEY_FALSE, this);
      }
    }

    v.push(this).visitCFG(body, continueTarget());
    v.visitCFGList(iters, (cond != null ? cond.entry() : body.entry()));

    return succs;
  }
Exemple #7
0
  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("({");

    int count = 0;

    for (Iterator<Stmt> i = statements.iterator(); i.hasNext(); ) {
      if (count++ > 2) {
        sb.append(" ...");
        break;
      }

      Stmt n = i.next();
      sb.append(" ");
      sb.append(n.toString());
    }

    if (result != null) {
      sb.append(" ");
      sb.append(result.toString());
    }

    sb.append(" })");
    return sb.toString();
  }
  /** Flatten complex expressions within the AST */
  public Node leave(Node old, Node n, NodeVisitor v) {
    if (n == noFlatten) {
      noFlatten = null;
      return n;
    }

    if (n instanceof Block) {
      List l = (List) stack.removeFirst();
      return ((Block) n).statements(l);
    } else if (n instanceof Stmt && !(n instanceof LocalDecl)) {
      List l = (List) stack.getFirst();
      l.add(n);
      return n;
    } else if (n instanceof Expr
        && !(n instanceof Lit)
        && !(n instanceof Special)
        && !(n instanceof Local)) {

      Expr e = (Expr) n;

      if (e instanceof Assign) {
        return n;
      }

      // create a local temp, initialized to the value of the complex
      // expression

      String name = newID();
      LocalDecl def =
          nf.LocalDecl(
              e.position(), Flags.FINAL, nf.CanonicalTypeNode(e.position(), e.type()), name, e);
      def = def.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));

      List l = (List) stack.getFirst();
      l.add(def);

      // return the local temp instead of the complex expression
      Local use = nf.Local(e.position(), name);
      use = (Local) use.type(e.type());
      use = use.localInstance(ts.localInstance(e.position(), Flags.FINAL, e.type(), name));
      return use;
    }

    return n;
  }
Exemple #9
0
  public Type childExpectedType(Expr child, AscriptionVisitor av) {
    TypeSystem ts = av.typeSystem();

    if (child == cond) {
      return ts.Boolean();
    }

    return child.type();
  }
  /*
      private Node checkTypeArguments(TypeChecker tc, JL5Call_c n) throws SemanticException {
          JL5MethodInstance mi = (JL5MethodInstance) n.methodInstance();
          JL5TypeSystem ts = (JL5TypeSystem) tc.typeSystem();

          // can only call a method with type args if it was declared as generic
          if (!typeArguments.isEmpty() && !mi.isGeneric()) {
              throw new SemanticException("Cannot call method: " + mi.name() + " with type arguments", position());
          }

          if (!typeArguments().isEmpty() && typeArguments.size() != mi.typeVariables().size()) {
              throw new SemanticException("Cannot call " + n.name()
                      + " with wrong number of type arguments", position());
          }

          // wildcards are not allowed for type args for generic call
          for (int i = 0; i < typeArguments.size(); i++) {
              TypeNode correspondingArg = (TypeNode) typeArguments.get(i);
              if (correspondingArg instanceof BoundedTypeNode) {
                  throw new SemanticException("Wildcard argument not allowed here", correspondingArg.position());
              }
          }
          return n;
      }
  */
  public String toString() {
    StringBuffer sb = new StringBuffer();
    if (!targetImplicit) sb.append(target.toString());

    if ((typeArguments != null) && typeArguments.size() != 0) {
      sb.append("<");
      for (Iterator it = typeArguments.iterator(); it.hasNext(); ) {
        sb.append(it.next().toString());
        if (it.hasNext()) {
          sb.append(", ");
        }
      }
      sb.append(">");
    }

    if (!targetImplicit) sb.append(".");

    sb.append(name);
    sb.append("(");

    int count = 0;

    for (Iterator i = arguments.iterator(); i.hasNext(); ) {
      if (count++ > 2) {
        sb.append("...");
        break;
      }

      Expr n = (Expr) i.next();
      sb.append(n.toString());

      if (i.hasNext()) {
        sb.append(", ");
      }
    }

    sb.append(")");
    return sb.toString();
  }
Exemple #11
0
  /** Type check the statement. */
  public Node typeCheck(TypeChecker tc) throws SemanticException {
    TypeSystem ts = tc.typeSystem();

    // Check that all initializers have the same type.
    // This should be enforced by the parser, but check again here,
    // just to be sure.
    Type t = null;

    for (Iterator i = inits.iterator(); i.hasNext(); ) {
      ForInit s = (ForInit) i.next();

      if (s instanceof LocalDecl) {
        LocalDecl d = (LocalDecl) s;
        Type dt = d.type().type();
        if (t == null) {
          t = dt;
        } else if (!t.equals(dt)) {
          throw new InternalCompilerError(
              "Local variable "
                  + "declarations in a for loop initializer must all "
                  + "be the same type, in this case "
                  + t
                  + ", not "
                  + dt
                  + ".",
              d.position());
        }
      }
    }

    if (cond != null && !ts.isImplicitCastValid(cond.type(), ts.Boolean())) {
      throw new SemanticException(
          "The condition of a for statement must have boolean type.", cond.position());
    }

    return this;
  }
Exemple #12
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();
  }
Exemple #13
0
 /** Type check the expression. */
 public Node typeCheck(ContextVisitor tc) {
   return type(result == null ? tc.typeSystem().Void() : result.type());
 }
Exemple #14
0
 public Term entry() {
   return listEntry(inits, (cond != null ? cond.entry() : body.entry()));
 }
  private Node translateExtForArray(ExtendedFor n, List<String> labels) throws SemanticException {
    Position pos = Position.compilerGenerated();
    Type iteratedType = n.decl().type().type();
    // translate "L1,...,Ln: for (C x: e) b" to
    // "{ C[] arr = e; int iter = 0;  L1,...,Ln: while (iter < arr.length)  { C x = arr[iter]; b ;
    // iter = iter + 1; }"
    List<Stmt> stmts = new ArrayList<Stmt>();

    // add the declaration of arr: "C[] arr = e"
    Id arrID = freshName("arr");
    LocalInstance arrLI = ts.localInstance(pos, Flags.NONE, n.expr().type(), arrID.id());
    {
      LocalDecl ld =
          nodeFactory()
              .LocalDecl(
                  pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, arrLI.type()), arrID);
      ld = ld.localInstance(arrLI);
      ld = ld.init(n.expr());
      stmts.add(ld);
    }

    // add the declaration of iterator: "int iter = 0"
    Id iterID = freshName("iter");
    LocalInstance iterLI = ts.localInstance(pos, Flags.NONE, ts.Int(), iterID.id());
    {
      LocalDecl ld =
          nodeFactory()
              .LocalDecl(
                  pos, Flags.NONE, nodeFactory().CanonicalTypeNode(pos, iterLI.type()), iterID);
      ld = ld.localInstance(iterLI);
      ld = ld.init(nodeFactory().IntLit(pos, IntLit.INT, 0).type(ts.Int()));
      stmts.add(ld);
    }

    // build the conditional "iter < arr.length"
    Expr cond;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Local arrLocal =
          (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type());
      Id id = nodeFactory().Id(pos, "length");
      Field field = (Field) nodeFactory().Field(pos, arrLocal, id).type(ts.Int());
      field = field.fieldInstance(ts.findField(arrLI.type().toReference(), "length"));

      cond = nodeFactory().Binary(pos, iterLocal, Binary.LT, field).type(ts.Boolean());
    }

    // build the initlizer for the local decl: arr[iter]
    Expr init;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Local arrLocal =
          (Local) nodeFactory().Local(pos, arrID).localInstance(arrLI).type(arrLI.type());
      init = nodeFactory().ArrayAccess(pos, arrLocal, iterLocal);
      init = init.type(iteratedType);
    }

    // build the increment for iter (iter = iter + 1;)
    Stmt inc;
    {
      Local iterLocal =
          (Local) nodeFactory().Local(pos, iterID).localInstance(iterLI).type(ts.Int());
      Expr incExpr =
          nodeFactory()
              .Binary(
                  pos,
                  iterLocal.type(ts.Int()),
                  Binary.ADD,
                  nodeFactory().IntLit(pos, IntLit.INT, 1).type(ts.Int()))
              .type(ts.Int());
      Assign incStore =
          (Assign) nodeFactory().Assign(pos, iterLocal, Assign.ASSIGN, incExpr).type(ts.Int());
      inc = nodeFactory().Eval(pos, incStore);
    }

    // build the while loop
    {
      // Create a new loop body from the old body followed by the increment
      Block loopBody = nodeFactory().Block(pos, n.decl().init(init), n.body(), inc);
      While loop = nodeFactory().While(pos, cond, loopBody);
      stmts.add(labelStmt(loop, labels));
    }
    return nodeFactory().Block(pos, stmts);
  }
Exemple #16
0
 public Term continueTarget() {
   return listEntry(iters, (cond != null ? cond.entry() : body.entry()));
 }
Exemple #17
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;
  }
Exemple #18
0
 public boolean isConstant() {
   return expr.isConstant() && castType.type().isPrimitive();
 }
Exemple #19
0
 private Node unbox(Expr expr) {
   Position pos = Position.compilerGenerated(null);
   return xnf.X10Cast(
           pos, xnf.CanonicalTypeNode(pos, expr.type()), expr, Converter.ConversionType.UNBOXING)
       .type(expr.type());
 }
Exemple #20
0
 public Term entry() {
   return expr.entry();
 }