001: /**************************************************************************/
002: /* N I C E */
003: /* A high-level object-oriented research language */
004: /* (c) Daniel Bonniot 2002 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package gnu.expr;
012:
013: import gnu.bytecode.*;
014: import gnu.mapping.*;
015:
016: /**
017: Call one of the constructors of a class.
018:
019: @version $Date: 2004/02/20 13:11:49 $
020: @author Daniel Bonniot (bonniot@users.sourceforge.net)
021: */
022:
023: public class InitializeProc extends ProcedureN implements Inlineable {
024: public InitializeProc(Method constructor) {
025: this (constructor, false, 0);
026: }
027:
028: /**
029: @param implicitThis true if a 'this' argument should be added
030: during the call.
031: */
032: public InitializeProc(Method constructor, boolean implicitThis) {
033: this (constructor, implicitThis, 0);
034: }
035:
036: /**
037: @param implicitThis true if a 'this' argument should be added
038: during the call.
039: */
040: public InitializeProc(Method constructor, boolean implicitThis,
041: int dummyArgs) {
042: this .constructor = constructor;
043: this .implicitThis = implicitThis;
044: this .dummyArgs = dummyArgs;
045: }
046:
047: public InitializeProc(ConstructorExp method) {
048: this (method, false);
049: }
050:
051: /**
052: @param implicitThis true if a 'this' argument should be added
053: during the call.
054: */
055: public InitializeProc(ConstructorExp method, boolean implicitThis) {
056: this .method = method;
057: this .implicitThis = implicitThis;
058: }
059:
060: private Method constructor;
061: private ConstructorExp method;
062: private boolean implicitThis;
063: private int dummyArgs;
064:
065: public void compile(ApplyExp exp, Compilation comp, Target target) {
066: if (constructor == null)
067: constructor = method.getMainMethod();
068:
069: gnu.bytecode.CodeAttr code = comp.getCode();
070: Expression[] args = exp.getArgs();
071: Type[] types = constructor.getParameterTypes();
072:
073: int arg = 0;
074: int type = 0;
075: if (implicitThis)
076: code.emitPushThis();
077: else
078: args[arg++].compile(comp, Target.pushObject);
079: for (; arg < args.length; arg++)
080: args[arg].compile(comp, types[type++]);
081:
082: // Add dummy arguments to match the bytecode constructor.
083: if (method != null)
084: dummyArgs = method.dummyArgs;
085: for (int i = 0; i < dummyArgs; i++)
086: code.emitPushInt(0);
087:
088: try {
089: code.emitInvokeSpecial(constructor);
090: } catch (VerificationError e) {
091: throw bossa.util.User.error(bossa.util.Location.make(exp),
092: e.getMessage());
093: }
094: }
095:
096: public Type getReturnType(Expression[] args) {
097: return Type.void_type;
098: }
099:
100: public String toString() {
101: String res;
102: if (constructor != null)
103: res = constructor.toString();
104: else if (method != null)
105: res = method.toString();
106: else
107: res = "?";
108: return "Initialization: " + res;
109: }
110:
111: /****************************************************************
112: * Interpretation
113: ****************************************************************/
114:
115: public Object applyN(Object args[]) {
116: throw new Error("Not implemented");
117: }
118: }
|