/*
* $Id: CatchStatement.java,v 1.18 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.statements;
import anvil.core.Any;
import anvil.Location;
import anvil.codec.Code;
import anvil.codec.ConstantPool;
import anvil.codec.Source;
import anvil.codec.ExceptionHandler;
import anvil.parser.Tag;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.parser.TemplateParser;
import anvil.script.expression.ExpressionList;
import anvil.script.expression.Expression;
import anvil.script.expression.Node;
import anvil.script.expression.TypeNode;
import java.io.IOException;
/**
* class CatchStatement
*
* @author: Jani Lehtim�ki
*/
public class CatchStatement extends ScopedStatement
{
protected Statement _statement = EMPTY;
protected Expression _assignment;
protected ExpressionList _types;
protected String _paramname = null;
public CatchStatement(Statement parent, Location location, Expression assignment, ExpressionList types)
{
super(parent, location);
_assignment = assignment;
_types = types;
}
public int typeOf()
{
return Statement.ST_CATCH;
}
public String name()
{
return "catch";
}
public void parse(TemplateParser parser, Tag tag)
{
}
public boolean onTag(TemplateParser parser, int type, Tag tag)
{
return true;
}
public Statement getChildStatement()
{
return _statement;
}
public void setChildStatement(Statement statement)
{
_statement = statement;
}
public void check(ErrorListener context)
{
_assignment.check(context);
if (!_assignment.isAssignable()) {
context.error(getLocation(), "Catch variable is not assignable");
}
_statement.check(context);
if (_types != null) {
_types.check(context);
}
}
public Jumps eliminate(ErrorListener context)
{
Jumps jumps = _statement.eliminate(context);
return jumps;
}
protected class DataNode extends Node
{
private int _data;
public DataNode(int data)
{
_data = data;
}
public void compile(ByteCompiler context, int operation)
{
context.getCode().aload(_data);
}
}
public void compile(ByteCompiler context, ExceptionHandler handler, int l_data)
{
Code code = context.getCode();
ConstantPool pool = code.getPool();
DataNode datanode = new DataNode(l_data);
if (_types != null) {
Source istrue = code.getSource();
Source isfalse = null;
int n = _types.childs();
boolean assigned = false;
for(int i=0; i<n; i++) {
if (_types.getChild(i).typeOf() != Node.EXPR_TYPE) {
_assignment.compile(context, datanode);
code.pop();
assigned = true;
}
}
for(int i=0; i<n; i++) {
if (isfalse != null) {
isfalse.bind();
}
Node child = _types.getChild(i);
if (child.typeOf() == Node.EXPR_TYPE) {
code.aload(l_data);
child.compile(context, Node.GET_TYPE);
code.invokevirtual(pool.addMethodRef(context.TYPE_ANY, "isInstanceOf", "(Lanvil/script/Type;)Z"));
} else {
child.compile(context, Node.GET_BOOLEAN);
}
isfalse = code.if_eq();
code.go_to(istrue);
}
istrue.bind();
if (!assigned) {
_assignment.compile(context, datanode);
code.pop();
}
_statement.compile(context);
handler.callFinally();
handler.jumpOut();
isfalse.bind();
} else {
_assignment.compile(context, datanode);
code.pop();
_statement.compile(context);
handler.callFinally();
handler.jumpOut();
}
}
public boolean isTypeless()
{
return _types == null;
}
}