Example #1
0
  public Type childExpectedType(Expr child, AscriptionVisitor av) {
    TypeSystem ts = av.typeSystem();

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

    return child.type();
  }
Example #2
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();
  }
Example #3
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());
  }
Example #4
0
  protected Object resolveObject(Object o) {
    if (!enableReplace) {
      return o;
    }
    String s = "";
    if (reporter.should_report(reporter.serialize, 2)) {
      try {
        s = o.toString();
      } catch (NullPointerException e) {
        s = "<NullPointerException thrown>";
      }
    }

    if (!enableReplace) {
      return o;
    } else if (o instanceof Internable) {
      if (reporter.should_report(Reporter.serialize, 2)) {
        reporter.report(2, "- Interning " + s + " : " + o.getClass());
      }
      return ((Internable) o).intern();
    } else if (o instanceof Goal) {
      return ((Goal) o).intern(ts.extensionInfo().scheduler());
    } else {
      if (reporter.should_report(Reporter.serialize, 2)) {
        reporter.report(2, "- " + s + " : " + o.getClass());
      }

      return o;
    }
  }
Example #5
0
  public List throwTypes(TypeSystem ts) {
    if (expr.type().isReference()) {
      return Collections.singletonList(ts.ClassCastException());
    }

    return Collections.EMPTY_LIST;
  }
  @Override
  public Node typeCheck(ContextVisitor tc) throws SemanticException {
    RuleDef sym = null;

    MethodInstance mi = call.methodInstance();
    IbexClassType ct = (IbexClassType) mi.container();
    for (RuleInstance rule : ct.rules()) {
      if (rule.name() == mi.name()) sym = rule.def();
    }

    if (sym == null) throw new SemanticException("Cannot find rule for " + mi);

    RhsInvoke n = (RhsInvoke) symbol(sym.asNonterminal()).type(call.type());

    if (assocTag) {
      Context c = tc.context();
      CodeDef code = c.currentCode();
      if (code instanceof RuleDef) {
        RuleDef rd = (RuleDef) code;
        if (rd != sym)
          throw new SemanticException(
              "Associativity annotation must be self-recursive.", position());
      }
    }

    if (!call.type().isVoid()) {
      TypeSystem ts = tc.typeSystem();
      LocalDef li = ts.localDef(position(), Flags.FINAL, Types.ref(call.type()), call.name().id());
      // Formal parameters are never compile-time constants.
      li.setNotConstant();

      IbexNodeFactory nf = (IbexNodeFactory) tc.nodeFactory();
      LocalDecl ld =
          nf.LocalDecl(
              position(),
              nf.FlagsNode(position(), li.flags()),
              nf.CanonicalTypeNode(position(), li.type()),
              nf.Id(position(), li.name()));
      ld = ld.localDef(li);
      ld = ld.init(n);

      return nf.RhsSyntheticBind(position(), ld).type(n.type());
    }

    return n;
  }
Example #7
0
 @Override
 public boolean run() {
   String name = goal.typeName();
   try {
     // Try to resolve the type; this may throw a
     // MissingDependencyException on the job to load the file
     // containing the type.
     Named n = ts.systemResolver().find(name);
     if (n instanceof Type) {
       return true;
     }
   } catch (SemanticException e) {
     ErrorQueue eq = ts.extensionInfo().compiler().errorQueue();
     eq.enqueue(ErrorInfo.SEMANTIC_ERROR, e.getMessage(), e.position());
   }
   return false;
 }
Example #8
0
  /** 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;
  }
Example #9
0
  public TypeInputStream(InputStream in, TypeSystem ts, Map<Object, Object> cache)
      throws IOException {
    super(in);

    enableResolveObject(true);

    this.ts = ts;
    this.reporter = ts.extensionInfo().getOptions().reporter;
    this.cache = cache;
    this.failed = false;
    this.enableReplace = true;
    this.placeHoldersUsed = CollectionFactory.newHashSet();
  }
Example #10
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;
  }
Example #11
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;
  }