* src/vm/access.c (assert.h): Move after config.h.
[cacao.git] / src / vm / access.c
1 /* vm/access.c - checking access rights
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Edwin Steiner
28
29    Changes:
30
31    $Id: access.c 4435 2006-02-04 23:59:54Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39
40 #include "vm/types.h"
41
42 #include "vm/access.h"
43 #include "vm/builtin.h"
44 #include "vm/class.h"
45
46
47 /****************************************************************************/
48 /* DEBUG HELPERS                                                            */
49 /****************************************************************************/
50
51 #ifdef NDEBUG
52 #define ACCESS_DEBUG
53 #endif
54
55 #ifdef ACCESS_DEBUG
56 #define ACCESS_ASSERT(cond)  assert(cond)
57 #else
58 #define ACCESS_ASSERT(cond)
59 #endif
60
61 /****************************************************************************/
62 /* ACCESS CHECKS                                                            */
63 /****************************************************************************/
64
65 /* access_is_accessible_class **************************************************
66  
67    Check if a class is accessible from another class
68   
69    IN:
70        referer..........the class containing the reference
71        cls..............the result of resolving the reference
72   
73    RETURN VALUE:
74        true.............access permitted
75        false............access denied
76    
77    NOTE:
78        This function performs the checks listed in section 5.4.4.
79            "Access Control" of "The Java(TM) Virtual Machine Specification,
80            Second Edition".
81
82 *******************************************************************************/
83
84 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
85 {
86         ACCESS_ASSERT(referer);
87         ACCESS_ASSERT(cls);
88
89         /* public classes are always accessible */
90         if (cls->flags & ACC_PUBLIC)
91                 return true;
92
93         /* a class in the same package is always accessible */
94         if (SAME_PACKAGE(referer, cls))
95                 return true;
96
97         /* a non-public class in another package is not accessible */
98         return false;
99 }
100
101
102
103
104 /* access_is_accessible_member *************************************************
105  
106    Check if a field or method is accessible from a given class
107   
108    IN:
109        referer..........the class containing the reference
110        declarer.........the class declaring the member
111        memberflags......the access flags of the member
112   
113    RETURN VALUE:
114        true.............access permitted
115        false............access denied
116
117    NOTE:
118        This function only performs the checks listed in section 5.4.4.
119            "Access Control" of "The Java(TM) Virtual Machine Specification,
120            Second Edition".
121
122            In particular a special condition for protected access with is
123            part of the verification process according to the spec is not
124            checked in this function.
125    
126 *******************************************************************************/
127
128 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
129                                                                  s4 memberflags)
130 {
131         ACCESS_ASSERT(referer);
132         ACCESS_ASSERT(declarer);
133         
134         /* public members are accessible */
135         if (memberflags & ACC_PUBLIC)
136                 return true;
137
138         /* {declarer is not an interface} */
139
140         /* private members are only accessible by the class itself */
141         if (memberflags & ACC_PRIVATE)
142                 return (referer == declarer);
143
144         /* {the member is protected or package private} */
145
146         /* protected and package private members are accessible in the same package */
147         if (SAME_PACKAGE(referer,declarer))
148                 return true;
149
150         /* package private members are not accessible outside the package */
151         if (!(memberflags & ACC_PROTECTED))
152                 return false;
153
154         /* {the member is protected and declarer is in another package} */
155
156         /* a necessary condition for access is that referer is a subclass of declarer */
157         ACCESS_ASSERT(referer->linked && declarer->linked);
158         if (builtin_isanysubclass(referer,declarer))
159                 return true;
160
161         return false;
162 }
163
164
165 /*
166  * These are local overrides for various environment variables in Emacs.
167  * Please do not remove this and leave it at the end of the file, where
168  * Emacs will automagically detect them.
169  * ---------------------------------------------------------------------
170  * Local variables:
171  * mode: c
172  * indent-tabs-mode: t
173  * c-basic-offset: 4
174  * tab-width: 4
175  * End:
176  * vim:noexpandtab:sw=4:ts=4:
177  */
178