Package com.google.gwt.dev.js.ast

Examples of com.google.gwt.dev.js.ast.JsFunction


      }
    }

    private void generateNullFunc(List<JsStatement> globalStatements) {
      // handle null method
      JsFunction nullFunc = new JsFunction(topScope, nullMethodName, true);
      nullFunc.setBody(new JsBlock());
      globalStatements.add(nullFunc.makeStmt());
    }
View Full Code Here


      if (x != program.getTypeJavaLangString()) {
        JsName seedFuncName = names.get(x);

        // seed function
        // function com_example_foo_Foo() { }
        JsFunction seedFunc = new JsFunction(topScope, seedFuncName, true);
        JsBlock body = new JsBlock();
        seedFunc.setBody(body);
        globalStmts.add(seedFunc.makeStmt());

        // setup prototype, assign to temp
        // _ = com_example_foo_Foo.prototype = new com_example_foo_FooSuper();
        JsNameRef lhs = prototype.makeRef();
        lhs.setQualifier(seedFuncName.makeRef());
View Full Code Here

        JsNameRef toStringRef = new JsNameRef(
            polymorphicNames.get(toStringMeth));
        toStringRef.setQualifier(new JsThisRef());
        call.setQualifier(toStringRef);
        JsReturn jsReturn = new JsReturn(call);
        JsFunction rhs = new JsFunction(topScope);
        JsBlock body = new JsBlock();
        body.getStatements().add(jsReturn);
        rhs.setBody(body);

        // asg
        JsExpression asg = createAssignment(lhs, rhs);
        globalStmts.add(new JsExprStmt(asg));
      }
View Full Code Here

      try {
        // start at -1 to avoid counting our synthetic header
        // TODO: get the character position start correct
        List<JsStatement> result = jsParser.parse(jsProgram.getScope(), sr, -1);
        JsExprStmt jsExprStmt = (JsExprStmt) result.get(0);
        JsFunction jsFunction = (JsFunction) jsExprStmt.getExpression();
        jsFunction.setFromJava(true);
        ((JsniMethodBody) newMethod.getBody()).setFunc(jsFunction);

        // Ensure that we've resolved the parameter and local references within
        // the JSNI method for later pruning.
        JsParameterResolver localResolver = new JsParameterResolver(jsFunction);
View Full Code Here

  /**
   * Given a JsInvocation, determine if it is invoking a JsFunction that is
   * specified to be executed only once during the program's lifetime.
   */
  private static JsFunction isExecuteOnce(JsInvocation invocation) {
    JsFunction f = isFunction(invocation.getQualifier());
    if (f != null && f.getExecuteOnce()) {
      return f;
    }
    return null;
  }
View Full Code Here

            && staticImplFor.getEnclosingType().methods.contains(staticImplFor)) {
          // instance method is still live
          return true;
        }

        JsFunction func = x.isNative()
            ? ((JsniMethodBody) x.getBody()).getFunc() : null;

        ArrayList<JParameter> originalParams = new ArrayList<JParameter>(
            x.params);

        for (int i = 0; i < x.params.size(); ++i) {
          JParameter param = x.params.get(i);
          if (!referencedNonTypes.contains(param)) {
            x.params.remove(i);
            didChange = true;
            // Remove the associated JSNI parameter
            if (func != null) {
              func.getParameters().remove(i);
            }
            --i;
            methodToOriginalParamsMap.put(x, originalParams);
          }
        }
View Full Code Here

    /**
     * Possibly record that we've seen a call in the current context.
     */
    @Override
    public boolean visit(JsInvocation x, JsContext<JsExpression> ctx) {
      JsFunction func = isExecuteOnce(x);
      while (func != null) {
        called.add(func);
        func = func.getImpliedExecute();
      }
      return true;
    }
View Full Code Here

    private boolean isDuplicateCall(JsExpression x) {
      if (!(x instanceof JsInvocation)) {
        return false;
      }

      JsFunction func = isExecuteOnce((JsInvocation) x);
      return (func != null && called.contains(func));
    }
View Full Code Here

       * to be functions. Otherwise, we can't know what statements the
       * invocation would actually invoke. The static reference would be null
       * when trying operate on references to external functions, or functions
       * as arguments to another function.
       */
      JsFunction f = isFunction(x.getQualifier());
      if (f == null) {
        return;
      }

      // Don't inline blacklisted functions
      if (blacklist.contains(f)) {
        return;
      }

      List<JsName> localVariableNames = new ArrayList<JsName>();
      List<JsStatement> statements = new ArrayList<JsStatement>(
          f.getBody().getStatements());
      List<JsExpression> hoisted = new ArrayList<JsExpression>(
          statements.size());

      boolean sawReturnStatement = false;
      for (JsStatement statement : statements) {
        if (sawReturnStatement) {
          /*
           * We've already seen a return statement, but there are still more
           * statements. The target is unsafe to inline, so bail. Note: in most
           * cases JsStaticEval will have removed any statements following a
           * return statement.
           *
           * The reason we have to bail is that the return statement's
           * expression MUST be the last thing evaluated.
           *
           * TODO(bobv): maybe it could still be inlined with smart
           * transformation?
           */
          return;
        }

        /*
         * Create replacement expressions to use in place of the original
         * statements. It is important that the replacement is newly-minted and
         * therefore not referenced by any other AST nodes. Consider the case of
         * a common, delegating function. If the hoisted expressions were not
         * distinct objects, it would not be possible to substitute different
         * JsNameRefs at different call sites.
         */
        JsExpression h = hoistedExpression(program, statement,
            localVariableNames);
        if (h == null) {
          return;
        }

        hoisted.add(h);

        if (isReturnStatement(statement)) {
          sawReturnStatement = true;
        }
      }

      /*
       * If the inlined method has no return statement, synthesize an undefined
       * reference. It will be reclaimed if the method call is from a
       * JsExprStmt.
       */
      if (!sawReturnStatement) {
        hoisted.add(program.getUndefinedLiteral());
      }

      assert (hoisted.size() > 0);

      /*
       * Build up the new comma expression from right-to-left; building the
       * rightmost comma expressions first. Bootstrapping with i.previous()
       * ensures that this logic will function correctly in the case of a single
       * expression.
       */
      ListIterator<JsExpression> i = hoisted.listIterator(hoisted.size());
      JsExpression op = i.previous();
      while (i.hasPrevious()) {
        JsBinaryOperation outerOp = new JsBinaryOperation(
            JsBinaryOperator.COMMA);
        outerOp.setArg1(i.previous());
        outerOp.setArg2(op);
        op = outerOp;
      }

      // Confirm that the expression conforms to the desired heuristics
      if (!isInlinable(program, functionStack.peek(), x, f, op)) {
        return;
      }

      // Perform the name replacement
      NameRefReplacerVisitor v = new NameRefReplacerVisitor(x, f);
      for (ListIterator<JsName> nameIterator = localVariableNames.listIterator(); nameIterator.hasNext();) {
        JsName name = nameIterator.next();

        /*
         * Find an unused identifier in the caller's scope. It's possible that
         * the same function has been inlined in multiple places within the
         * function so we'll use a counter for disambiguation.
         */
        String ident;
        int count = 0;
        JsScope scope = functionStack.peek().getScope();
        do {
          ident = f.getName() + "_" + name.getIdent() + "_" + count++;
        } while (scope.findExistingName(ident) != null);

        JsName newName = scope.declareName(ident, name.getShortIdent());
        v.setReplacementName(name, newName);
        nameIterator.set(newName);
View Full Code Here

    @Override
    public boolean visit(final JMethod x, Context ctx) {
      if (x.isNative()) {
        // Manually rescue native parameter references
        final JsniMethodBody body = (JsniMethodBody) x.getBody();
        final JsFunction func = body.getFunc();

        new JsVisitor() {
          @Override
          public void endVisit(JsNameRef nameRef, JsContext<JsExpression> ctx) {
            JsName ident = nameRef.getName();

            if (ident != null) {
              // If we're referencing a parameter, rescue the associated
              // JParameter
              int index = func.getParameters().indexOf(ident.getStaticRef());
              if (index != -1) {
                rescue(x.params.get(index));
              }
            }
          }
View Full Code Here

TOP

Related Classes of com.google.gwt.dev.js.ast.JsFunction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.