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:
15: /**
16: A constructor method.
17:
18: @version $Date: 2005/03/01 17:36:35 $
19: @author Daniel Bonniot (bonniot@users.sourceforge.net)
20: */
21:
22: public class ConstructorExp extends LambdaExp {
23: public ConstructorExp(Declaration this Decl) {
24: this .this Decl = this Decl;
25: this .classType = (ClassType) this Decl.getType();
26: this Decl.context = this ;
27: }
28:
29: public ConstructorExp(ClassType classType) {
30: this .classType = classType;
31: }
32:
33: public void setSuperCall(Expression super Call) {
34: this .super Call = super Call;
35: }
36:
37: private Declaration this Decl;
38: private ClassType classType;
39: private Expression super Call;
40:
41: public Declaration getThisDecl() {
42: return this Decl;
43: }
44:
45: ClassType getClassType() {
46: return classType;
47: }
48:
49: public Method getMainMethod() {
50: if (primMethods == null)
51: addMethodFor(classType, null, null);
52: return primMethods[0];
53: }
54:
55: void addMethodFor(ClassType ctype, Compilation comp,
56: ObjectType closureEnvType) {
57: if (primMethods != null)
58: return;
59:
60: closureEnv = declareThis(ctype);
61:
62: Type[] args = new Type[min_args];
63: Declaration var = firstDecl();
64: for (int itype = 0; var != null; var = var.nextDecl())
65: args[itype++] = var.getType().getImplementationType();
66:
67: // Make sure the signature is unique
68: while (ctype.getDeclaredMethod("<init>", args) != null) {
69: Type[] newArgs = new Type[args.length + 1];
70: System.arraycopy(args, 0, newArgs, 0, args.length);
71: newArgs[args.length] = Type.int_type;
72: args = newArgs;
73: dummyArgs++;
74: addDeclaration("dummy");
75: }
76:
77: Method method = ctype.addMethod("<init>", args, Type.void_type,
78: Access.PUBLIC);
79: primMethods = new Method[] { method };
80:
81: addAttributes(method);
82: }
83:
84: /** Number of dummy arguments added to make the signature unique. */
85: int dummyArgs = 0;
86:
87: void enterFunction(Compilation comp) {
88: // The super call has to come before anything else.
89: if (super Call != null)
90: super Call.compile(comp, Target.Ignore);
91:
92: // Do the normal stuff.
93: super.enterFunction(comp);
94: }
95: }
|