360b53c5ef78787101b9c92f3ba838d9143d736b
[cacao.git] / src / vm / resolve.h
1 /* vm/resolve.h - resolving classes/interfaces/fields/methods
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Edwin Steiner
28
29    Changes:
30
31    $Id: resolve.h 2215 2005-04-04 14:59:12Z edwin $
32
33 */
34
35
36 #ifndef _RESOLVE_H
37 #define _RESOLVE_H
38
39 /* forward declarations *******************************************************/
40
41 typedef struct unresolved_class unresolved_class;
42 typedef struct unresolved_field unresolved_field;
43 typedef struct unresolved_method unresolved_method;
44 typedef struct unresolved_subtype_set unresolved_subtype_set;
45
46
47 #include "vm/global.h"
48 #include "vm/references.h"
49 #include "vm/jit/jit.h"
50
51
52 /* constants ******************************************************************/
53
54 #define RESOLVE_STATIC    0x0001
55 #define RESOLVE_PUTFIELD  0x0002
56
57
58 /* enums **********************************************************************/
59
60 typedef enum {
61         resolveLazy,
62         resolveEager
63 } resolve_mode_t;
64
65
66 typedef enum {
67         resolveLinkageError,
68         resolveIllegalAccessError
69 } resolve_err_t;
70
71
72 /* structs ********************************************************************/
73
74 struct unresolved_subtype_set {
75         classref_or_classinfo *subtyperefs;     /* NULL terminated list */
76 };
77
78 struct unresolved_class {
79         constant_classref      *classref;
80         methodinfo                     *referermethod;
81         unresolved_subtype_set  subtypeconstraints;
82 };
83
84 /* XXX unify heads of unresolved_field and unresolved_method? */
85
86 struct unresolved_field {
87         constant_FMIref *fieldref;
88         methodinfo      *referermethod;
89         s4               flags;
90         
91         unresolved_subtype_set  instancetypes;
92         unresolved_subtype_set  valueconstraints;
93 };
94
95 struct unresolved_method {
96         constant_FMIref *methodref;
97         methodinfo      *referermethod;
98         s4               flags;
99         
100         unresolved_subtype_set  instancetypes;
101         unresolved_subtype_set *paramconstraints;
102 };
103
104 #define SUBTYPESET_IS_EMPTY(stset) \
105         ((stset).subtyperefs == NULL)
106
107 #define UNRESOLVED_SUBTYPE_SET_EMTPY(stset) \
108         do { (stset).subtyperefs = NULL; } while(0)
109
110 /* function prototypes ********************************************************/
111
112 /* resolve_class_from_name *****************************************************
113  
114    Resolve a symbolic class reference
115   
116    IN:
117        referer..........the class containing the reference
118        refmethod........the method from which resolution was triggered
119                         (may be NULL if not applicable)
120        classname........class name to resolve
121        mode.............mode of resolution:
122                             resolveLazy...only resolve if it does not
123                                           require loading classes
124                             resolveEager..load classes if necessary
125   
126    OUT:
127        *result..........set to result of resolution, or to NULL if
128                         the reference has not been resolved
129                         In the case of an exception, *result is
130                         guaranteed to be set to NULL.
131   
132    RETURN VALUE:
133        true.............everything ok 
134                         (*result may still be NULL for resolveLazy)
135        false............an exception has been thrown
136
137    NOTE:
138        The returned class is *not* guaranteed to be linked!
139            (It is guaranteed to be loaded, though.)
140    
141 *******************************************************************************/
142
143 bool
144 resolve_class_from_name(classinfo* referer,methodinfo *refmethod,
145                                                 utf *classname,
146                                                 resolve_mode_t mode,
147                                                 classinfo **result);
148
149 /* resolve_classref ************************************************************
150  
151    Resolve a symbolic class reference
152   
153    IN:
154        refmethod........the method from which resolution was triggered
155                         (may be NULL if not applicable)
156        ref..............class reference
157        mode.............mode of resolution:
158                             resolveLazy...only resolve if it does not
159                                           require loading classes
160                             resolveEager..load classes if necessary
161            link.............if true, guarantee that the returned class, if any,
162                             has been linked
163   
164    OUT:
165        *result..........set to result of resolution, or to NULL if
166                         the reference has not been resolved
167                         In the case of an exception, *result is
168                         guaranteed to be set to NULL.
169   
170    RETURN VALUE:
171        true.............everything ok 
172                         (*result may still be NULL for resolveLazy)
173        false............an exception has been thrown
174    
175 *******************************************************************************/
176
177 bool
178 resolve_classref(methodinfo *refmethod,
179                                  constant_classref *ref,
180                                  resolve_mode_t mode,
181                              bool link,
182                                  classinfo **result);
183
184 /* resolve_classref_or_classinfo ***********************************************
185  
186    Resolve a symbolic class reference if necessary
187   
188    IN:
189        refmethod........the method from which resolution was triggered
190                         (may be NULL if not applicable)
191        cls..............class reference or classinfo
192        mode.............mode of resolution:
193                             resolveLazy...only resolve if it does not
194                                           require loading classes
195                             resolveEager..load classes if necessary
196            link.............if true, guarantee that the returned class, if any,
197                             has been linked
198   
199    OUT:
200        *result..........set to result of resolution, or to NULL if
201                         the reference has not been resolved
202                         In the case of an exception, *result is
203                         guaranteed to be set to NULL.
204   
205    RETURN VALUE:
206        true.............everything ok 
207                         (*result may still be NULL for resolveLazy)
208        false............an exception has been thrown
209    
210 *******************************************************************************/
211
212 bool
213 resolve_classref_or_classinfo(methodinfo *refmethod,
214                                                           classref_or_classinfo cls,
215                                                           resolve_mode_t mode,
216                                                           bool link,
217                                                           classinfo **result);
218
219 /* resolve_class_from_typedesc *************************************************
220  
221    Return a classinfo * for the given type descriptor
222   
223    IN:
224        d................type descriptor
225            link.............if true, guarantee that the returned class, if any,
226                             has been linked
227    OUT:
228        *result..........set to result of resolution, or to NULL if
229                         the reference has not been resolved
230                         In the case of an exception, *result is
231                         guaranteed to be set to NULL.
232   
233    RETURN VALUE:
234        true.............everything ok 
235        false............an exception has been thrown
236
237    NOTE:
238        This function always resolved eagerly.
239    
240 *******************************************************************************/
241
242 bool resolve_class_from_typedesc(typedesc *d,bool link,classinfo **result);
243
244 /* resolve_class ***************************************************************
245  
246    Resolve an unresolved class reference
247   
248    IN:
249        ref..............struct containing the reference
250        mode.............mode of resolution:
251                             resolveLazy...only resolve if it does not
252                                           require loading classes
253                             resolveEager..load classes if necessary
254   
255    OUT:
256        *result..........set to the result of resolution, or to NULL if
257                         the reference has not been resolved
258                         In the case of an exception, *result is
259                         guaranteed to be set to NULL.
260   
261    RETURN VALUE:
262        true.............everything ok 
263                         (*result may still be NULL for resolveLazy)
264        false............an exception has been thrown
265    
266 *******************************************************************************/
267
268 bool
269 resolve_class(unresolved_class *ref,
270                           resolve_mode_t mode,
271                           classinfo **result);
272
273 /* resolve_field ***************************************************************
274  
275    Resolve an unresolved field reference
276   
277    IN:
278        ref..............struct containing the reference
279        mode.............mode of resolution:
280                             resolveLazy...only resolve if it does not
281                                           require loading classes
282                             resolveEager..load classes if necessary
283   
284    OUT:
285        *result..........set to the result of resolution, or to NULL if
286                         the reference has not been resolved
287                         In the case of an exception, *result is
288                         guaranteed to be set to NULL.
289   
290    RETURN VALUE:
291        true.............everything ok 
292                         (*result may still be NULL for resolveLazy)
293        false............an exception has been thrown
294    
295 *******************************************************************************/
296
297 bool
298 resolve_field(unresolved_field *ref,
299                           resolve_mode_t mode,
300                           fieldinfo **result);
301
302 /* resolve_method **************************************************************
303  
304    Resolve an unresolved method reference
305   
306    IN:
307        ref..............struct containing the reference
308        mode.............mode of resolution:
309                             resolveLazy...only resolve if it does not
310                                           require loading classes
311                             resolveEager..load classes if necessary
312   
313    OUT:
314        *result..........set to the result of resolution, or to NULL if
315                         the reference has not been resolved
316                         In the case of an exception, *result is
317                         guaranteed to be set to NULL.
318   
319    RETURN VALUE:
320        true.............everything ok 
321                         (*result may still be NULL for resolveLazy)
322        false............an exception has been thrown
323    
324 *******************************************************************************/
325
326 bool
327 resolve_method(unresolved_method *ref,
328                           resolve_mode_t mode,
329                            methodinfo **result);
330
331 /* resolve_and_check_subtype_set ***********************************************
332  
333    Resolve the references in the given set and test subtype relationships
334   
335    IN:
336        referer..........the class containing the references
337        refmethod........the method triggering the resolution
338        ref..............a set of class/interface references
339                         (may be empty)
340        type.............the type to test against the set
341        reversed.........if true, test if type is a subtype of
342                         the set members, instead of the other
343                         way round
344        mode.............mode of resolution:
345                             resolveLazy...only resolve if it does not
346                                           require loading classes
347                             resolveEager..load classes if necessary
348        error............which type of exception to throw if
349                         the test fails. May be:
350                             resolveLinkageError, or
351                             resolveIllegalAccessError
352
353    OUT:
354        *checked.........set to true if all checks were performed,
355                             otherwise set to false
356                             (This is guaranteed to be true if mode was
357                                                 resolveEager and no exception occured.)
358                                                 If checked == NULL, this parameter is not used.
359   
360    RETURN VALUE:
361        true.............the check succeeded
362        false............the check failed. An exception has been
363                         thrown.
364    
365    NOTE:
366        The references in the set are resolved first, so any
367        exception which may occurr during resolution may
368        be thrown by this function.
369    
370 *******************************************************************************/
371
372 bool
373 resolve_and_check_subtype_set(classinfo *referer,methodinfo *refmethod,
374                                                           unresolved_subtype_set *ref,
375                                                           classref_or_classinfo type,
376                                                           bool reversed,
377                                                           resolve_mode_t mode,
378                                                           resolve_err_t error,
379                                                           bool *checked);
380
381 /* create_unresolved_class *****************************************************
382  
383    Create an unresolved_class struct for the given class reference
384   
385    IN:
386            refmethod........the method triggering the resolution (if any)
387            classref.........the class reference
388            valuetype........value type to check against the resolved class
389
390    RETURN VALUE:
391        a pointer to a new unresolved_class struct
392
393 *******************************************************************************/
394
395 unresolved_class *
396 create_unresolved_class(methodinfo *refmethod,
397                                                 constant_classref *classref,
398                                                 typeinfo *valuetype);
399
400 /* create_unresolved_field *****************************************************
401  
402    Create an unresolved_field struct for the given field access instruction
403   
404    IN:
405        referer..........the class containing the reference
406            refmethod........the method triggering the resolution (if any)
407            iptr.............the {GET,PUT}{FIELD,STATIC}{,CONST} instruction
408            stack............the input stack of the instruction
409
410    RETURN VALUE:
411        a pointer to a new unresolved_field struct
412
413 *******************************************************************************/
414
415 unresolved_field *
416 create_unresolved_field(classinfo *referer,methodinfo *refmethod,
417                                                 instruction *iptr,
418                                                 stackelement *stack);
419
420 /* create_unresolved_method ****************************************************
421  
422    Create an unresolved_method struct for the given method invocation
423   
424    IN:
425        referer..........the class containing the reference
426            refmethod........the method triggering the resolution (if any)
427            iptr.............the INVOKE* instruction
428            stack............the input stack of the instruction
429
430    RETURN VALUE:
431        a pointer to a new unresolved_method struct
432
433 *******************************************************************************/
434
435 unresolved_method *
436 create_unresolved_method(classinfo *referer,methodinfo *refmethod,
437                                                  instruction *iptr,
438                                                  stackelement *stack);
439
440 /* unresolved_class_free *******************************************************
441  
442    Free the memory used by an unresolved_class
443   
444    IN:
445        ref..............the unresolved_class
446
447 *******************************************************************************/
448
449 void unresolved_class_free(unresolved_class *ref);
450
451 /* unresolved_field_free *******************************************************
452  
453    Free the memory used by an unresolved_field
454   
455    IN:
456        ref..............the unresolved_field
457
458 *******************************************************************************/
459
460 void unresolved_field_free(unresolved_field *ref);
461
462 /* unresolved_method_free ******************************************************
463  
464    Free the memory used by an unresolved_method
465   
466    IN:
467        ref..............the unresolved_method
468
469 *******************************************************************************/
470
471 void unresolved_method_free(unresolved_method *ref);
472
473 /* unresolved_class_debug_dump *************************************************
474  
475    Print debug info for unresolved_class to stream
476   
477    IN:
478        ref..............the unresolved_class
479            file.............the stream
480
481 *******************************************************************************/
482
483 void unresolved_class_debug_dump(unresolved_class *ref,FILE *file);
484
485 /* unresolved_field_debug_dump *************************************************
486  
487    Print debug info for unresolved_field to stream
488   
489    IN:
490        ref..............the unresolved_field
491            file.............the stream
492
493 *******************************************************************************/
494
495 void unresolved_field_debug_dump(unresolved_field *ref,FILE *file);
496
497 /* unresolved_method_debug_dump ************************************************
498  
499    Print debug info for unresolved_method to stream
500   
501    IN:
502        ref..............the unresolved_method
503            file.............the stream
504
505 *******************************************************************************/
506
507 void unresolved_method_debug_dump(unresolved_method *ref,FILE *file);
508
509 /* unresolved_subtype_set_debug_dump *******************************************
510  
511    Print debug info for unresolved_subtype_set to stream
512   
513    IN:
514        stset............the unresolved_subtype_set
515            file.............the stream
516
517 *******************************************************************************/
518
519 void unresolved_subtype_set_debug_dump(unresolved_subtype_set *stset,FILE *file);
520         
521 #endif /* _RESOLVE_H */
522
523 /*
524  * These are local overrides for various environment variables in Emacs.
525  * Please do not remove this and leave it at the end of the file, where
526  * Emacs will automagically detect them.
527  * ---------------------------------------------------------------------
528  * Local variables:
529  * mode: c
530  * indent-tabs-mode: t
531  * c-basic-offset: 4
532  * tab-width: 4
533  * End:
534  * vim:noexpandtab:sw=4:ts=4:
535  */
536