@Override
  public void endVisit(CompilationUnit unit) {
    for (SuperMethodBindingPair superMethod : superMethods) {
      String funcName = getSuperFunctionName(superMethod);
      String signature = getSuperFunctionSignature(superMethod.method);

      // Add declarations for the function pointers to call.
      unit.getNativeBlocks()
          .add(
              NativeDeclaration.newOuterDeclaration(
                  null, "static " + UnicodeUtils.format(signature, funcName) + ";"));

      // Look up the implementations in the static initialization.
      AbstractTypeDeclaration typeNode = typeMap.get(superMethod.type.getTypeDeclaration());
      assert typeNode != null : "Type is expected to be in this compilation unit";
      String superclassName = nameTable.getFullName(superMethod.type.getSuperclass());
      typeNode
          .getClassInitStatements()
          .add(
              0,
              new NativeStatement(
                  UnicodeUtils.format(
                      "%s = (%s)[%s instanceMethodForSelector:@selector(%s)];",
                      funcName,
                      UnicodeUtils.format(signature, ""),
                      superclassName,
                      nameTable.getMethodSelector(superMethod.method))));
    }
  }
  @Override
  public void endVisit(SuperMethodInvocation node) {
    Name qualifier = node.getQualifier();
    if (qualifier == null) {
      return;
    }
    IMethodBinding method = node.getMethodBinding();
    ITypeBinding exprType = node.getTypeBinding();
    IVariableBinding var = TreeUtil.getVariableBinding(qualifier);
    assert var != null : "Expected qualifier to be a variable";
    ITypeBinding qualifierType = var.getType();

    SuperMethodBindingPair superMethod = new SuperMethodBindingPair(qualifierType, method);
    superMethods.add(superMethod);

    FunctionBinding binding =
        new FunctionBinding(getSuperFunctionName(superMethod), exprType, qualifierType);
    binding.addParameters(qualifierType, typeEnv.getIdType());
    binding.addParameters(method.getParameterTypes());
    FunctionInvocation invocation = new FunctionInvocation(binding, exprType);
    List<Expression> args = invocation.getArguments();
    args.add(TreeUtil.remove(qualifier));
    String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
    args.add(new NativeExpression(selectorExpr, typeEnv.getIdType()));
    TreeUtil.copyList(node.getArguments(), args);
    node.replaceWith(invocation);
  }
Пример #3
0
 public void testVeryDeeplyNextedExpression() throws IOException {
   StringBuilder sb = new StringBuilder("return i == -1");
   // Create a 2000 node deep infix expression.
   for (int i = 0; i < 2000; i++) {
     sb.append(" || i == " + i);
   }
   sb.append(";");
   String exprStr = sb.toString();
   String translation =
       translateSourceFile(
           UnicodeUtils.format("class Test { boolean test(int i) { %s } }", exprStr),
           "Test",
           "Test.m");
   assertTranslation(translation, exprStr);
 }
 private String getSuperFunctionName(SuperMethodBindingPair superMethod) {
   return UnicodeUtils.format(
       "%s_super$_%s",
       nameTable.getFullName(superMethod.type), nameTable.getFunctionName(superMethod.method));
 }