org.mozilla.javascript.ast.FunctionCall Java Examples

The following examples show how to use org.mozilla.javascript.ast.FunctionCall. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 6 votes vote down vote up
/**
 * call a function through a closure
 */
private void processClosureCall(FunctionCall fc) {

	AstNode target = fc.getTarget();
	ITypeTerm funVar = processExpression(target);

	// for call foo(E_1,...,E_n), equate the type of the call to ret(foo)
	FunctionCallTerm callTerm = findOrCreateFunctionCallTerm(fc);
	callTerm.setTarget(funVar);
	ITypeTerm retTerm = findOrCreateFunctionReturnTerm(funVar, fc.getArguments().size(), fc.getLineno(), fc);
	addTypeEqualityConstraint(callTerm, retTerm, fc.getLineno(), null);

	// for call foo(E_1,...,E_n), generate constraints |E_i| <: Param(foo,i)
	for (int i=0; i < fc.getArguments().size(); i++){
		AstNode arg = fc.getArguments().get(i);
		ITypeTerm argExp = processExpression(arg);
		ITypeTerm paramExp = findOrCreateFunctionParamTerm(funVar, i, fc.getArguments().size(), fc.getLineno());
		processCopy(arg, argExp, paramExp,
				fc.getLineno(), (solution) ->
						subtypeError("bad argument " + shortSrc(arg) + " passed to function",
								solution.typeOfTerm(argExp), solution.typeOfTerm(paramExp), locationOf(arg)));
	}
}
 
Example #2
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private Node transformFunctionCall(FunctionCall node) {
    Node call = createCallOrNew(Token.CALL, transform(node.getTarget()));
    call.setLineno(node.getLineno());
    decompiler.addToken(Token.LP);
    List<AstNode> args = node.getArguments();
    for (int i = 0; i < args.size(); i++) {
        AstNode arg = args.get(i);
        call.addChildToBack(transform(arg));
        if (i < args.size() - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RP);
    return call;
}
 
Example #3
Source File: ConstraintGenUtil.java    From SJS with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the declaration of the called function. Assumes that the call's target
 * is a Name, which is passed as the second parameter.
 *
 */
static FunctionNode findFunDecl(FunctionCall fc, Name funName){
	List<FunctionNode> funsFound = findFunDecl2(fc, new ArrayList<FunctionNode>());
	for (int i=0; i < funsFound.size(); i++){
		FunctionNode fun = funsFound.get(i);
		if (funName.getIdentifier().equals(fun.getName())){
			return fun;
		}
	}
	return null;
}
 
Example #4
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 5 votes vote down vote up
/**
   * Syntactically identify module imports
   */
  private boolean isSyntacticModuleRequire(FunctionCall fc) {
AstNode target = fc.getTarget();
      if (target instanceof Name) {
          Name name = (Name)target;
          return name.getIdentifier().equals("require") && fc.getArguments().size() == 1 && fc.getArguments().get(0) instanceof StringLiteral;
      } else {
          return false;
      }
  }
 
Example #5
Source File: FunctionCallTerm.java    From SJS with Apache License 2.0 4 votes vote down vote up
public FunctionCallTerm(FunctionCall n){
	super(n);
	this.type = new AnyType();
}
 
Example #6
Source File: FunctionCallTerm.java    From SJS with Apache License 2.0 4 votes vote down vote up
public FunctionCall getFunctionCall(){
	return (FunctionCall)getNode();
}
 
Example #7
Source File: ConstraintFactory.java    From SJS with Apache License 2.0 4 votes vote down vote up
public FunctionCallTerm findOrCreateFunctionCallTerm(FunctionCall n) {
    if (!functionCallTerms.containsKey(n)){
        functionCallTerms.put(n, new FunctionCallTerm(n));
    }
    return functionCallTerms.get(n);
}
 
Example #8
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 4 votes vote down vote up
/**
 * This method generates constraints for all relevant AstNodes. It delegates its work to various
 * processXXX() methods that handle AstNodes of type XXX.
 */
@Override
public boolean visit(AstNode node) {
	if (node instanceof VariableInitializer){
		processVariableInitializer(node);
	} else if (node instanceof ReturnStatement){
		processReturnStatement((ReturnStatement)node);
	} else if (node instanceof ExpressionStatement){
		processExpressionStatement((ExpressionStatement)node);
	} else if (node instanceof ForLoop){
		processForLoop((ForLoop)node);
	} else if (node instanceof ForInLoop){
		processForInLoop((ForInLoop)node);
	}else if (node instanceof WhileLoop){
		processWhileLoop((WhileLoop)node);
	} else if (node instanceof DoLoop){
		processDoLoop((DoLoop)node);
	} else if (node instanceof NewExpression){
		processNewExpression((NewExpression)node);
	} else if (node instanceof FunctionCall){
		processFunctionCall((FunctionCall)node);
	} else if (node instanceof ElementGet){
		processElementGet((ElementGet)node);
	} else if (node instanceof FunctionNode){
		processFunctionNode((FunctionNode)node);
	} else if (node instanceof IfStatement){
		processIfStatement((IfStatement)node);
	} else if (node instanceof KeywordLiteral){
		processKeywordLiteral((KeywordLiteral)node);
	} else if (node instanceof SwitchStatement){
		processSwitchStatement((SwitchStatement)node);
	} else if (node instanceof SwitchCase){
		processSwitchCase((SwitchCase)node);
	} else if ((node instanceof AstRoot) || //AstRoot: no constraints need to be generated
		(node instanceof BreakStatement) || //BreakStatement: no constraints need to be generated
		(node instanceof VariableDeclaration) || //VariableDeclaration: we generate constraints for its constituent VariableInitializer nodes
		(node instanceof Name) || //Name: generate constraints for complex expressions that refer to names
		(node instanceof NumberLiteral) || //NumberLiteral: generate constraints for complex expressions that refer to names
		(node instanceof StringLiteral) || //StringLiteral: generate constraints for complex expressions that refer to names
		(node instanceof Assignment) || // Assignment is a special case of InfixExpression
		(node instanceof ArrayLiteral) ||
		(node instanceof UnaryExpression) ||
		(node instanceof InfixExpression) ||
		(node instanceof ConditionalExpression) ||
		(node instanceof ParenthesizedExpression) ||
		(node instanceof EmptyExpression) ||
		(node instanceof ObjectLiteral) ||
		(node instanceof EmptyStatement) ||
		(node instanceof ContinueStatement) ||
		(node instanceof Scope) ||
		(node instanceof Block)){ // // occurs in programs with for loops -- nothing to be done here?
		/* nothing */
	} else {
		error("unsupported node " + node.toSource().trim() + " of type: " + node.getClass().getName(), node);
	}
	return true;
}
 
Example #9
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 4 votes vote down vote up
/**
 * Creates constraints for the subtree rooted at a designated expression node,
 * and returns a constraint variable corresponding to the root of the tree.
 */
private ITypeTerm processExpression(AstNode n){

	ITypeTerm cached = theMap.get(n);
	if (cached != null) return cached;

	if (n instanceof Name){
		return processVariableReference((Name)n);
	} else if (n instanceof NumberLiteral){
		return processNumericConstant((NumberLiteral)n);
	} else if (n instanceof StringLiteral){
		return processStringLiteral((StringLiteral)n);
	} else if (ConstraintGenUtil.isBooleanConstant(n)){
		return processBooleanConstant(n);
	} else if (n instanceof UnaryExpression){
		return processUnaryExpression((UnaryExpression)n);
	} else if (n instanceof InfixExpression){
		return processInfixExpression((InfixExpression)n);
	} else if (n instanceof FunctionCall){
		return processFunctionCallExpression((FunctionCall)n);
	} else if (n instanceof ArrayLiteral){
		return processArrayLiteral((ArrayLiteral)n);
	} else if (n instanceof ElementGet){
		return processElementGet((ElementGet)n);
	} else if (n instanceof ParenthesizedExpression) {
		return processParenthesizedExpression((ParenthesizedExpression)n);
	} else if (n instanceof ConditionalExpression) {
		return processConditionalExpression((ConditionalExpression)n);
	} else if (n instanceof ObjectLiteral) {
		return processObjectLiteral((ObjectLiteral)n);
	} else if (n instanceof KeywordLiteral){
		return processKeywordLiteral((KeywordLiteral)n);
	} else if (n instanceof FunctionNode){
		return processFunctionNode((FunctionNode)n);
	} else if (n instanceof EmptyExpression){
		return processEmptyExpression((EmptyExpression)n);
	} else {
		System.err.println(n.toSource());
		return expError("unimplemented case in findOrCreateExpressionVariable: " + n.getClass().getName(), n);
	}
}
 
Example #10
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 4 votes vote down vote up
private FunctionCallTerm findOrCreateFunctionCallTerm(FunctionCall fc){
	FunctionCallTerm t = factory.findOrCreateFunctionCallTerm(fc);
	generator.addTermLineNumber(t, fc.getLineno());
	return t;
}
 
Example #11
Source File: ConstraintVisitor.java    From SJS with Apache License 2.0 2 votes vote down vote up
/**
 * for function calls, return a constraint variable that corresponds to the
 * callee's return type. The generation of constraints that require the type of each actual
 *  parameter to be a subtype of the type of the corresponding formal parameter happens
 *  in processFunctionCallParams
 */
private ITypeTerm processFunctionCallExpression(FunctionCall fc) {
	return findOrCreateFunctionCallTerm(fc);
}