01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2002 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.lang.inline;
12:
13: /**
14: Throws its first argument as exception.
15:
16: The first argument must be a subtype of nice.lang.Throwable
17: or the bytecode generated will be incorrect.
18:
19: @version $Date: 2002/02/26 14:59:02 $
20: @author Per Bothner
21: @author Daniel Bonniot (Daniel.Bonniot@inria.fr)
22: */
23:
24: import gnu.bytecode.*;
25: import gnu.mapping.*;
26: import gnu.expr.*;
27:
28: public class Throw extends Procedure1 implements Inlineable {
29: public static Throw create(String param) {
30: return instance;
31: }
32:
33: public final static Throw instance = new Throw();
34:
35: public void compile(ApplyExp exp, Compilation comp, Target target) {
36: gnu.bytecode.CodeAttr code = comp.getCode();
37: exp.getArgs()[0].compile(comp, Target.pushObject);
38: // Ensure that the object is Throwable so that the bytecode is well-formed.
39: // This is only necessary in presence of fancy polymorphism.
40: if (!(code.topType().isSubtype(Type.throwable_type)))
41: code.emitCheckcast(Type.throwable_type);
42: code.emitThrow();
43: }
44:
45: public Type getReturnType(Expression[] args) {
46: return Type.neverReturnsType;
47: }
48:
49: /****************************************************************
50: * Interpretation
51: ****************************************************************/
52:
53: public Object apply1(Object arg1) {
54: // Throw it if it is not a checked exception
55: if (arg1 instanceof Throwable)
56: throw ((RuntimeException) arg1);
57: else if (arg1 instanceof Error)
58: throw ((Error) arg1);
59: // Otherwise wrap it
60: else
61: throw (new RuntimeException(arg1.toString()));
62: }
63: }
|