KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > types > TypeSystem


1 package polyglot.types;
2
3 import java.util.Collection JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import polyglot.frontend.ExtensionInfo;
8 import polyglot.frontend.Source;
9 import polyglot.util.Position;
10
11 /**
12  * The <code>TypeSystem</code> defines the types of the language and
13  * how they are related.
14  */

15 public interface TypeSystem {
16     /**
17      * Initialize the type system with the compiler. This method must be
18      * called before any other type system method is called.
19      *
20      * @param resolver The resolver to use for loading types from class files
21      * or other source files.
22      * @param extInfo The ExtensionInfo the TypeSystem is being created for.
23      */

24     void initialize(LoadedClassResolver resolver, ExtensionInfo extInfo)
25                     throws SemanticException;
26
27     /**
28      * Returns the system resolver. This resolver can load top-level classes
29      * with fully qualified names from the class path and the source path.
30      */

31     TopLevelResolver systemResolver();
32
33     /**
34      * Return the type system's table resolver.
35      * This resolver contains types parsed from source files.
36      */

37     TableResolver parsedResolver();
38
39     /**
40      * Return the type system's loaded resolver.
41      * This resolver contains types loaded from class files.
42      */

43     LoadedClassResolver loadedResolver();
44
45     /** Create an import table for the source file.
46      * @param sourceName Name of the source file to import into. This is used
47      * mainly for error messages and for debugging.
48      * @param pkg The package of the source file in which to import.
49      */

50     ImportTable importTable(String JavaDoc sourceName, Package JavaDoc pkg);
51
52     /** Create an import table for the source file.
53      * @param pkg The package of the source file in which to import.
54      */

55     ImportTable importTable(Package JavaDoc pkg);
56
57     /**
58      * Return a list of the packages names that will be imported by
59      * default. A list of Strings is returned, not a list of Packages.
60      */

61     List JavaDoc defaultPackageImports();
62
63     /**
64      * Returns true if the package named <code>name</code> exists.
65      */

66     boolean packageExists(String JavaDoc name);
67
68     /** Get the named type object with the following name.
69      * @param name The name of the type object to look for.
70      * @exception SemanticException when object is not found.
71      */

72     Named forName(String JavaDoc name) throws SemanticException;
73     
74     /** Get the type with the following name.
75      * @param name The name to create the type for.
76      * @exception SemanticException when type is not found.
77      */

78     Type typeForName(String JavaDoc name) throws SemanticException;
79
80     /** Create an initailizer instance.
81      * @param pos Position of the initializer.
82      * @param container Containing class of the initializer.
83      * @param flags The initializer's flags.
84      */

85     InitializerInstance initializerInstance(Position pos, ClassType container,
86                                             Flags flags);
87
88     /** Create a constructor instance.
89      * @param pos Position of the constructor.
90      * @param container Containing class of the constructor.
91      * @param flags The constructor's flags.
92      * @param argTypes The constructor's formal parameter types.
93      * @param excTypes The constructor's exception throw types.
94      */

95     ConstructorInstance constructorInstance(Position pos, ClassType container,
96                                             Flags flags, List JavaDoc argTypes,
97                                             List JavaDoc excTypes);
98
99     /** Create a method instance.
100      * @param pos Position of the method.
101      * @param container Containing type of the method.
102      * @param flags The method's flags.
103      * @param returnType The method's return type.
104      * @param name The method's name.
105      * @param argTypes The method's formal parameter types.
106      * @param excTypes The method's exception throw types.
107      */

108     MethodInstance methodInstance(Position pos, ReferenceType container,
109                                   Flags flags, Type returnType, String JavaDoc name,
110                                   List JavaDoc argTypes, List JavaDoc excTypes);
111
112     /** Create a field instance.
113      * @param pos Position of the field.
114      * @param container Containing type of the field.
115      * @param flags The field's flags.
116      * @param type The field's type.
117      * @param name The field's name.
118      */

119     FieldInstance fieldInstance(Position pos, ReferenceType container,
120                                 Flags flags, Type type, String JavaDoc name);
121
122     /** Create a local variable instance.
123      * @param pos Position of the local variable.
124      * @param flags The local variable's flags.
125      * @param type The local variable's type.
126      * @param name The local variable's name.
127      */

128     LocalInstance localInstance(Position pos, Flags flags, Type type,
129                                 String JavaDoc name);
130
131     /** Create a default constructor instance.
132      * @param pos Position of the constructor.
133      * @param container Containing class of the constructor.
134      */

135     ConstructorInstance defaultConstructor(Position pos, ClassType container);
136
137     /** Get an unknown type. */
138     UnknownType unknownType(Position pos);
139
140     /** Get an unknown package. */
141     UnknownPackage unknownPackage(Position pos);
142
143     /** Get an unknown type qualifier. */
144     UnknownQualifier unknownQualifier(Position pos);
145
146     /**
147      * Returns true iff child descends from ancestor or child == ancestor.
148      * This is equivalent to:
149      * <pre>
150      * descendsFrom(child, ancestor) || equals(child, ancestor)
151      * </pre>
152      */

153     boolean isSubtype(Type child, Type ancestor);
154
155     /**
156      * Returns true iff child is not ancestor, but child descends from ancestor. */

157     boolean descendsFrom(Type child, Type ancestor);
158
159     /**
160      * Returns true iff a cast from fromType to toType is valid; in other
161      * words, some non-null members of fromType are also members of toType.
162      */

163     boolean isCastValid(Type fromType, Type toType);
164
165     /**
166      * Returns true iff an implicit cast from fromType to toType is valid;
167      * in other words, every member of fromType is member of toType.
168      */

169     boolean isImplicitCastValid(Type fromType, Type toType);
170
171     /**
172      * Returns true iff type1 and type2 represent the same type object.
173      */

174     boolean equals(TypeObject type1, TypeObject type2);
175
176     /**
177      * Returns true if <code>value</code> can be implicitly cast to type
178      * <code>t</code>. This method should be removed. It is kept for backward
179      * compatibility.
180      */

181     boolean numericConversionValid(Type t, long value);
182
183     /**
184      * Returns true if <code>value</code> can be implicitly cast to
185      * type <code>t</code>.
186      */

187     boolean numericConversionValid(Type t, Object JavaDoc value);
188
189     /**
190      * Returns the least common ancestor of type1 and type2
191      * @exception SemanticException if the LCA does not exist
192      */

193     Type leastCommonAncestor(Type type1, Type type2) throws SemanticException;
194
195     /**
196      * Returns true iff <code>type</code> is a canonical
197      * (fully qualified) type.
198      */

199     boolean isCanonical(Type type);
200
201     /**
202      * Checks whether a class member can be accessed from Context context.
203      */

204     boolean isAccessible(MemberInstance mi, Context context);
205
206     /**
207      * Checks whether a class can be accessed from Context context.
208      */

209     boolean classAccessible(ClassType ct, Context context);
210
211     /**
212      * Checks whether a top-level or member class can be accessed from the
213      * package pkg. Returns false for local and anonymous classes.
214      */

215     boolean classAccessibleFromPackage(ClassType ct, Package JavaDoc pkg);
216
217     /**
218      * Returns whether inner is enclosed within outer
219      */

220     boolean isEnclosed(ClassType inner, ClassType outer);
221
222     /**
223      * Returns whether an object of the inner class <code>inner</code> has an
224      * enclosing instance of class <code>encl</code>.
225      */

226     boolean hasEnclosingInstance(ClassType inner, ClassType encl);
227     
228     ////
229
// Various one-type predicates.
230
////
231

232     /**
233      * Returns true iff the type t can be coerced to a String in the given
234      * Context. If a type can be coerced to a String then it can be
235      * concatenated with Strings, e.g. if o is of type T, then the code snippet
236      * "" + o
237      * would be allowed.
238      */

239     boolean canCoerceToString(Type t, Context c);
240     
241     /**
242      * Returns true iff an object of type <code>type</code> may be thrown.
243      */

244     boolean isThrowable(Type type);
245
246     /**
247      * Returns a true iff the type or a supertype is in the list
248      * returned by uncheckedExceptions().
249      */

250     boolean isUncheckedException(Type type);
251
252     /**
253      * Returns a collection of the Throwable types that need not be declared
254      * in method and constructor signatures.
255      */

256     Collection JavaDoc uncheckedExceptions();
257
258     /** Unary promotion for numeric types.
259      * @exception SemanticException if the type cannot be promoted.
260      */

261     PrimitiveType promote(Type t) throws SemanticException;
262
263     /** Binary promotion for numeric types.
264      * @exception SemanticException if the types cannot be promoted.
265      */

266     PrimitiveType promote(Type t1, Type t2) throws SemanticException;
267
268     ////
269
// Functions for type membership.
270
////
271

272     /**
273      * Deprecated version of the findField method.
274      * @deprecated
275      */

276     FieldInstance findField(ReferenceType container, String JavaDoc name, Context c)
277         throws SemanticException;
278
279     /**
280      * Returns the field named 'name' defined on 'type'.
281      * We check if the field is accessible from the class currClass.
282      * @exception SemanticException if the field cannot be found or is
283      * inaccessible.
284      */

285     FieldInstance findField(ReferenceType container, String JavaDoc name, ClassType currClass)
286         throws SemanticException;
287
288     /**
289      * Returns the field named 'name' defined on 'type'.
290      * @exception SemanticException if the field cannot be found or is
291      * inaccessible.
292      */

293     FieldInstance findField(ReferenceType container, String JavaDoc name)
294     throws SemanticException;
295
296     /**
297      * Find a method. We need to pass the class from which the method
298      * is being found because the method
299      * we find depends on whether the method is accessible from that
300      * class.
301      * We also check if the field is accessible from the context 'c'.
302      * @exception SemanticException if the method cannot be found or is
303      * inaccessible.
304      */

305     MethodInstance findMethod(ReferenceType container,
306                               String JavaDoc name, List JavaDoc argTypes,
307                               ClassType currClass) throws SemanticException;
308     /**
309      * Deprecated version of the findMethod method.
310      * @deprecated
311      */

312     MethodInstance findMethod(ReferenceType container,
313                               String JavaDoc name, List JavaDoc argTypes,
314                               Context c) throws SemanticException;
315
316     /**
317      * Find a constructor. We need to pass the class from which the constructor
318      * is being found because the constructor
319      * we find depends on whether the constructor is accessible from that
320      * class.
321      * @exception SemanticException if the constructor cannot be found or is
322      * inaccessible.
323      */

324     ConstructorInstance findConstructor(ClassType container, List JavaDoc argTypes,
325                                         ClassType currClass) throws SemanticException;
326
327     /**
328      * Deprecated version of the findConstructor method.
329      * @deprecated
330      */

331     ConstructorInstance findConstructor(ClassType container, List JavaDoc argTypes,
332                                         Context c) throws SemanticException;
333
334     /**
335      * Find a member class.
336      * We check if the field is accessible from the class currClass.
337      * @exception SemanticException if the class cannot be found or is
338      * inaccessible.
339      */

340     ClassType findMemberClass(ClassType container, String JavaDoc name, ClassType currClass)
341     throws SemanticException;
342
343     /**
344      * Deprecated version of the findMemberClass method.
345      * @deprecated
346      */

347     ClassType findMemberClass(ClassType container, String JavaDoc name, Context c)
348     throws SemanticException;
349
350     /**
351      * Find a member class.
352      * @exception SemanticException if the class cannot be found or is
353      * inaccessible.
354      */

355     ClassType findMemberClass(ClassType container, String JavaDoc name)
356     throws SemanticException;
357
358     /**
359      * Returns the immediate supertype of type, or null if type has no
360      * supertype.
361      **/

362     Type superType(ReferenceType type);
363
364     /**
365      * Returns an immutable list of all the interface types which type
366      * implements.
367      **/

368     List JavaDoc interfaces(ReferenceType type);
369
370     ////
371
// Functions for method testing.
372
////
373

374     /**
375      * Returns true iff <code>m1</code> throws fewer exceptions than
376      * <code>m2</code>.
377      */

378     boolean throwsSubset(ProcedureInstance m1, ProcedureInstance m2);
379
380     /**
381      * Returns true iff <code>t</code> has the method <code>mi</code>.
382      */

383     boolean hasMethod(ReferenceType t, MethodInstance mi);
384
385     /**
386      * Returns true iff <code>t</code> has a method with name <code>name</code>
387      * either defined in <code>t</code> or inherited into it.
388      */

389     boolean hasMethodNamed(ReferenceType t, String JavaDoc name);
390
391     /**
392      * Returns true iff <code>m1</code> is the same method as <code>m2</code>.
393      */

394     boolean isSameMethod(MethodInstance m1, MethodInstance m2);
395
396     /**
397      * Returns true iff <code>m1</code> is more specific than <code>m2</code>.
398      */

399     boolean moreSpecific(ProcedureInstance m1, ProcedureInstance m2);
400
401     /**
402      * Returns true iff <code>p</code> has exactly the formal arguments
403      * <code>formalTypes</code>.
404      */

405     boolean hasFormals(ProcedureInstance p, List JavaDoc formalTypes);
406
407     ////
408
// Functions which yield particular types.
409
////
410

411     /**
412      * The type of <code>null</code>.
413      */

414     NullType Null();
415
416     /**
417      * <code>void</code>
418      */

419     PrimitiveType Void();
420
421     /**
422      * <code>boolean</code>
423      */

424     PrimitiveType Boolean();
425
426     /**
427      * <code>char</code>
428      */

429     PrimitiveType Char();
430
431     /**
432      * <code>byte</code>
433      */

434     PrimitiveType Byte();
435
436     /**
437      * <code>short</code>
438      */

439     PrimitiveType Short();
440
441     /**
442      * <code>int</code>
443      */

444     PrimitiveType Int();
445
446     /**
447      * <code>long</code>
448      */

449     PrimitiveType Long();
450
451     /**
452      * <code>float</code>
453      */

454     PrimitiveType Float();
455
456     /**
457      * <code>double</code>
458      */

459     PrimitiveType Double();
460
461     /**
462      * <code>java.lang.Object</code>
463      */

464     ClassType Object();
465
466     /**
467      * <code>java.lang.String</code>
468      */

469     ClassType String();
470
471     /**
472      * <code>java.lang.Class</code>
473      */

474     ClassType Class();
475
476     /**
477      * <code>java.lang.Throwable</code>
478      */

479     ClassType Throwable();
480
481     /**
482      * <code>java.lang.Error</code>
483      */

484     ClassType Error();
485
486     /**
487      * <code>java.lang.Exception</code>
488      */

489     ClassType Exception();
490
491     /**
492      * <code>java.lang.RuntimeException</code>
493      */

494     ClassType RuntimeException();
495
496     /**
497      * <code>java.lang.Cloneable</code>
498      */

499     ClassType Cloneable();
500
501     /**
502      * <code>java.io.Serializable</code>
503      */

504     ClassType Serializable();
505
506     /**
507      * <code>java.lang.NullPointerException</code>
508      */

509     ClassType NullPointerException();
510
511     /**
512      * <code>java.lang.ClassCastException</code>
513      */

514     ClassType ClassCastException();
515
516     /**
517      * <code>java.lang.ArrayIndexOutOfBoundsException</code>
518      */

519     ClassType OutOfBoundsException();
520
521     /**
522      * <code>java.lang.ArrayStoreException</code>
523      */

524     ClassType ArrayStoreException();
525
526     /**
527      * <code>java.lang.ArithmeticException</code>
528      */

529     ClassType ArithmeticException();
530
531     /**
532      * Return an array of <code>type</code>
533      */

534     ArrayType arrayOf(Type type);
535
536     /**
537      * Return an array of <code>type</code>
538      */

539     ArrayType arrayOf(Position pos, Type type);
540
541     /**
542      * Return a <code>dims</code>-array of <code>type</code>
543      */

544     ArrayType arrayOf(Type type, int dims);
545
546     /**
547      * Return a <code>dims</code>-array of <code>type</code>
548      */

549     ArrayType arrayOf(Position pos, Type type, int dims);
550
551     /**
552      * Return a package by name.
553      * Fail if the package does not exists.
554      */

555     Package JavaDoc packageForName(String JavaDoc name) throws SemanticException;
556
557     /**
558      * Return a package by name with the given outer package.
559      * Fail if the package does not exists.
560      */

561     Package JavaDoc packageForName(Package JavaDoc prefix, String JavaDoc name) throws SemanticException;
562
563     /**
564      * Return a package by name.
565      */

566     Package JavaDoc createPackage(String JavaDoc name);
567
568     /**
569      * Return a package by name with the given outer package.
570      */

571     Package JavaDoc createPackage(Package JavaDoc prefix, String JavaDoc name);
572
573     /**
574      * Create a new context object for looking up variables, types, etc.
575      */

576     Context createContext();
577
578     /** Get a resolver for looking up a type in a package. */
579     Resolver packageContextResolver(Resolver resolver, Package JavaDoc pkg);
580
581     /** Get a resolver for looking up a type in a class context. */
582     Resolver classContextResolver(ClassType ct);
583
584     /**
585      * The default lazy class initializer.
586      */

587     LazyClassInitializer defaultClassInitializer();
588
589     /**
590      * Create a new empty class.
591      */

592     ParsedClassType createClassType(LazyClassInitializer init);
593
594     /**
595      * Create a new empty class.
596      */

597     ParsedClassType createClassType();
598
599     /**
600      * Create a new empty class.
601      */

602     ParsedClassType createClassType(LazyClassInitializer init, Source fromSource);
603
604     /**
605      * Create a new empty class.
606      */

607     ParsedClassType createClassType(Source fromSource);
608
609     /**
610      * Return the set of objects that should be serialized into the
611      * type information for the given ClassType.
612      * Usually only the clazz itself should get encoded, and references
613      * to other classes should just have their name written out.
614      * If it makes sense for additional types to be fully encoded,
615      * (ie, they're necessary to correctly reconstruct the given clazz,
616      * and the usual class resolvers can't otherwise find them) they
617      * should be returned in the set in addition to clazz.
618      */

619     Set JavaDoc getTypeEncoderRootSet(Type clazz);
620
621     /**
622      * Get the transformed class name of a class.
623      * This utility method returns the "mangled" name of the given class,
624      * whereby all periods ('.') following the toplevel class name
625      * are replaced with dollar signs ('$'). If any of the containing
626      * classes is not a member class or a top level class, then null is
627      * returned.
628      */

629     public String JavaDoc getTransformedClassName(ClassType ct);
630
631     /** Get a place-holder for serializing a type object.
632      * @param o The object to get the place-holder for.
633      * @param roots The root objects for the serialization. Place holders
634      * are not created for these.
635      */

636     Object JavaDoc placeHolder(TypeObject o, java.util.Set JavaDoc roots);
637
638     /** Get a place-holder for serializing a type object.
639      * @param o The object to get the place-holder for.
640      */

641     Object JavaDoc placeHolder(TypeObject o);
642
643     /**
644      * Translate a package.
645      */

646     String JavaDoc translatePackage(Resolver c, Package JavaDoc p);
647
648     /**
649      * Translate a primitive type.
650      */

651     String JavaDoc translatePrimitive(Resolver c, PrimitiveType t);
652
653     /**
654      * Translate an array type.
655      */

656     String JavaDoc translateArray(Resolver c, ArrayType t);
657
658     /**
659      * Translate a top-level class type.
660      */

661     String JavaDoc translateClass(Resolver c, ClassType t);
662
663     /**
664      * Return the boxed version of <code>t</code>.
665      */

666     String JavaDoc wrapperTypeString(PrimitiveType t);
667
668     /**
669      * Return true if <code>mi</code> can be called with name <code>name</code>
670      * and actual parameters of types <code>actualTypes</code>.
671      */

672     boolean methodCallValid(MethodInstance mi, String JavaDoc name, List JavaDoc argTypes);
673
674     /**
675      * Return true if <code>pi</code> can be called with
676      * actual parameters of types <code>actualTypes</code>.
677      */

678     boolean callValid(ProcedureInstance mi, List JavaDoc argTypes);
679
680     /**
681      * Get the list of methods <code>mi</code> (potentially) overrides, in
682      * order from this class (that is, including <code>this</code>) to super
683      * classes.
684      */

685     List JavaDoc overrides(MethodInstance mi);
686
687     /**
688      * Return true if <code>mi</code> can override <code>mj</code>.
689      */

690     boolean canOverride(MethodInstance mi, MethodInstance mj);
691
692     /**
693      * Throw a SemanticException if <code>mi</code> cannot override
694      * <code>mj</code>.
695      */

696     void checkOverride(MethodInstance mi, MethodInstance mj) throws SemanticException;
697
698     /**
699      * Get the list of methods <code>mi</code> implements, in no
700      * specified order.
701      */

702     List JavaDoc implemented(MethodInstance mi);
703     
704     /**
705      * Return the primitive with the given name.
706      */

707     PrimitiveType primitiveForName(String JavaDoc name) throws SemanticException;
708
709     /**
710      * Assert if the flags <code>f</code> are legal method flags.
711      */

712     void checkMethodFlags(Flags f) throws SemanticException;
713
714     /**
715      * Assert if the flags <code>f</code> are legal local variable flags.
716      */

717     void checkLocalFlags(Flags f) throws SemanticException;
718
719     /**
720      * Assert if the flags <code>f</code> are legal field flags.
721      */

722     void checkFieldFlags(Flags f) throws SemanticException;
723
724     /**
725      * Assert if the flags <code>f</code> are legal constructor flags.
726      */

727     void checkConstructorFlags(Flags f) throws SemanticException;
728
729     /**
730      * Assert if the flags <code>f</code> are legal initializer flags.
731      */

732     void checkInitializerFlags(Flags f) throws SemanticException;
733
734     /**
735      * Assert if the flags <code>f</code> are legal top-level class flags.
736      */

737     void checkTopLevelClassFlags(Flags f) throws SemanticException;
738
739     /**
740      * Assert if the flags <code>f</code> are legal member class flags.
741      */

742     void checkMemberClassFlags(Flags f) throws SemanticException;
743
744     /**
745      * Assert if the flags <code>f</code> are legal local class flags.
746      */

747     void checkLocalClassFlags(Flags f) throws SemanticException;
748
749     /**
750      * Assert if the flags <code>f</code> are legal access flags.
751      */

752     void checkAccessFlags(Flags f) throws SemanticException;
753
754     /**
755      * Assert that <code>t</code> has no cycles in the super type+nested class
756      * graph starting at <code>t</code>.
757      */

758     void checkCycles(ReferenceType t) throws SemanticException;
759
760     /**
761      * Assert that <code>ct</code> implements all abstract methods that it
762      * has to; that is, if it is a concrete class, then it must implement all
763      * interfaces and abstract methods that it or its superclasses declare.
764      */

765     public void checkClassConformance(ClassType ct) throws SemanticException;
766
767     /**
768      * Returns <code>t</code>, modified as necessary to make it a legal
769      * static target.
770      */

771     public Type staticTarget(Type t);
772
773     /**
774      * Given the JVM encoding of a set of flags, returns the Flags object
775      * for that encoding.
776      */

777     public Flags flagsForBits(int bits);
778
779     /**
780      * Create a new unique Flags object.
781      * @param name the name of the flag
782      * @param print_after print the new flag after these flags
783      */

784     public Flags createNewFlag(String JavaDoc name, Flags print_after);
785
786     public Flags NoFlags();
787     public Flags Public();
788     public Flags Protected();
789     public Flags Private();
790     public Flags Static();
791     public Flags Final();
792     public Flags Synchronized();
793     public Flags Transient();
794     public Flags Native();
795     public Flags Interface();
796     public Flags Abstract();
797     public Flags Volatile();
798     public Flags StrictFP();
799 }
800
Popular Tags