root/trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/values/Constructor.java

Revision 2072, 26.8 kB (checked in by dlsmith, 2 months ago)

Updated ASTGen with improved support for custom extensions. Related bug fixes in ASTGen exposed the fact that the Applicable node class doesn't belong in the Node hierarchy -- there are non-node instances that don't support visiting, for example -- so it was moved to nodes_util. Similarly, Fortress.ast was adjusted to be compatible with clarified semantics of ASTGen.

Line 
1 /*******************************************************************************
2     Copyright 2008 Sun Microsystems, Inc.,
3     4150 Network Circle, Santa Clara, California 95054, U.S.A.
4     All rights reserved.
5
6     U.S. Government Rights - Commercial software.
7     Government users are subject to the Sun Microsystems, Inc. standard
8     license agreement and applicable provisions of the FAR and its supplements.
9
10     Use is subject to license terms.
11
12     This distribution may include materials developed by third parties.
13
14     Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15     trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16  ******************************************************************************/
17
18 package com.sun.fortress.interpreter.evaluator.values;
19
20 import static com.sun.fortress.exceptions.InterpreterBug.bug;
21 import static com.sun.fortress.exceptions.ProgramError.error;
22 import static com.sun.fortress.exceptions.ProgramError.errorMsg;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import com.sun.fortress.interpreter.env.BetterEnv;
32 import com.sun.fortress.interpreter.evaluator.BuildEnvironments;
33 import com.sun.fortress.interpreter.evaluator.Environment;
34 import com.sun.fortress.interpreter.evaluator.EvalType;
35 import com.sun.fortress.interpreter.evaluator.EvalVarsEnvironment;
36 import com.sun.fortress.interpreter.evaluator.types.FTraitOrObject;
37 import com.sun.fortress.interpreter.evaluator.types.FType;
38 import com.sun.fortress.interpreter.evaluator.types.FTypeArrow;
39 import com.sun.fortress.interpreter.evaluator.types.FTypeObject;
40 import com.sun.fortress.interpreter.evaluator.types.FTypeTrait;
41 import com.sun.fortress.interpreter.glue.NativeApp;
42 import com.sun.fortress.interpreter.glue.WellKnownNames;
43 import com.sun.fortress.nodes.AbsDeclOrDecl;
44 import com.sun.fortress.nodes.FnDef;
45 import com.sun.fortress.nodes.GenericWithParams;
46 import com.sun.fortress.nodes.IdOrOpOrAnonymousName;
47 import com.sun.fortress.nodes.Param;
48 import com.sun.fortress.nodes_util.Applicable;
49 import com.sun.fortress.nodes_util.NodeFactory;
50 import com.sun.fortress.useful.GHashMap;
51 import com.sun.fortress.useful.HasAt;
52 import com.sun.fortress.useful.MultiMap;
53 import com.sun.fortress.useful.ReversedList;
54 import com.sun.fortress.useful.Useful;
55
56 import edu.rice.cs.plt.tuple.Option;
57
58 public class Constructor extends NonPrimitive {
59
60     // TODO need to be more organized about all the names
61     // that get rewritten.
62  //   public HashSet<String> parameterNames = new HashSet<String>();
63
64     private HasAt at;
65     protected FTypeObject selfType;
66
67    @Override
68     public HasAt getAt() {
69         return at;
70     }
71
72     public String stringName() {
73         return "Constructor for " + selfType;
74     }
75
76     public boolean seqv(FValue v) { return false; }
77
78     boolean finished = false;
79
80     IdOrOpOrAnonymousName cfn;
81     List<? extends AbsDeclOrDecl> defs;
82     Option<List<Param>> params;
83
84     MultiMap<FTraitOrObject, SingleFcn> traitsToMethodSets =
85         new MultiMap<FTraitOrObject, SingleFcn>();
86
87     MultiMap<String, MethodClosure> namesToSignatureSets =
88         new MultiMap<String, MethodClosure>();
89
90     MultiMap<FTraitOrObject, String> traitsToNamesReferenced =
91         new MultiMap<FTraitOrObject, String>();
92
93     // sets of strings (can't be explicit due to erasure)
94     Set<?>[] traitNameReferenceArray;
95
96     FTraitOrObject[] traitArray;
97     MethodClosure[] methodsArray;
98     MethodClosure[] closuresArray;
99     int[] traitIndexForMethod; // 0 = object
100     int[] overloadMembership;  // 0 = no overload
101     int overloadCount = 0; // first overload is indexed at 1.
102
103     Environment methodsEnv;
104
105     public Constructor(Environment env,
106             FTypeObject selfType,
107             GenericWithParams def) {
108         this(env,
109                 selfType,
110                 (HasAt) def,
111                 NodeFactory.makeConstructorFnName(def),
112                 def.getDecls(),
113                 def.getParams()
114                 );
115  //       addParamsToCollection(def, parameterNames);
116     }
117
118     public Constructor(Environment env,
119             FTypeObject selfType,
120             GenericWithParams def,
121             Option<List<Param>> params) {
122         this(env,
123                 selfType,
124                 (HasAt) def,
125                 NodeFactory.makeConstructorFnName(def),
126                 def.getDecls(),
127                 params
128                 );
129  //       addParamsToCollection(def, parameterNames);
130     }
131
132
133     /**
134      * @param def
135      */
136 //    static public void addParamsToCollection(
137 //          HasParams def, Collection<String> parameterNames) {
138 //        addParamsToCollection(def.getParams(), parameterNames);
139 //
140 //    }
141 //    static public void addParamsToCollection(
142 //          Option<List<Param>> opt_params, Collection<String> parameterNames) {
143 //        if (opt_params.isSome()) {
144 //            addParamsToCollection(opt_params.unwrap(), parameterNames);
145 //        }
146 //    }
147 //    static public void addParamsToCollection(
148 //          List<Param> params, Collection<String> parameterNames) {
149 //        for (Param p : params) {
150 //                if (!NodeUtil.isTransient(p))
151 //                    parameterNames.add(p.getName().getId().getText());
152 //       }
153 //    }
154 //    static public void removeParamsFromCollection(
155 //          ObjectDecl def, Collection<String> parameterNames) {
156 //        removeParamsFromCollection(def.getParams(), parameterNames);
157 //
158 //    }
159 //    static public void removeParamsFromCollection(
160 //          Option<List<Param>> opt_params, Collection<String> parameterNames) {
161 //        if (opt_params.isSome()) {
162 //            removeParamsFromCollection(opt_params.unwrap(), parameterNames);
163 //        }
164 //    }
165 //    static public void removeParamsFromCollection(
166 //          List<Param> params,Collection<String> parameterNames) {
167 //        for (Param p : params) {
168 //            if (!NodeUtil.isTransient(p))
169 //                parameterNames.remove(p.getName().getId().getText());
170 //   }
171 //}
172
173     // TODO need to copy the field names
174
175     public Constructor(Environment env, FTypeObject selfType, HasAt def,
176                 IdOrOpOrAnonymousName name, List<? extends AbsDeclOrDecl> defs,
177                 Option<List<Param>> params) {
178         super(env); // TODO verify that this is the proper env.
179         this.selfType = selfType;
180         this.at = def;
181         this.cfn = name;
182         this.defs = defs;
183         this.params = params;
184     }
185
186     /**
187       * Figure out the methods inherited from the super-traits.
188       */
189     public void finishInitializing() {
190         // Next build a bogus environment to help us figure out
191         // overloading, shadowing, etc.  First puts win.
192         if (params.isSome()) {
193             List<Parameter> fparams = EvalType.paramsToParameters(
194                     getWithin(), params.unwrap());
195             setParams(fparams);
196         } else {
197             setParams(Collections.<Parameter> emptyList());
198         }
199
200         Environment bte = selfType.getMethodExecutionEnv(); // new BetterEnv(getWithin(), getAt());
201         selfType.getMembers(); // has initializing side-effect.
202         finishInitializing(bte);
203     }
204
205     private void finishInitializing(Environment bte) {
206
207        // HashSet<String> fields = new HashSet<String>();
208          // HashMap<String, String> com.sun.fortress.interpreter.rewrite =
209         //  new HashMap<String, String>();
210
211       //  BuildObjectEnvironment bt =
212       //      new BuildObjectEnvironment(bte, selfType.getWithin(), selfType, fields);
213
214         // Inject methods into this environment
215         // This should create new MethodClosures
216        // visitDefs(bt);
217
218         GHashMap<SingleFcn, FTraitOrObject>
219         signaturesToTraitsContainingMethods =
220             new GHashMap<SingleFcn, FTraitOrObject>(
221                      SingleFcn.signatureEquivalence);
222
223         MultiMap<String, GenericMethod> generics =
224              new MultiMap<String, GenericMethod>();
225
226         // TODO deal with ORDER and ambiguity.  TransitiveExtends returns
227         // a topological sort, which is close, but not perfect.
228         //
229         List<FType> extendedTraits = selfType.getProperTransitiveExtends();
230
231         // First process all the generics, so we can form generic functions
232         // TODO this is approximate; add generics in reversed order.
233         for (FType t : new ReversedList<FType>(extendedTraits)) {
234             FTypeTrait ft = (FTypeTrait) t;
235             BetterEnv e = ft.getMembers(); // This is correct, it enumerates the methods.
236             accumulateGenericMethods(generics, ft, e);
237         }
238
239         // This also enumerates the methods.
240         accumulateGenericMethods(generics, selfType, bte);
241
242         // For each set of generic methods (with same name and signature)
243         // create a list of symbolic values to substitute for the type
244         // parameters so that the methods may be pseudo-instantiated
245         // and plugged into the trait/object and overloading definition
246         // machinery.
247         Map<String, List<FType>> genericArgs =
248             new HashMap<String, List<FType>>();
249         for (String s : generics.keySet() ) {
250             // All the methods are similarly parameterized, so the
251             // first generic in the set (generics is a multimap)
252             // is as good as any other for this purpose.
253             GenericMethod g = generics.get(s).iterator().next();
254
255             Applicable ap = g.getDef();
256             List<FType> instantiationTypes =
257                 SingleFcn.createSymbolicInstantiation(bte, ap, getAt());
258             genericArgs.put(s, instantiationTypes);
259         }
260
261         final Set<String> overridden = new HashSet<String>();
262 //        final NodeVisitor_void overrideFinder = new NodeAbstractVisitor_void() {
263 //
264 //            FnAbsDeclOrDecl current;
265 //
266 //            public void forModifierOverride(ModifierOverride mo) {
267 //                overridden.add(current.stringName());
268 //                System.err.println("Override of " + current.stringName());
269 //            }
270 //
271 //            @Override
272 //            public void forFnAbsDeclOrDecl(FnAbsDeclOrDecl that) {
273 //                current = that;
274 //                for (Modifier m : that.getMods()) {
275 //                    m.accept(this);
276 //                }
277 //            }
278 //
279 //        };
280
281
282         //  Find all the methods in selfType, using the containing environment
283         // to give them meaning.
284         accumulateEnvMethods(null, overridden,
285           signaturesToTraitsContainingMethods,
286           generics, genericArgs, selfType, bte);
287
288 //        for (AbsDeclOrDecl d : defs ) {
289 //            d.accept(overrideFinder);
290 //        }
291         // Accumulate all the trait methods, evaluated against their
292         // trait environments.
293         // TODO The signature map uses EQUALITY, and that might be wrong,
294         // if the object can implement with a more general signature.
295         for (FType t : extendedTraits) {
296             FTypeTrait ft = (FTypeTrait) t;
297             BetterEnv e = ft.getMembers();
298             accumulateEnvMethods(overridden, null,
299              signaturesToTraitsContainingMethods, generics, genericArgs, ft, e);
300         }
301
302         // Check that all methods are defined, also check to see
303         // if the object defines any of them.
304         boolean objectDefinesAny = false;
305         for (Map.Entry<SingleFcn, FTraitOrObject> ent :
306                  (Set<Map.Entry<SingleFcn, FTraitOrObject>>)
307                      signaturesToTraitsContainingMethods.entrySet()) {
308             SingleFcn sf = ent.getKey();
309             FTraitOrObject too = ent.getValue();
310
311             if (too instanceof FTypeObject)
312                 objectDefinesAny = true;
313             checkForDef(sf,too);
314         }
315
316         // Plan to iterate over traits at instantiation, and form closures
317         // for methods defined there based on the trait environment and
318         // a filter of the object environment into that trait.
319         // Duplication of symbols into the per-method environements is
320         // ok because the symbols duplicated are immutably bound (are
321         // methods).
322         traitsToMethodSets.addInverse(signaturesToTraitsContainingMethods);
323
324         // Count traits and create an array of trait types, so that
325         // the constructor can build things without thinking.
326         // int traitCount = traitsToMethodSets.size() + (objectDefinesAny ? 0 : 1);
327         int traitCount =
328             traitsToNamesReferenced.size() + (objectDefinesAny ? 0 : 1);
329
330         HashMap<FTraitOrObject, Integer> traitToIndex =
331             new HashMap<FTraitOrObject, Integer> ();
332
333         traitArray = new FTraitOrObject[traitCount];
334         traitNameReferenceArray = new Set<?>[traitCount];
335
336         traitArray[0] = selfType;
337         traitToIndex.put(selfType, Integer.valueOf(0));
338         int trait_i = 1;
339         for (FTraitOrObject too : traitsToNamesReferenced.keySet() ) { // was traitsToMethodSets.keySet()
340             if (too != selfType) {
341                 traitArray[trait_i] = too;
342                 traitToIndex.put(too, Integer.valueOf(trait_i));
343                 traitNameReferenceArray[trait_i] =
344                     traitsToNamesReferenced.get(too);
345                 trait_i ++;
346             }
347         }
348
349         /* At construction time,
350           1) create an array of environments (one per trait)
351           2) iterate over the methods, and assign each of them the appropriate
352              environment to form closures, setting the results aside.
353           3) then form any overloads necessary
354           4) then iterate over the traits, binding names to method values.
355          */
356         int signatureCount = signaturesToTraitsContainingMethods.size()+1;
357         methodsArray = new MethodClosure[signatureCount];
358         closuresArray = new MethodClosure[signatureCount];
359         HashMap<MethodClosure, Integer> methodsIndex =
360             new HashMap<MethodClosure, Integer> ();
361
362         traitIndexForMethod = new int[signatureCount];
363         overloadMembership = new int[signatureCount];
364
365         // Iterate over the signatures to create a map from names to sets
366         // of functions, for use by overloading.  Also create indexing
367         // for methods, and initialize arrays from method (signature)
368         // index to partially defined method (carries base environment),
369         // and trait index.
370         int sig_i = 1;
371
372         for (Map.Entry<SingleFcn, FTraitOrObject> ent :
373             (Set<Map.Entry<SingleFcn, FTraitOrObject>>)
374             signaturesToTraitsContainingMethods.entrySet()) {
375
376             SingleFcn sf = ent.getKey();
377             /*
378              * This is a moderate hack.  Because the methods for any
379              * particular trait must obey the overloading rules individually,
380              * overloaded things also get bound "as methods".  However, the
381              * the rules for binding methods to actual objects behave as-if the
382              * overloaded method is dismantled and separately overridden.  To
383              * take advantage of the SingleFcn-specific machinery in
384              * OverloadedFunctions, the type signature of the "early" data
385              * structures in this process allow SingleFcn's to appear, but
386              * now (right here) they had better be gone.
387              *
388              */
389             if (! (sf instanceof MethodClosure)) {
390                 bug(errorMsg("Internal error, non-method ", sf));
391             }
392             MethodClosure mc = (MethodClosure) sf;
393             FTraitOrObject too = ent.getValue();
394
395             Set<MethodClosure> s =
396                 namesToSignatureSets.putItem(sf.asMethodName(), mc);
397             if (s.size() == 2)
398                 overloadCount++;
399             methodsArray[sig_i] = mc;
400             methodsIndex.put(mc, Integer.valueOf(sig_i));
401             traitIndexForMethod[sig_i] = traitToIndex.get(too).intValue();
402
403             sig_i++;
404         }
405
406         // If the same method name is defined with more than one signature,
407         // then it is overloaded.  If a method is part of an overload,
408         // record its membership in the overloadIndex array.
409         int overloadIndex = 1;
410         for (Map.Entry<String, Set<MethodClosure>> ent:
411             namesToSignatureSets.entrySet()) {
412             Set<MethodClosure> s = ent.getValue();
413             if (s.size() > 1) {
414                 // This will pop an error if the set of defined functions is
415                 // a bad overload.  Discard the value.
416                 new OverloadedMethod(ent.getKey(), s, getWithin());
417
418                 for (MethodClosure m : s) {
419                     overloadMembership[methodsIndex.get(m).intValue() ] =
420                         overloadIndex;
421                 }
422                 overloadIndex++;
423             }
424         }
425
426         // Experimental early computation of this information.
427         for (int i = 1; i < methodsArray.length; i++) {
428             // Closure cl = methodsArray[i].completeClosure(trait_envs[traitIndexForMethod[i]]);
429             MethodClosure cl = methodsArray[i];
430             // .completeClosure(traitArray[traitIndexForMethod[i]].getEnv());
431             closuresArray[i] = cl;
432         }
433
434
435         methodsEnv = within.extend();
436         addMethodsToEnv(methodsEnv);
437         methodsEnv.bless();
438
439         finished = true;
440     }
441
442     /**
443       * Checks for definition of sf from supertrait too, fails if absent.
444       * @param sf
445       * @param too
446       * @return
447       */
448     private void checkForDef(SingleFcn sf, FTraitOrObject too) {
449         if (too instanceof FTypeObject) return;
450         if (sf instanceof MethodClosure) {
451             MethodClosure pdm = (MethodClosure) sf;
452             Applicable a = pdm.getDef();
453             if (a instanceof FnDef || a instanceof NativeApp) return;
454             error(cfn, errorMsg("Object ",cfn.stringName(),
455                   " does not define an abstract method declared in type ",
456                                 too.getName(), ":\n  ", sf.getString(),
457                                 "\n   instead found: ",a," class ",a.getClass()));
458         }
459         bug(errorMsg("Unexpected symbolic method binding ", sf));
460         return;
461     }
462
463     /**
464      * Iterates over the youngest (innermost) scope of e to determine
465      * what things are defined there, and record the relationship
466      * from signature to containing trait, and from trait to names
467      * referenced.
468      *
469      * @param signaturesToTraitsContainingMethods
470      * @param genericArgs
471      * @param ft
472      * @param e
473      */
474     private void accumulateEnvMethods(
475             Set<String> alreadyOverridden,
476             Set<String> newOverrides,
477             GHashMap<SingleFcn, FTraitOrObject> signaturesToTraitsContainingMethods,
478             MultiMap<String, GenericMethod> generics,
479             Map<String, List<FType>> genericArgs, FTraitOrObject ft, Environment e) {
480         for (String s : e.youngestFrame()) {
481             FValue fv = e.getValue(s);
482             if (alreadyOverridden == null || ! alreadyOverridden.contains(s)) {
483
484            // This has got to be wrong...
485             if (fv instanceof OverloadedFunction) {
486                 // Treat the overloaded function as a bag of separate
487                 // definitions.
488                 List<Overload> overloads = ((OverloadedFunction) fv)
489                         .getOverloads();
490                 for (Overload ov : overloads) {
491                     SingleFcn sfcn = ov.getFn();
492                     if (newOverrides != null && sfcn.isOverride())
493                         newOverrides.add(s);
494                     // extract below as method, call it here.
495                     signaturesToTraitsContainingMethods.putIfAbsent(sfcn, ft);
496                 }
497             } else
498                 if (fv instanceof GenericMethod) {
499                 GenericMethod gfv = (GenericMethod) fv;
500                 if (newOverrides != null && gfv.isOverride())
501                     newOverrides.add(s);
502
503                 MethodClosure sfcn = gfv.make(genericArgs.get(s), gfv.getAt());
504
505                 signaturesToTraitsContainingMethods.putIfAbsent(sfcn, ft);
506
507             } else if (fv instanceof MethodClosure) {
508                 MethodClosure mc = (MethodClosure) fv;
509                 signaturesToTraitsContainingMethods.putIfAbsent
510                     (mc, ft);
511                 if (newOverrides != null && mc.isOverride())
512                     newOverrides.add(s);
513             } else {
514                 bug(errorMsg("Don't handle ", fv, " yet"));
515             }
516             }
517             // Record the name to ensure that it is defined somewhere.
518             // The name of the overloaded function goes wrong, if it is a functional method.
519             traitsToNamesReferenced.putItem(ft, s);// ((Fcn)fv).asMethodName());
520         }
521     }
522
523     private void accumulateGenericMethods(
524             MultiMap<String, GenericMethod> generics,
525             FTraitOrObject ft, Environment e) {
526         for (String s : e.youngestFrame()) {
527             FValue fv = e.getValue(s);
528             if (fv instanceof GenericMethod) {
529                 // TODO This is VERY approximate
530                 generics.putItem(s, (GenericMethod) fv);
531             } else {
532                 // do nothing
533             }
534         }
535     }
536
537     public String getString() {
538         return cfn.stringName();
539     }
540
541     public String toString() {
542         String name = getFnName().toString();
543         List<FType> l = null;
544         try {
545             l = getDomain();
546         } catch (Throwable th) {
547             ; /* do nothing */
548         }
549         return (s(selfType)) + (l == null ? "(DOMAIN_ERROR null)" : Useful.listInParens(l)) + cfn.at();
550     }
551
552     public IdOrOpOrAnonymousName getFnName() {
553         return cfn;
554     }
555
556     @Override
557     public FValue applyInner(
558             List<FValue> args, HasAt loc, Environment envForInference) {
559         Environment lex_env = getWithin();
560         return applyConstructor(args, loc, lex_env);
561     }
562     /**
563      *
564      * Apply a constructor.  This method allows separate specification
565      * of the lexical environment; this is done to simplify implementation
566      * of object expressions.
567      *
568      */
569     public FValue applyConstructor(
570             List<FValue> args, HasAt loc, Environment lex_env) {
571         // Problem -- we need to detach self-env from other env.
572         if (methodsEnv == null)
573             bug("Null methods env for " + this);
574
575         Environment self_env = buildEnvFromEnvAndParams(methodsEnv, args, loc);
576
577         FObject theObject = makeAnObject(lex_env, self_env);
578
579         self_env.putValueRaw(WellKnownNames.secretSelfName, theObject);
580
581         // TODO this is WRONG.  The vars need to be inserted into self, but
582         // get evaluated against the larger (lexical) environment.  Arrrrrrrggggggh.
583
584         self_env.bless(); // HACK we add to this later.
585                           // This should go wrong if one of the vars has closure value
586                           // or objectExpr value.
587
588         if (defs.size() > 0) {
589             // Minor optimization, avoid this if no defs to eval.
590             EvalVarsEnvironment eve = new EvalVarsEnvironment(lex_env.extend(self_env), self_env);
591             visitDefs(eve); // HACK here's where we add to self_env.
592         }
593
594         return stripTransient(theObject);
595     }
596
597     private FObject stripTransient(FObject original) {
598         Environment selfEnv = original.getSelfEnv();
599         for (Parameter param : getParameters()) {
600             if (param.isTransient())
601                 selfEnv.removeVar(param.getName());
602         }
603         return makeAnObject(original.getLexicalEnv(), selfEnv);
604     }
605
606     public FValue applyOEConstructor(HasAt loc, Environment lex_env) {
607         // Problem -- we need to detach self-env from other env.
608         Environment self_env = methodsEnv.extendAt(loc);
609
610         FValue surroundSelf = lex_env.getValueNull(WellKnownNames.secretSelfName);
611         if (surroundSelf != null)
612             self_env.putValueRaw(WellKnownNames.secretParentName, surroundSelf);
613
614         FObject theObject = makeAnObject(lex_env, self_env);
615
616         self_env.putValueRaw(WellKnownNames.secretSelfName, theObject);
617
618         // TODO this is WRONG.  The vars need to be inserted into self, but
619         // get evaluated against the larger (lexical) environment.  Arrrrrrrggggggh.
620
621         self_env.bless(); // HACK we add to this later.
622                           // This should go wrong if one of the vars has closure value
623                           // or objectExpr value.
624
625         if (defs.size() > 0) {
626             // Minor optimization, avoid this if no defs to eval.
627             EvalVarsEnvironment eve = new EvalVarsEnvironment(lex_env.extend(self_env), self_env);
628             visitDefs(eve); // HACK here's where we add to self_env.
629         }
630
631         return theObject;
632     }
633
634     private void addMethodsToEnv(Environment self_env) {
635         OverloadedMethod[] overloads = new OverloadedMethod[overloadCount+1];
636
637         // First initialize an array of environments.
638 //        for (int i = 1; i < trait_envs.length; i++) {
639 //            trait_envs[i] = new ImmutableSpineEnv(traitArray[i].getEnv(), loc);
640 //        }
641
642         // For each method, attach the appropriate environment from the array
643         for (int i = 1; i < methodsArray.length; i++) {
644             // Closure cl = methodsArray[i].completeClosure(trait_envs[traitIndexForMethod[i]]);
645             Closure cl = closuresArray[i]; // methodsArray[i].completeClosure(traitArray[traitIndexForMethod[i]].getEnv());
646             int j = overloadMembership[i];
647             // Check to see if the new closure should be bound now or
648             // placed in an overload.
649             if (j != 0) {
650                 OverloadedMethod of = overloads[j];
651                 if (of == null) {
652                     // Overload in self_env
653                     of = new OverloadedMethod(cl.asMethodName(), self_env);
654                     self_env.putValue(cl.asMethodName(), of);
655                     overloads[j] = of;
656                 }
657                 of.addOverload(cl);
658             } else {
659                 // Assertion -- by construction, illegal shadowing won't occur,
660                 // because it is processed/detected in the overloading code.
661                 self_env.putValueRaw(cl.asMethodName(), cl);
662             }
663         }
664
665         // By construction, all the overloads are already ok.
666         // Bless them to avoid the overhad of checking what we
667         // "know" to be true.
668         for (int i = 1; i < overloads.length; i++) {
669             overloads[i].bless();
670         }
671     }
672
673     protected FObject makeAnObject(Environment lex_env, Environment self_env) {
674         return new FOrdinaryObject(selfType, lex_env, self_env);
675     }
676
677     /**
678      * @param be
679      */
680     protected void visitDefs(BuildEnvironments be) {
681         be.doDefs1234(defs);
682 //        be.secondPass();
683 //
684 //        be.doDefs(defs);
685 //        be.getBindingEnv().bless();
686 //
687 //        be.thirdPass();
688 //        be.doDefs(defs);
689 //
690 //        be.fourthPass();
691 //        be.doDefs(defs);
692
693     }
694
695     @Override
696     protected void setValueType() {
697         // TODO Constructors aren't exposed, do they have a type?
698         // yes, they are exposed, and they do.
699         setFtype(FTypeArrow.make(getDomain(), selfType));
700     }
701
702     @Override
703     boolean getFinished() {
704         // TODO Auto-generated method stub
705         return finished;
706     }
707
708 }
Note: See TracBrowser for help on using the browser.