* don't include "md.h", not needed
[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 2788 2005-06-22 16:08:51Z 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            checkaccess......if true, access rights to the class are checked
126            link.............if true, guarantee that the returned class, if any,
127                             has been linked
128   
129    OUT:
130        *result..........set to result of resolution, or to NULL if
131                         the reference has not been resolved
132                         In the case of an exception, *result is
133                         guaranteed to be set to NULL.
134   
135    RETURN VALUE:
136        true.............everything ok 
137                         (*result may still be NULL for resolveLazy)
138        false............an exception has been thrown
139
140    NOTE:
141        The returned class is *not* guaranteed to be linked!
142            (It is guaranteed to be loaded, though.)
143    
144 *******************************************************************************/
145
146 bool
147 resolve_class_from_name(classinfo* referer,methodinfo *refmethod,
148                                                 utf *classname,
149                                                 resolve_mode_t mode,
150                                                 bool checkaccess,
151                                                 bool link,
152                                                 classinfo **result);
153
154 /* resolve_classref ************************************************************
155  
156    Resolve a symbolic class reference
157   
158    IN:
159        refmethod........the method from which resolution was triggered
160                         (may be NULL if not applicable)
161        ref..............class reference
162        mode.............mode of resolution:
163                             resolveLazy...only resolve if it does not
164                                           require loading classes
165                             resolveEager..load classes if necessary
166            checkaccess......if true, access rights to the class are checked
167            link.............if true, guarantee that the returned class, if any,
168                             has been linked
169   
170    OUT:
171        *result..........set to result of resolution, or to NULL if
172                         the reference has not been resolved
173                         In the case of an exception, *result is
174                         guaranteed to be set to NULL.
175   
176    RETURN VALUE:
177        true.............everything ok 
178                         (*result may still be NULL for resolveLazy)
179        false............an exception has been thrown
180    
181 *******************************************************************************/
182
183 bool
184 resolve_classref(methodinfo *refmethod,
185                                  constant_classref *ref,
186                                  resolve_mode_t mode,
187                                  bool checkaccess,
188                              bool link,
189                                  classinfo **result);
190
191 /* resolve_classref_or_classinfo ***********************************************
192  
193    Resolve a symbolic class reference if necessary
194   
195    IN:
196        refmethod........the method from which resolution was triggered
197                         (may be NULL if not applicable)
198        cls..............class reference or classinfo
199        mode.............mode of resolution:
200                             resolveLazy...only resolve if it does not
201                                           require loading classes
202                             resolveEager..load classes if necessary
203            checkaccess......if true, access rights to the class are checked
204            link.............if true, guarantee that the returned class, if any,
205                             has been linked
206   
207    OUT:
208        *result..........set to result of resolution, or to NULL if
209                         the reference has not been resolved
210                         In the case of an exception, *result is
211                         guaranteed to be set to NULL.
212   
213    RETURN VALUE:
214        true.............everything ok 
215                         (*result may still be NULL for resolveLazy)
216        false............an exception has been thrown
217    
218 *******************************************************************************/
219
220 bool
221 resolve_classref_or_classinfo(methodinfo *refmethod,
222                                                           classref_or_classinfo cls,
223                                                           resolve_mode_t mode,
224                                                           bool checkaccess,
225                                                           bool link,
226                                                           classinfo **result);
227
228 /* resolve_class_from_typedesc *************************************************
229  
230    Return a classinfo * for the given type descriptor
231   
232    IN:
233        d................type descriptor
234            checkaccess......if true, access rights to the class are checked
235            link.............if true, guarantee that the returned class, if any,
236                             has been linked
237    OUT:
238        *result..........set to result of resolution, or to NULL if
239                         the reference has not been resolved
240                         In the case of an exception, *result is
241                         guaranteed to be set to NULL.
242   
243    RETURN VALUE:
244        true.............everything ok 
245        false............an exception has been thrown
246
247    NOTE:
248        This function always resolved eagerly.
249    
250 *******************************************************************************/
251
252 bool resolve_class_from_typedesc(typedesc *d,bool checkaccess,bool link,classinfo **result);
253
254 /* resolve_class ***************************************************************
255  
256    Resolve an unresolved class reference. The class is also linked.
257   
258    IN:
259        ref..............struct containing the reference
260        mode.............mode of resolution:
261                             resolveLazy...only resolve if it does not
262                                           require loading classes
263                             resolveEager..load classes if necessary
264            checkaccess......if true, access rights to the class are checked
265    
266    OUT:
267        *result..........set to the result of resolution, or to NULL if
268                         the reference has not been resolved
269                         In the case of an exception, *result is
270                         guaranteed to be set to NULL.
271   
272    RETURN VALUE:
273        true.............everything ok 
274                         (*result may still be NULL for resolveLazy)
275        false............an exception has been thrown
276    
277 *******************************************************************************/
278
279 bool
280 resolve_class(unresolved_class *ref,
281                           resolve_mode_t mode,
282                           bool checkaccess,
283                           classinfo **result);
284
285 /* resolve_field ***************************************************************
286  
287    Resolve an unresolved field reference
288   
289    IN:
290        ref..............struct containing the reference
291        mode.............mode of resolution:
292                             resolveLazy...only resolve if it does not
293                                           require loading classes
294                             resolveEager..load classes if necessary
295   
296    OUT:
297        *result..........set to the result of resolution, or to NULL if
298                         the reference has not been resolved
299                         In the case of an exception, *result is
300                         guaranteed to be set to NULL.
301   
302    RETURN VALUE:
303        true.............everything ok 
304                         (*result may still be NULL for resolveLazy)
305        false............an exception has been thrown
306    
307 *******************************************************************************/
308
309 bool
310 resolve_field(unresolved_field *ref,
311                           resolve_mode_t mode,
312                           fieldinfo **result);
313
314 /* resolve_method **************************************************************
315  
316    Resolve an unresolved method reference
317   
318    IN:
319        ref..............struct containing the reference
320        mode.............mode of resolution:
321                             resolveLazy...only resolve if it does not
322                                           require loading classes
323                             resolveEager..load classes if necessary
324   
325    OUT:
326        *result..........set to the result of resolution, or to NULL if
327                         the reference has not been resolved
328                         In the case of an exception, *result is
329                         guaranteed to be set to NULL.
330   
331    RETURN VALUE:
332        true.............everything ok 
333                         (*result may still be NULL for resolveLazy)
334        false............an exception has been thrown
335    
336 *******************************************************************************/
337
338 bool
339 resolve_method(unresolved_method *ref,
340                           resolve_mode_t mode,
341                            methodinfo **result);
342
343 /* resolve_and_check_subtype_set ***********************************************
344  
345    Resolve the references in the given set and test subtype relationships
346   
347    IN:
348        referer..........the class containing the references
349        refmethod........the method triggering the resolution
350        ref..............a set of class/interface references
351                         (may be empty)
352        type.............the type to test against the set
353        reversed.........if true, test if type is a subtype of
354                         the set members, instead of the other
355                         way round
356        mode.............mode of resolution:
357                             resolveLazy...only resolve if it does not
358                                           require loading classes
359                             resolveEager..load classes if necessary
360        error............which type of exception to throw if
361                         the test fails. May be:
362                             resolveLinkageError, or
363                             resolveIllegalAccessError
364
365    OUT:
366        *checked.........set to true if all checks were performed,
367                             otherwise set to false
368                             (This is guaranteed to be true if mode was
369                                                 resolveEager and no exception occured.)
370                                                 If checked == NULL, this parameter is not used.
371   
372    RETURN VALUE:
373        true.............the check succeeded
374        false............the check failed. An exception has been
375                         thrown.
376    
377    NOTE:
378        The references in the set are resolved first, so any
379        exception which may occurr during resolution may
380        be thrown by this function.
381    
382 *******************************************************************************/
383
384 bool
385 resolve_and_check_subtype_set(classinfo *referer,methodinfo *refmethod,
386                                                           unresolved_subtype_set *ref,
387                                                           classref_or_classinfo type,
388                                                           bool reversed,
389                                                           resolve_mode_t mode,
390                                                           resolve_err_t error,
391                                                           bool *checked);
392
393 /* create_unresolved_class *****************************************************
394  
395    Create an unresolved_class struct for the given class reference
396   
397    IN:
398            refmethod........the method triggering the resolution (if any)
399            classref.........the class reference
400            valuetype........value type to check against the resolved class
401                                                 may be NULL, if no typeinfo is available
402
403    RETURN VALUE:
404        a pointer to a new unresolved_class struct, or
405            NULL if an exception has been thrown
406
407 *******************************************************************************/
408
409 unresolved_class *
410 create_unresolved_class(methodinfo *refmethod,
411                                                 constant_classref *classref,
412                                                 typeinfo *valuetype);
413
414 /* create_unresolved_field *****************************************************
415  
416    Create an unresolved_field struct for the given field access instruction
417   
418    IN:
419        referer..........the class containing the reference
420            refmethod........the method triggering the resolution (if any)
421            iptr.............the {GET,PUT}{FIELD,STATIC}{,CONST} instruction
422
423    RETURN VALUE:
424        a pointer to a new unresolved_field struct, or
425            NULL if an exception has been thrown
426
427 *******************************************************************************/
428
429 unresolved_field *
430 create_unresolved_field(classinfo *referer,methodinfo *refmethod,
431                                                 instruction *iptr);
432
433 /* constrain_unresolved_field **************************************************
434  
435    Record subtype constraints for a field access.
436   
437    IN:
438        ref..............the unresolved_field structure of the access
439        referer..........the class containing the reference
440            refmethod........the method triggering the resolution (if any)
441            iptr.............the {GET,PUT}{FIELD,STATIC}{,CONST} instruction
442            stack............the input stack of the instruction
443
444    RETURN VALUE:
445        true.............everything ok
446            false............an exception has been thrown
447
448 *******************************************************************************/
449
450 bool
451 constrain_unresolved_field(unresolved_field *ref,
452                                                    classinfo *referer,methodinfo *refmethod,
453                                                    instruction *iptr,
454                                                    stackelement *stack);
455
456 /* create_unresolved_method ****************************************************
457  
458    Create an unresolved_method struct for the given method invocation
459   
460    IN:
461        referer..........the class containing the reference
462            refmethod........the method triggering the resolution (if any)
463            iptr.............the INVOKE* instruction
464
465    RETURN VALUE:
466        a pointer to a new unresolved_method struct, or
467            NULL if an exception has been thrown
468
469 *******************************************************************************/
470
471 unresolved_method *
472 create_unresolved_method(classinfo *referer,methodinfo *refmethod,
473                                                  instruction *iptr);
474
475 /* constrain_unresolved_method *************************************************
476  
477    Record subtype constraints for the arguments of a method call.
478   
479    IN:
480        ref..............the unresolved_method structure of the call
481        referer..........the class containing the reference
482            refmethod........the method triggering the resolution (if any)
483            iptr.............the INVOKE* instruction
484            stack............the input stack of the instruction
485
486    RETURN VALUE:
487        true.............everything ok
488            false............an exception has been thrown
489
490 *******************************************************************************/
491
492 bool
493 constrain_unresolved_method(unresolved_method *ref,
494                                                         classinfo *referer,methodinfo *refmethod,
495                                                         instruction *iptr,
496                                                         stackelement *stack);
497
498 /* unresolved_class_free *******************************************************
499  
500    Free the memory used by an unresolved_class
501   
502    IN:
503        ref..............the unresolved_class
504
505 *******************************************************************************/
506
507 void unresolved_class_free(unresolved_class *ref);
508
509 /* unresolved_field_free *******************************************************
510  
511    Free the memory used by an unresolved_field
512   
513    IN:
514        ref..............the unresolved_field
515
516 *******************************************************************************/
517
518 void unresolved_field_free(unresolved_field *ref);
519
520 /* unresolved_method_free ******************************************************
521  
522    Free the memory used by an unresolved_method
523   
524    IN:
525        ref..............the unresolved_method
526
527 *******************************************************************************/
528
529 void unresolved_method_free(unresolved_method *ref);
530
531 /* unresolved_class_debug_dump *************************************************
532  
533    Print debug info for unresolved_class to stream
534   
535    IN:
536        ref..............the unresolved_class
537            file.............the stream
538
539 *******************************************************************************/
540
541 void unresolved_class_debug_dump(unresolved_class *ref,FILE *file);
542
543 /* unresolved_field_debug_dump *************************************************
544  
545    Print debug info for unresolved_field to stream
546   
547    IN:
548        ref..............the unresolved_field
549            file.............the stream
550
551 *******************************************************************************/
552
553 void unresolved_field_debug_dump(unresolved_field *ref,FILE *file);
554
555 /* unresolved_method_debug_dump ************************************************
556  
557    Print debug info for unresolved_method to stream
558   
559    IN:
560        ref..............the unresolved_method
561            file.............the stream
562
563 *******************************************************************************/
564
565 void unresolved_method_debug_dump(unresolved_method *ref,FILE *file);
566
567 /* unresolved_subtype_set_debug_dump *******************************************
568  
569    Print debug info for unresolved_subtype_set to stream
570   
571    IN:
572        stset............the unresolved_subtype_set
573            file.............the stream
574
575 *******************************************************************************/
576
577 void unresolved_subtype_set_debug_dump(unresolved_subtype_set *stset,FILE *file);
578         
579 #endif /* _RESOLVE_H */
580
581 /*
582  * These are local overrides for various environment variables in Emacs.
583  * Please do not remove this and leave it at the end of the file, where
584  * Emacs will automagically detect them.
585  * ---------------------------------------------------------------------
586  * Local variables:
587  * mode: c
588  * indent-tabs-mode: t
589  * c-basic-offset: 4
590  * tab-width: 4
591  * End:
592  * vim:noexpandtab:sw=4:ts=4:
593  */
594