/**
   * Type check a call to a standard function. Insert CastExprs when needed. If as a result of the
   * insertion of a CastExpr a type check error is thrown, then catch it and re-throw it with a new
   * "this".
   */
  public Type typeCheckStandard(SymbolTable stable) throws TypeCheckError {
    _fname.clearNamespace(); // HACK!!!

    final int n = _arguments.size();
    final Vector argsType = typeCheckArgs(stable);
    final MethodType args = new MethodType(Type.Void, argsType);
    final MethodType ptype = lookupPrimop(stable, _fname.getLocalPart(), args);

    if (ptype != null) {
      for (int i = 0; i < n; i++) {
        final Type argType = (Type) ptype.argsType().elementAt(i);
        final Expression exp = (Expression) _arguments.elementAt(i);
        if (!argType.identicalTo(exp.getType())) {
          try {
            _arguments.setElementAt(new CastExpr(exp, argType), i);
          } catch (TypeCheckError e) {
            throw new TypeCheckError(this); // invalid conversion
          }
        }
      }
      _chosenMethodType = ptype;
      return _type = ptype.resultType();
    }
    throw new TypeCheckError(this);
  }
  public Type typeCheckConstructor(SymbolTable stable) throws TypeCheckError {
    final Vector constructors = findConstructors();
    if (constructors == null) {
      // Constructor not found in this class
      throw new TypeCheckError(ErrorMsg.CONSTRUCTOR_NOT_FOUND, _className);
    }

    final int nConstructors = constructors.size();
    final int nArgs = _arguments.size();
    final Vector argsType = typeCheckArgs(stable);

    // Try all constructors
    int bestConstrDistance = Integer.MAX_VALUE;
    _type = null; // reset
    for (int j, i = 0; i < nConstructors; i++) {
      // Check if all parameters to this constructor can be converted
      final Constructor constructor = (Constructor) constructors.elementAt(i);
      final Class[] paramTypes = constructor.getParameterTypes();

      Class extType = null;
      int currConstrDistance = 0;
      for (j = 0; j < nArgs; j++) {
        // Convert from internal (translet) type to external (Java) type
        extType = paramTypes[j];
        final Type intType = (Type) argsType.elementAt(j);
        Object match = _internal2Java.maps(intType, extType);
        if (match != null) {
          currConstrDistance += ((JavaType) match).distance;
        } else if (intType instanceof ObjectType) {
          ObjectType objectType = (ObjectType) intType;
          if (objectType.getJavaClass() == extType) continue;
          else if (extType.isAssignableFrom(objectType.getJavaClass())) currConstrDistance += 1;
          else {
            currConstrDistance = Integer.MAX_VALUE;
            break;
          }
        } else {
          // no mapping available
          currConstrDistance = Integer.MAX_VALUE;
          break;
        }
      }

      if (j == nArgs && currConstrDistance < bestConstrDistance) {
        _chosenConstructor = constructor;
        _isExtConstructor = true;
        bestConstrDistance = currConstrDistance;

        _type = (_clazz != null) ? Type.newObjectType(_clazz) : Type.newObjectType(_className);
      }
    }

    if (_type != null) {
      return _type;
    }

    throw new TypeCheckError(ErrorMsg.ARGUMENT_CONVERSION_ERR, getMethodSignature(argsType));
  }
  /** Return the signature of the current method */
  private String getMethodSignature(Vector argsType) {
    final StringBuffer buf = new StringBuffer(_className);
    buf.append('.').append(_fname.getLocalPart()).append('(');

    int nArgs = argsType.size();
    for (int i = 0; i < nArgs; i++) {
      final Type intType = (Type) argsType.elementAt(i);
      buf.append(intType.toString());
      if (i < nArgs - 1) buf.append(", ");
    }

    buf.append(')');
    return buf.toString();
  }
Example #4
0
  /**
   * Translate a predicate expression. This translation pushes two references on the stack: a
   * reference to a newly created filter object and a reference to the predicate's closure.
   */
  public void translateFilter(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Compile auxiliary class for filter
    compileFilter(classGen, methodGen);

    // Create new instance of filter
    il.append(new NEW(cpg.addClass(_className)));
    il.append(DUP);
    il.append(new INVOKESPECIAL(cpg.addMethodref(_className, "<init>", "()V")));

    // Initialize closure variables
    final int length = (_closureVars == null) ? 0 : _closureVars.size();

    for (int i = 0; i < length; i++) {
      VariableRefBase varRef = (VariableRefBase) _closureVars.get(i);
      VariableBase var = varRef.getVariable();
      Type varType = var.getType();

      il.append(DUP);

      // Find nearest closure implemented as an inner class
      Closure variableClosure = _parentClosure;
      while (variableClosure != null) {
        if (variableClosure.inInnerClass()) break;
        variableClosure = variableClosure.getParentClosure();
      }

      // Use getfield if in an inner class
      if (variableClosure != null) {
        il.append(ALOAD_0);
        il.append(
            new GETFIELD(
                cpg.addFieldref(
                    variableClosure.getInnerClassName(),
                    var.getEscapedName(),
                    varType.toSignature())));
      } else {
        // Use a load of instruction if in translet class
        il.append(var.loadInstruction());
      }

      // Store variable in new closure
      il.append(
          new PUTFIELD(cpg.addFieldref(_className, var.getEscapedName(), varType.toSignature())));
    }
  }
Example #5
0
 public Type typeCheck(SymbolTable stable) throws TypeCheckError {
   final Type tleft = _left.typeCheck(stable);
   final Type tright = _right.typeCheck(stable);
   final MethodType ptype =
       lookupPrimop(stable, Ops[_op], new MethodType(Type.Void, tleft, tright));
   if (ptype != null) {
     final Type arg1 = (Type) ptype.argsType().elementAt(0);
     if (!arg1.identicalTo(tleft)) {
       _left = new CastExpr(_left, arg1);
     }
     final Type arg2 = (Type) ptype.argsType().elementAt(1);
     if (!arg2.identicalTo(tright)) {
       _right = new CastExpr(_right, arg1);
     }
     return _type = ptype.resultType();
   }
   throw new TypeCheckError(this);
 }
  public static String compileSortRecordFactory(
      Vector sortObjects,
      ClassGenerator classGen,
      MethodGenerator methodGen,
      String sortRecordClass) {
    final XSLTC xsltc = ((Sort) sortObjects.firstElement()).getXSLTC();
    final String className = xsltc.getHelperClassName();

    final NodeSortRecordFactGenerator sortRecordFactory =
        new NodeSortRecordFactGenerator(
            className,
            NODE_SORT_FACTORY,
            className + ".java",
            ACC_PUBLIC | ACC_SUPER | ACC_FINAL,
            new String[] {},
            classGen.getStylesheet());

    ConstantPoolGen cpg = sortRecordFactory.getConstantPool();

    // Add a new instance variable for each var in closure
    final int nsorts = sortObjects.size();
    final ArrayList dups = new ArrayList();

    for (int j = 0; j < nsorts; j++) {
      final Sort sort = (Sort) sortObjects.get(j);
      final int length = (sort._closureVars == null) ? 0 : sort._closureVars.size();

      for (int i = 0; i < length; i++) {
        final VariableRefBase varRef = (VariableRefBase) sort._closureVars.get(i);

        // Discard duplicate variable references
        if (dups.contains(varRef)) continue;

        final VariableBase var = varRef.getVariable();
        sortRecordFactory.addField(
            new Field(
                ACC_PUBLIC,
                cpg.addUtf8(var.getEscapedName()),
                cpg.addUtf8(var.getType().toSignature()),
                null,
                cpg.getConstantPool()));
        dups.add(varRef);
      }
    }

    // Define a constructor for this class
    final org.apache.bcel.generic.Type[] argTypes = new org.apache.bcel.generic.Type[7];
    argTypes[0] = Util.getJCRefType(DOM_INTF_SIG);
    argTypes[1] = Util.getJCRefType(STRING_SIG);
    argTypes[2] = Util.getJCRefType(TRANSLET_INTF_SIG);
    argTypes[3] = Util.getJCRefType("[" + STRING_SIG);
    argTypes[4] = Util.getJCRefType("[" + STRING_SIG);
    argTypes[5] = Util.getJCRefType("[" + STRING_SIG);
    argTypes[6] = Util.getJCRefType("[" + STRING_SIG);

    final String[] argNames = new String[7];
    argNames[0] = DOCUMENT_PNAME;
    argNames[1] = "className";
    argNames[2] = TRANSLET_PNAME;
    argNames[3] = "order";
    argNames[4] = "type";
    argNames[5] = "lang";
    argNames[6] = "case_order";

    InstructionList il = new InstructionList();
    final MethodGenerator constructor =
        new MethodGenerator(
            ACC_PUBLIC,
            org.apache.bcel.generic.Type.VOID,
            argTypes,
            argNames,
            "<init>",
            className,
            il,
            cpg);

    // Push all parameters onto the stack and called super.<init>()
    il.append(ALOAD_0);
    il.append(ALOAD_1);
    il.append(ALOAD_2);
    il.append(new ALOAD(3));
    il.append(new ALOAD(4));
    il.append(new ALOAD(5));
    il.append(new ALOAD(6));
    il.append(new ALOAD(7));
    il.append(
        new INVOKESPECIAL(
            cpg.addMethodref(
                NODE_SORT_FACTORY,
                "<init>",
                "("
                    + DOM_INTF_SIG
                    + STRING_SIG
                    + TRANSLET_INTF_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + "["
                    + STRING_SIG
                    + ")V")));
    il.append(RETURN);

    // Override the definition of makeNodeSortRecord()
    il = new InstructionList();
    final MethodGenerator makeNodeSortRecord =
        new MethodGenerator(
            ACC_PUBLIC,
            Util.getJCRefType(NODE_SORT_RECORD_SIG),
            new org.apache.bcel.generic.Type[] {
              org.apache.bcel.generic.Type.INT, org.apache.bcel.generic.Type.INT
            },
            new String[] {"node", "last"},
            "makeNodeSortRecord",
            className,
            il,
            cpg);

    il.append(ALOAD_0);
    il.append(ILOAD_1);
    il.append(ILOAD_2);
    il.append(
        new INVOKESPECIAL(
            cpg.addMethodref(
                NODE_SORT_FACTORY, "makeNodeSortRecord", "(II)" + NODE_SORT_RECORD_SIG)));
    il.append(DUP);
    il.append(new CHECKCAST(cpg.addClass(sortRecordClass)));

    // Initialize closure in record class
    final int ndups = dups.size();
    for (int i = 0; i < ndups; i++) {
      final VariableRefBase varRef = (VariableRefBase) dups.get(i);
      final VariableBase var = varRef.getVariable();
      final Type varType = var.getType();

      il.append(DUP);

      // Get field from factory class
      il.append(ALOAD_0);
      il.append(
          new GETFIELD(cpg.addFieldref(className, var.getEscapedName(), varType.toSignature())));

      // Put field in record class
      il.append(
          new PUTFIELD(
              cpg.addFieldref(sortRecordClass, var.getEscapedName(), varType.toSignature())));
    }
    il.append(POP);
    il.append(ARETURN);

    constructor.setMaxLocals();
    constructor.setMaxStack();
    sortRecordFactory.addMethod(constructor);
    makeNodeSortRecord.setMaxLocals();
    makeNodeSortRecord.setMaxStack();
    sortRecordFactory.addMethod(makeNodeSortRecord);
    xsltc.dumpClass(sortRecordFactory.getJavaClass());

    return className;
  }
  /**
   * Translate a function call. The compiled code will leave the function's return value on the
   * JVM's stack.
   */
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final int n = argumentCount();
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final boolean isSecureProcessing = classGen.getParser().getXSLTC().isSecureProcessing();
    int index;

    // Translate calls to methods in the BasisLibrary
    if (isStandard() || isExtension()) {
      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        exp.startIterator(classGen, methodGen);
      }

      // append "F" to the function's name
      final String name = _fname.toString().replace('-', '_') + "F";
      String args = Constants.EMPTYSTRING;

      // Special precautions for some method calls
      if (name.equals("sumF")) {
        args = DOM_INTF_SIG;
        il.append(methodGen.loadDOM());
      } else if (name.equals("normalize_spaceF")) {
        if (_chosenMethodType.toSignature(args).equals("()Ljava/lang/String;")) {
          args = "I" + DOM_INTF_SIG;
          il.append(methodGen.loadContextNode());
          il.append(methodGen.loadDOM());
        }
      }

      // Invoke the method in the basis library
      index = cpg.addMethodref(BASIS_LIBRARY_CLASS, name, _chosenMethodType.toSignature(args));
      il.append(new INVOKESTATIC(index));
    }
    // Add call to BasisLibrary.unresolved_externalF() to generate
    // run-time error message for unsupported external functions
    else if (unresolvedExternal) {
      index =
          cpg.addMethodref(BASIS_LIBRARY_CLASS, "unresolved_externalF", "(Ljava/lang/String;)V");
      il.append(new PUSH(cpg, _fname.toString()));
      il.append(new INVOKESTATIC(index));
    } else if (_isExtConstructor) {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenConstructor.getDeclaringClass().getName();
      Class[] paramTypes = _chosenConstructor.getParameterTypes();
      LocalVariableGen[] paramTemp = new LocalVariableGen[n];

      // Backwards branches are prohibited if an uninitialized object is
      // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
      // We don't know whether this code might contain backwards branches
      // so we mustn't create the new object until after we've created
      // the suspect arguments to its constructor.  Instead we calculate
      // the values of the arguments to the constructor first, store them
      // in temporary variables, create the object and reload the
      // arguments from the temporaries to avoid the problem.

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        Type expType = exp.getType();
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        expType.translateTo(classGen, methodGen, paramTypes[i]);
        paramTemp[i] =
            methodGen.addLocalVariable(
                "function_call_tmp" + i, expType.toJCType(), il.getEnd(), null);
        il.append(expType.STORE(paramTemp[i].getIndex()));
      }

      il.append(new NEW(cpg.addClass(_className)));
      il.append(InstructionConstants.DUP);

      for (int i = 0; i < n; i++) {
        final Expression arg = argument(i);
        il.append(arg.getType().LOAD(paramTemp[i].getIndex()));
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append("V");

      index = cpg.addMethodref(clazz, "<init>", buffer.toString());
      il.append(new INVOKESPECIAL(index));

      // Convert the return type back to our internal type
      (Type.Object).translateFrom(classGen, methodGen, _chosenConstructor.getDeclaringClass());

    }
    // Invoke function calls that are handled in separate classes
    else {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenMethod.getDeclaringClass().getName();
      Class[] paramTypes = _chosenMethod.getParameterTypes();

      // Push "this" if it is an instance method
      if (_thisArgument != null) {
        _thisArgument.translate(classGen, methodGen);
      }

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append(getSignature(_chosenMethod.getReturnType()));

      if (_thisArgument != null && _clazz.isInterface()) {
        index = cpg.addInterfaceMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(new INVOKEINTERFACE(index, n + 1));
      } else {
        index = cpg.addMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(
            _thisArgument != null
                ? (InvokeInstruction) new INVOKEVIRTUAL(index)
                : (InvokeInstruction) new INVOKESTATIC(index));
      }

      // Convert the return type back to our internal type
      _type.translateFrom(classGen, methodGen, _chosenMethod.getReturnType());
    }
  }
  /**
   * Type check a call to an external (Java) method. The method must be static an public, and a
   * legal type conversion must exist for all its arguments and its return type. Every method of
   * name <code>_fname</code> is inspected as a possible candidate.
   */
  public Type typeCheckExternal(SymbolTable stable) throws TypeCheckError {
    int nArgs = _arguments.size();
    final String name = _fname.getLocalPart();

    // check if function is a contructor 'new'
    if (_fname.getLocalPart().equals("new")) {
      return typeCheckConstructor(stable);
    }
    // check if we are calling an instance method
    else {
      boolean hasThisArgument = false;

      if (nArgs == 0) _isStatic = true;

      if (!_isStatic) {
        if (_namespace_format == NAMESPACE_FORMAT_JAVA
            || _namespace_format == NAMESPACE_FORMAT_PACKAGE) hasThisArgument = true;

        Expression firstArg = (Expression) _arguments.elementAt(0);
        Type firstArgType = (Type) firstArg.typeCheck(stable);

        if (_namespace_format == NAMESPACE_FORMAT_CLASS
            && firstArgType instanceof ObjectType
            && _clazz != null
            && _clazz.isAssignableFrom(((ObjectType) firstArgType).getJavaClass()))
          hasThisArgument = true;

        if (hasThisArgument) {
          _thisArgument = (Expression) _arguments.elementAt(0);
          _arguments.remove(0);
          nArgs--;
          if (firstArgType instanceof ObjectType) {
            _className = ((ObjectType) firstArgType).getJavaClassName();
          } else throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, name);
        }
      } else if (_className.length() == 0) {
        /*
         * Warn user if external function could not be resolved.
         * Warning will _NOT_ be issued is the call is properly
         * wrapped in an <xsl:if> or <xsl:when> element. For details
         * see If.parserContents() and When.parserContents()
         */
        final Parser parser = getParser();
        if (parser != null) {
          reportWarning(this, parser, ErrorMsg.FUNCTION_RESOLVE_ERR, _fname.toString());
        }
        unresolvedExternal = true;
        return _type = Type.Int; // use "Int" as "unknown"
      }
    }

    final Vector methods = findMethods();

    if (methods == null) {
      // Method not found in this class
      throw new TypeCheckError(ErrorMsg.METHOD_NOT_FOUND_ERR, _className + "." + name);
    }

    Class extType = null;
    final int nMethods = methods.size();
    final Vector argsType = typeCheckArgs(stable);

    // Try all methods to identify the best fit
    int bestMethodDistance = Integer.MAX_VALUE;
    _type = null; // reset internal type
    for (int j, i = 0; i < nMethods; i++) {
      // Check if all paramteters to this method can be converted
      final Method method = (Method) methods.elementAt(i);
      final Class[] paramTypes = method.getParameterTypes();

      int currMethodDistance = 0;
      for (j = 0; j < nArgs; j++) {
        // Convert from internal (translet) type to external (Java) type
        extType = paramTypes[j];
        final Type intType = (Type) argsType.elementAt(j);
        Object match = _internal2Java.maps(intType, extType);
        if (match != null) {
          currMethodDistance += ((JavaType) match).distance;
        } else {
          // no mapping available
          //
          // Allow a Reference type to match any external (Java) type at
          // the moment. The real type checking is performed at runtime.
          if (intType instanceof ReferenceType) {
            currMethodDistance += 1;
          } else if (intType instanceof ObjectType) {
            ObjectType object = (ObjectType) intType;
            if (extType.getName().equals(object.getJavaClassName())) currMethodDistance += 0;
            else if (extType.isAssignableFrom(object.getJavaClass())) currMethodDistance += 1;
            else {
              currMethodDistance = Integer.MAX_VALUE;
              break;
            }
          } else {
            currMethodDistance = Integer.MAX_VALUE;
            break;
          }
        }
      }

      if (j == nArgs) {
        // Check if the return type can be converted
        extType = method.getReturnType();

        _type = (Type) _java2Internal.get(extType);
        if (_type == null) {
          _type = Type.newObjectType(extType);
        }

        // Use this method if all parameters & return type match
        if (_type != null && currMethodDistance < bestMethodDistance) {
          _chosenMethod = method;
          bestMethodDistance = currMethodDistance;
        }
      }
    }

    // It is an error if the chosen method is an instance menthod but we don't
    // have a this argument.
    if (_chosenMethod != null
        && _thisArgument == null
        && !Modifier.isStatic(_chosenMethod.getModifiers())) {
      throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, getMethodSignature(argsType));
    }

    if (_type != null) {
      if (_type == Type.NodeSet) {
        getXSLTC().setMultiDocument(true);
      }
      return _type;
    }

    throw new TypeCheckError(ErrorMsg.ARGUMENT_CONVERSION_ERR, getMethodSignature(argsType));
  }