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 gnu.expr;
12:
13: import gnu.bytecode.*;
14: import gnu.mapping.*;
15:
16: /**
17: Instantiate an object and call one of its constructors.
18:
19: @version $Date: 2003/12/15 02:40:16 $
20: @author Daniel Bonniot (bonniot@users.sourceforge.net)
21: */
22:
23: public class InstantiateProc extends ProcedureN implements Inlineable {
24: public InstantiateProc(Method constructor) {
25: this (constructor, 0);
26: }
27:
28: public InstantiateProc(Method constructor, int dummyArgs) {
29: this .constructor = constructor;
30: this .dummyArgs = dummyArgs;
31: }
32:
33: public InstantiateProc(ConstructorExp method) {
34: this .method = method;
35: }
36:
37: private Method constructor;
38: private ConstructorExp method;
39: private int dummyArgs;
40:
41: public void compile(ApplyExp exp, Compilation comp, Target target) {
42: if (constructor == null)
43: constructor = method.getMainMethod();
44:
45: gnu.bytecode.CodeAttr code = comp.getCode();
46: Expression[] args = exp.getArgs();
47: Type[] types = constructor.getParameterTypes();
48:
49: ClassType type = constructor.getDeclaringClass();
50: code.emitNew(type);
51: code.emitDup(type);
52:
53: for (int i = 0; i < args.length; i++)
54: args[i].compile(comp, types[i]);
55:
56: // Add dummy arguments to match the bytecode constructor.
57: if (method != null)
58: dummyArgs = method.dummyArgs;
59: for (int i = 0; i < dummyArgs; i++)
60: code.emitPushInt(0);
61:
62: code.emitInvokeSpecial(constructor);
63: target.compileFromStack(comp, type);
64: }
65:
66: public Type getReturnType(Expression[] args) {
67: if (constructor != null)
68: return constructor.getDeclaringClass();
69: else
70: return method.getClassType();
71: }
72:
73: /****************************************************************
74: * Interpretation
75: ****************************************************************/
76:
77: public Object applyN(Object args[]) {
78: throw new Error("Not implemented");
79: }
80: }
|