Package anvil.script.statements

Source Code of anvil.script.statements.TryStatement

/*
* $Id: TryStatement.java,v 1.7 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.Location;
import anvil.codec.Code;
import anvil.codec.ExceptionHandler;
import anvil.parser.Tag;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.ScriptException;
import anvil.script.parser.TemplateParser;
import java.io.IOException;

/**
* class TryStatement
*
* @author: Jani Lehtim�ki
*/
public class TryStatement extends ScopedStatement
{

  protected Statement        _statement = EMPTY;
  protected CatchStatement[] _catch   = new CatchStatement[4];
  protected int              _catches = 0;
  protected FinallyStatement _finally = null;
  protected ExceptionHandler _handler = null;


  public TryStatement(Statement parent, Location location)
  {
    super(parent, location);
  }


  public int typeOf()
  {
    return Statement.ST_TRY;
  }


  public String name()
  {
    return "try";
  }


  public Statement getChildStatement()
  {
    return _statement;
  }  


  public void setChildStatement(Statement statement)
  {
    _statement = statement;
  }  


  public FinallyStatement getFinally()
  {
    return _finally;
  }


  public boolean hasFinally()
  {
    return _finally != null;
  }


  public void addCatch(CatchStatement stmt)
  {
    int n = _catch.length;
    if (_catches >= n) {
      CatchStatement[] katch = new CatchStatement[n += 4];
      System.arraycopy(_catch, 0, katch, 0, n);
      _catch = katch;
    }
     _catch[_catches++] = stmt;
  }


  public void setFinally(FinallyStatement stmt)
  {
    _finally = stmt;
  }


  public boolean onTag(TemplateParser parser, int type, Tag tag)
  {
    return true
  }


  public void check(ErrorListener context)
  {
    _statement.check(context);
    boolean seentypeless = false;
    for(int i=0; i<_catches; i++) {
      CatchStatement katch = _catch[i];
      if (seentypeless) {
        context.error(katch.getLocation(), "Dead code: preceding non-typed catch renders this catch block inaccessible");
      }
      if (katch.isTypeless()) {
        if (seentypeless) {
          context.error(katch.getLocation(), "There may be only one non-typed catch statement in catch sequence");
        }
        seentypeless = true;
      }
      katch.check(context);
    }
    if (_finally != null) {
      _finally.check(context);
    }
  }


  public Jumps eliminate(ErrorListener context)
  {
    Jumps jumps = _statement.eliminate(context);

    for(int i=0; i<_catches; i++) {
      Statement katch = _catch[i];
      boolean blocked = jumps.isBlocked();
      jumps.setThrow(false).setBlocked(false);
      Jumps j = katch.eliminate(context);
      blocked = blocked && j.isBlocked();
      jumps.merge(j);
      jumps.setBlocked(blocked);
    }
    if (_finally != null) {
      boolean blocked = jumps.isBlocked();
      Jumps j = _finally.eliminate(context);
      blocked = blocked || j.isBlocked();
      jumps.merge(j);
      jumps.setBlocked(blocked);
    }
    //_isblocked = jumps.isBlocked();
    return jumps;
  }


  public void compile(ByteCompiler context)
  {
    boolean has_try = (_catches > 0) || (_finally != null);
    if (has_try) {
      Code code = context.getCode();

      // try
      ExceptionHandler handler = code.startExceptionHandler(_finally != null);
      _handler = handler;
      if (_statement == EMPTY) {
        code.nop();
      } else {
        _statement.compile(context);
      }
      handler.endTry();
      if (!_statement.isBlocked()) {
        handler.callFinally();
        handler.jumpOut();
      }

      // catch
      if (_catches > 0) {
        handler.startCatch(code.getPool().addClass("anvil/script/ScriptException"));
        int l_throwable = code.addLocal();
        int l_data = code.addLocal();
        code.astore(l_throwable);
        code.aload(l_throwable);
        code.invokevirtual(code.getPool().addMethodRef("anvil/script/ScriptException", "getData", "()Lanvil/core/Any;"));
        code.astore(l_data);
        for(int i=0; i<_catches; i++) {
          _catch[i].compile(context, handler, l_data);
        }
        code.aload(l_throwable);
        code.athrow();
      }

      handler.endProtectedRegion();

      if (_finally != null) {
        handler.startCatch(0);
        int thrown = code.addLocal();
        code.astore(thrown);
        handler.callFinally();
        code.aload(thrown);
        code.athrow();
      }
      handler.endCatches();
      _handler = null;

      // finally
      if (_finally != null) {
        handler.startFinally();
        int returnto = code.addLocal();
        code.astore(returnto);
        _finally.compile(context);
        if (!_finally.isBlocked()) {
          code.ret(returnto);
        }
        handler.endFinally();
      }
     
      handler.end();
     
    } else {
      _statement.compile(context);
    }
  }


  public boolean callFinalizer()
  {
    if (_handler == null) {
      return false;
    }
    if (_finally != null) {
      _handler.callFinally();
      if (_finally.isBlocked()) {
        return true;
      }
    }
    return false;
  }

}
TOP

Related Classes of anvil.script.statements.TryStatement

TOP
Copyright © 2018 www.massapi.com. 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.