use of org.mozilla.javascript.ast.Symbol in project HL4A by HL4A.
the class Parser method defineSymbol.
void defineSymbol(int declType, String name, boolean ignoreNotInBlock) {
if (name == null) {
if (compilerEnv.isIdeMode()) {
// be robust in IDE-mode
return;
} else {
codeBug();
}
}
Scope definingScope = currentScope.getDefiningScope(name);
Symbol symbol = definingScope != null ? definingScope.getSymbol(name) : null;
int symDeclType = symbol != null ? symbol.getDeclType() : -1;
if (symbol != null && (symDeclType == Token.CONST || declType == Token.CONST || (definingScope == currentScope && symDeclType == Token.LET))) {
addError(symDeclType == Token.CONST ? "msg.const.redecl" : symDeclType == Token.LET ? "msg.let.redecl" : symDeclType == Token.VAR ? "msg.var.redecl" : symDeclType == Token.FUNCTION ? "msg.fn.redecl" : "msg.parm.redecl", name);
return;
}
switch(declType) {
case Token.LET:
if (!ignoreNotInBlock && ((currentScope.getType() == Token.IF) || currentScope instanceof Loop)) {
addError("msg.let.decl.not.in.block");
return;
}
currentScope.putSymbol(new Symbol(declType, name));
return;
case Token.VAR:
case Token.CONST:
case Token.FUNCTION:
if (symbol != null) {
if (symDeclType == Token.VAR)
addStrictWarning("msg.var.redecl", name);
else if (symDeclType == Token.LP) {
addStrictWarning("msg.var.hides.arg", name);
}
} else {
currentScriptOrFn.putSymbol(new Symbol(declType, name));
}
return;
case Token.LP:
if (symbol != null) {
// must be duplicate parameter. Second parameter hides the
// first, so go ahead and add the second parameter
addWarning("msg.dup.parms", name);
}
currentScriptOrFn.putSymbol(new Symbol(declType, name));
return;
default:
throw codeBug();
}
}
use of org.mozilla.javascript.ast.Symbol in project HL4A by HL4A.
the class IRFactory method initFunction.
private Node initFunction(FunctionNode fnNode, int functionIndex, Node statements, int functionType) {
fnNode.setFunctionType(functionType);
fnNode.addChildToBack(statements);
int functionCount = fnNode.getFunctionCount();
if (functionCount != 0) {
// Functions containing other functions require activation objects
fnNode.setRequiresActivation();
}
if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
Name name = fnNode.getFunctionName();
if (name != null && name.length() != 0 && fnNode.getSymbol(name.getIdentifier()) == null) {
// A function expression needs to have its name as a
// variable (if it isn't already allocated as a variable).
// See ECMA Ch. 13. We add code to the beginning of the
// function to initialize a local variable of the
// function's name to the function value, but only if the
// function doesn't already define a formal parameter, var,
// or nested function with the same name.
fnNode.putSymbol(new Symbol(Token.FUNCTION, name.getIdentifier()));
Node setFn = new Node(Token.EXPR_VOID, new Node(Token.SETNAME, Node.newString(Token.BINDNAME, name.getIdentifier()), new Node(Token.THISFN)));
statements.addChildrenToFront(setFn);
}
}
// Add return to end if needed.
Node lastStmt = statements.getLastChild();
if (lastStmt == null || lastStmt.getType() != Token.RETURN) {
statements.addChildToBack(new Node(Token.RETURN));
}
Node result = Node.newString(Token.FUNCTION, fnNode.getName());
result.putIntProp(Node.FUNCTION_PROP, functionIndex);
return result;
}
Aggregations