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