Example #1
0
  /**
   * Check the types of this cast. Check if the expression can be casted to the given type.
   *
   * @see de.unika.ipd.grgen.ast.BaseNode#typeCheckLocal()
   */
  private boolean typeCheckLocal() {
    TypeNode fromType = expr.getType();
    if (fromType instanceof NodeTypeNode && type instanceof NodeTypeNode) {
      // we support up- and down-casts, but no cross-casts of nodes
      HashSet<TypeNode> supertypesOfFrom = new HashSet<TypeNode>();
      ((NodeTypeNode) fromType).doGetCompatibleToTypes(supertypesOfFrom);
      HashSet<TypeNode> supertypesOfTo = new HashSet<TypeNode>();
      ((NodeTypeNode) type).doGetCompatibleToTypes(supertypesOfTo);
      return fromType.equals(type)
          || supertypesOfFrom.contains(type)
          || supertypesOfTo.contains(fromType);
    }
    if (fromType instanceof EdgeTypeNode && type instanceof EdgeTypeNode) {
      // we support up- and down-casts, but no cross-casts of edges
      HashSet<TypeNode> supertypesOfFrom = new HashSet<TypeNode>();
      ((EdgeTypeNode) fromType).doGetCompatibleToTypes(supertypesOfFrom);
      HashSet<TypeNode> supertypesOfTo = new HashSet<TypeNode>();
      ((EdgeTypeNode) type).doGetCompatibleToTypes(supertypesOfTo);
      return fromType.equals(type)
          || supertypesOfFrom.contains(type)
          || supertypesOfTo.contains(fromType);
    }
    if (fromType instanceof ObjectTypeNode
        && !(type instanceof NodeTypeNode)
        && !(type instanceof EdgeTypeNode))
      return true; // object is castable to anything besides nodes and edges
    if (type instanceof ObjectTypeNode
        && !(fromType instanceof NodeTypeNode)
        && !(fromType instanceof EdgeTypeNode))
      return true; // anything besides nodes and edges can be casted into an object
    if (fromType instanceof ExternalTypeNode && type instanceof ExternalTypeNode) {
      // we support up- and down-casts, but no cross-casts of external types
      HashSet<TypeNode> supertypesOfFrom = new HashSet<TypeNode>();
      ((ExternalTypeNode) fromType).doGetCompatibleToTypes(supertypesOfFrom);
      HashSet<TypeNode> supertypesOfTo = new HashSet<TypeNode>();
      ((ExternalTypeNode) type).doGetCompatibleToTypes(supertypesOfTo);
      return fromType.equals(type)
          || supertypesOfFrom.contains(type)
          || supertypesOfTo.contains(fromType);
    }

    boolean result = fromType.isCastableTo(type);
    if (!result) {
      reportError("Illegal cast from \"" + expr.getType() + "\" to \"" + type + "\"");
    }

    return result;
  }