| 1 |
// Copyright (c) 1999-2006 by Digital Mars |
|---|
| 2 |
// All Rights Reserved |
|---|
| 3 |
// written by Walter Bright |
|---|
| 4 |
// http://www.digitalmars.com |
|---|
| 5 |
// License for redistribution is by either the Artistic License |
|---|
| 6 |
// in artistic.txt, or the GNU General Public License in gnu.txt. |
|---|
| 7 |
// See the included readme.txt for details. |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
#include <stdio.h> |
|---|
| 11 |
#include <stdlib.h> |
|---|
| 12 |
#include <assert.h> |
|---|
| 13 |
|
|---|
| 14 |
#include "root.h" |
|---|
| 15 |
#include "mem.h" |
|---|
| 16 |
|
|---|
| 17 |
#include "enum.h" |
|---|
| 18 |
#include "aggregate.h" |
|---|
| 19 |
#include "init.h" |
|---|
| 20 |
#include "attrib.h" |
|---|
| 21 |
#include "scope.h" |
|---|
| 22 |
#include "id.h" |
|---|
| 23 |
#include "mtype.h" |
|---|
| 24 |
#include "declaration.h" |
|---|
| 25 |
#include "aggregate.h" |
|---|
| 26 |
#include "expression.h" |
|---|
| 27 |
#include "module.h" |
|---|
| 28 |
|
|---|
| 29 |
#define LOG 0 |
|---|
| 30 |
|
|---|
| 31 |
/* Code to do access checks |
|---|
| 32 |
*/ |
|---|
| 33 |
|
|---|
| 34 |
int hasPackageAccess(Scope *sc, Dsymbol *s); |
|---|
| 35 |
|
|---|
| 36 |
/**************************************** |
|---|
| 37 |
* Return PROT access for Dsymbol smember in this declaration. |
|---|
| 38 |
*/ |
|---|
| 39 |
|
|---|
| 40 |
enum PROT AggregateDeclaration::getAccess(Dsymbol *smember) |
|---|
| 41 |
{ |
|---|
| 42 |
return PROTpublic; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
enum PROT StructDeclaration::getAccess(Dsymbol *smember) |
|---|
| 46 |
{ |
|---|
| 47 |
enum PROT access_ret = PROTnone; |
|---|
| 48 |
|
|---|
| 49 |
#if LOG |
|---|
| 50 |
printf("+StructDeclaration::getAccess(this = '%s', smember = '%s')\n", |
|---|
| 51 |
toChars(), smember->toChars()); |
|---|
| 52 |
#endif |
|---|
| 53 |
if (smember->toParent() == this) |
|---|
| 54 |
{ |
|---|
| 55 |
access_ret = smember->prot(); |
|---|
| 56 |
} |
|---|
| 57 |
else if (smember->isDeclaration()->isStatic()) |
|---|
| 58 |
{ |
|---|
| 59 |
access_ret = smember->prot(); |
|---|
| 60 |
} |
|---|
| 61 |
return access_ret; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
enum PROT ClassDeclaration::getAccess(Dsymbol *smember) |
|---|
| 65 |
{ |
|---|
| 66 |
enum PROT access_ret = PROTnone; |
|---|
| 67 |
|
|---|
| 68 |
#if LOG |
|---|
| 69 |
printf("+ClassDeclaration::getAccess(this = '%s', smember = '%s')\n", |
|---|
| 70 |
toChars(), smember->toChars()); |
|---|
| 71 |
#endif |
|---|
| 72 |
if (smember->toParent() == this) |
|---|
| 73 |
{ |
|---|
| 74 |
access_ret = smember->prot(); |
|---|
| 75 |
} |
|---|
| 76 |
else |
|---|
| 77 |
{ |
|---|
| 78 |
enum PROT access; |
|---|
| 79 |
int i; |
|---|
| 80 |
|
|---|
| 81 |
if (smember->isDeclaration()->isStatic()) |
|---|
| 82 |
{ |
|---|
| 83 |
access_ret = smember->prot(); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
for (i = 0; i < baseclasses.dim; i++) |
|---|
| 87 |
{ BaseClass *b = (BaseClass *)baseclasses.data[i]; |
|---|
| 88 |
|
|---|
| 89 |
access = b->base->getAccess(smember); |
|---|
| 90 |
switch (access) |
|---|
| 91 |
{ |
|---|
| 92 |
case PROTnone: |
|---|
| 93 |
break; |
|---|
| 94 |
|
|---|
| 95 |
case PROTprivate: |
|---|
| 96 |
access = PROTnone; // private members of base class not accessible |
|---|
| 97 |
break; |
|---|
| 98 |
|
|---|
| 99 |
case PROTpackage: |
|---|
| 100 |
case PROTprotected: |
|---|
| 101 |
case PROTpublic: |
|---|
| 102 |
case PROTexport: |
|---|
| 103 |
// If access is to be tightened |
|---|
| 104 |
if (b->protection < access) |
|---|
| 105 |
access = b->protection; |
|---|
| 106 |
|
|---|
| 107 |
// Pick path with loosest access |
|---|
| 108 |
if (access > access_ret) |
|---|
| 109 |
access_ret = access; |
|---|
| 110 |
break; |
|---|
| 111 |
|
|---|
| 112 |
default: |
|---|
| 113 |
assert(0); |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
#if LOG |
|---|
| 118 |
printf("-ClassDeclaration::getAccess(this = '%s', smember = '%s') = %d\n", |
|---|
| 119 |
toChars(), smember->toChars(), access_ret); |
|---|
| 120 |
#endif |
|---|
| 121 |
return access_ret; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
/******************************************************** |
|---|
| 125 |
* Helper function for ClassDeclaration::accessCheck() |
|---|
| 126 |
* Returns: |
|---|
| 127 |
* 0 no access |
|---|
| 128 |
* 1 access |
|---|
| 129 |
*/ |
|---|
| 130 |
|
|---|
| 131 |
static int accessCheckX( |
|---|
| 132 |
Dsymbol *smember, |
|---|
| 133 |
Dsymbol *sfunc, |
|---|
| 134 |
AggregateDeclaration *dthis, |
|---|
| 135 |
AggregateDeclaration *cdscope) |
|---|
| 136 |
{ |
|---|
| 137 |
assert(dthis); |
|---|
| 138 |
|
|---|
| 139 |
#if 0 |
|---|
| 140 |
printf("accessCheckX for %s.%s in function %s() in scope %s\n", |
|---|
| 141 |
dthis->toChars(), smember->toChars(), |
|---|
| 142 |
sfunc ? sfunc->toChars() : "NULL", |
|---|
| 143 |
cdscope ? cdscope->toChars() : "NULL"); |
|---|
| 144 |
#endif |
|---|
| 145 |
if (dthis->hasPrivateAccess(sfunc) || |
|---|
| 146 |
dthis->isFriendOf(cdscope)) |
|---|
| 147 |
{ |
|---|
| 148 |
if (smember->toParent() == dthis) |
|---|
| 149 |
return 1; |
|---|
| 150 |
else |
|---|
| 151 |
{ |
|---|
| 152 |
ClassDeclaration *cdthis = dthis->isClassDeclaration(); |
|---|
| 153 |
if (cdthis) |
|---|
| 154 |
{ |
|---|
| 155 |
for (int i = 0; i < cdthis->baseclasses.dim; i++) |
|---|
| 156 |
{ BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i]; |
|---|
| 157 |
enum PROT access; |
|---|
| 158 |
|
|---|
| 159 |
access = b->base->getAccess(smember); |
|---|
| 160 |
if (access >= PROTprotected || |
|---|
| 161 |
accessCheckX(smember, sfunc, b->base, cdscope) |
|---|
| 162 |
) |
|---|
| 163 |
return 1; |
|---|
| 164 |
|
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
} |
|---|
| 169 |
else |
|---|
| 170 |
{ |
|---|
| 171 |
if (smember->toParent() != dthis) |
|---|
| 172 |
{ |
|---|
| 173 |
ClassDeclaration *cdthis = dthis->isClassDeclaration(); |
|---|
| 174 |
if (cdthis) |
|---|
| 175 |
{ |
|---|
| 176 |
for (int i = 0; i < cdthis->baseclasses.dim; i++) |
|---|
| 177 |
{ BaseClass *b = (BaseClass *)cdthis->baseclasses.data[i]; |
|---|
| 178 |
|
|---|
| 179 |
if (accessCheckX(smember, sfunc, b->base, cdscope)) |
|---|
| 180 |
return 1; |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
return 0; |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
/******************************* |
|---|
| 189 |
* Do access check for member of this class, this class being the |
|---|
| 190 |
* type of the 'this' pointer used to access smember. |
|---|
| 191 |
*/ |
|---|
| 192 |
|
|---|
| 193 |
void AggregateDeclaration::accessCheck(Loc loc, Scope *sc, Dsymbol *smember) |
|---|
| 194 |
{ |
|---|
| 195 |
int result; |
|---|
| 196 |
|
|---|
| 197 |
FuncDeclaration *f = sc->func; |
|---|
| 198 |
AggregateDeclaration *cdscope = sc->getStructClassScope(); |
|---|
| 199 |
enum PROT access; |
|---|
| 200 |
|
|---|
| 201 |
#if LOG |
|---|
| 202 |
printf("AggregateDeclaration::accessCheck() for %s.%s in function %s() in scope %s\n", |
|---|
| 203 |
toChars(), smember->toChars(), |
|---|
| 204 |
f ? f->toChars() : NULL, |
|---|
| 205 |
cdscope ? cdscope->toChars() : NULL); |
|---|
| 206 |
#endif |
|---|
| 207 |
|
|---|
| 208 |
Dsymbol *smemberparent = smember->toParent(); |
|---|
| 209 |
if (!smemberparent || !smemberparent->isAggregateDeclaration()) |
|---|
| 210 |
{ |
|---|
| 211 |
#if LOG |
|---|
| 212 |
printf("not an aggregate member\n"); |
|---|
| 213 |
#endif |
|---|
| 214 |
return; // then it is accessible |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
// BUG: should enable this check |
|---|
| 218 |
//assert(smember->parent->isBaseOf(this, NULL)); |
|---|
| 219 |
|
|---|
| 220 |
if (smemberparent == this) |
|---|
| 221 |
{ enum PROT access = smember->prot(); |
|---|
| 222 |
|
|---|
| 223 |
result = access >= PROTpublic || |
|---|
| 224 |
hasPrivateAccess(f) || |
|---|
| 225 |
isFriendOf(cdscope) || |
|---|
| 226 |
(access == PROTpackage && hasPackageAccess(sc, this)); |
|---|
| 227 |
#if LOG |
|---|
| 228 |
printf("result1 = %d\n", result); |
|---|
| 229 |
#endif |
|---|
| 230 |
} |
|---|
| 231 |
else if ((access = this->getAccess(smember)) >= PROTpublic) |
|---|
| 232 |
{ |
|---|
| 233 |
result = 1; |
|---|
| 234 |
#if LOG |
|---|
| 235 |
printf("result2 = %d\n", result); |
|---|
| 236 |
#endif |
|---|
| 237 |
} |
|---|
| 238 |
else if (access == PROTpackage && hasPackageAccess(sc, this)) |
|---|
| 239 |
{ |
|---|
| 240 |
result = 1; |
|---|
| 241 |
#if LOG |
|---|
| 242 |
printf("result3 = %d\n", result); |
|---|
| 243 |
#endif |
|---|
| 244 |
} |
|---|
| 245 |
else |
|---|
| 246 |
{ |
|---|
| 247 |
result = accessCheckX(smember, f, this, cdscope); |
|---|
| 248 |
#if LOG |
|---|
| 249 |
printf("result4 = %d\n", result); |
|---|
| 250 |
#endif |
|---|
| 251 |
} |
|---|
| 252 |
if (!result) |
|---|
| 253 |
{ |
|---|
| 254 |
error(loc, "member %s is not accessible", smember->toChars()); |
|---|
| 255 |
halt(); |
|---|
| 256 |
} |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
/**************************************** |
|---|
| 260 |
* Determine if this is the same or friend of cd. |
|---|
| 261 |
*/ |
|---|
| 262 |
|
|---|
| 263 |
int AggregateDeclaration::isFriendOf(AggregateDeclaration *cd) |
|---|
| 264 |
{ |
|---|
| 265 |
#if LOG |
|---|
| 266 |
printf("AggregateDeclaration::isFriendOf(this = '%s', cd = '%s')\n", toChars(), cd ? cd->toChars() : "null"); |
|---|
| 267 |
#endif |
|---|
| 268 |
if (this == cd) |
|---|
| 269 |
return 1; |
|---|
| 270 |
|
|---|
| 271 |
// Friends if both are in the same module |
|---|
| 272 |
//if (toParent() == cd->toParent()) |
|---|
| 273 |
if (cd && getModule() == cd->getModule()) |
|---|
| 274 |
{ |
|---|
| 275 |
#if LOG |
|---|
| 276 |
printf("\tin same module\n"); |
|---|
| 277 |
#endif |
|---|
| 278 |
return 1; |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
#if LOG |
|---|
| 282 |
printf("\tnot friend\n"); |
|---|
| 283 |
#endif |
|---|
| 284 |
return 0; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
/**************************************** |
|---|
| 288 |
* Determine if scope sc has package level access to s. |
|---|
| 289 |
*/ |
|---|
| 290 |
|
|---|
| 291 |
int hasPackageAccess(Scope *sc, Dsymbol *s) |
|---|
| 292 |
{ |
|---|
| 293 |
#if LOG |
|---|
| 294 |
printf("hasPackageAccess(s = '%s', sc = '%p')\n", s->toChars(), sc); |
|---|
| 295 |
#endif |
|---|
| 296 |
|
|---|
| 297 |
for (; s; s = s->parent) |
|---|
| 298 |
{ |
|---|
| 299 |
if (s->isPackage() && !s->isModule()) |
|---|
| 300 |
break; |
|---|
| 301 |
} |
|---|
| 302 |
#if LOG |
|---|
| 303 |
if (s) |
|---|
| 304 |
printf("\tthis is in package '%s'\n", s->toChars()); |
|---|
| 305 |
#endif |
|---|
| 306 |
|
|---|
| 307 |
if (s && s == sc->module->parent) |
|---|
| 308 |
{ |
|---|
| 309 |
#if LOG |
|---|
| 310 |
printf("\ts is in same package as sc\n"); |
|---|
| 311 |
#endif |
|---|
| 312 |
return 1; |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
#if LOG |
|---|
| 317 |
printf("\tno package access\n"); |
|---|
| 318 |
#endif |
|---|
| 319 |
return 0; |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
/********************************** |
|---|
| 323 |
* Determine if smember has access to private members of this declaration. |
|---|
| 324 |
*/ |
|---|
| 325 |
|
|---|
| 326 |
int AggregateDeclaration::hasPrivateAccess(Dsymbol *smember) |
|---|
| 327 |
{ |
|---|
| 328 |
if (smember) |
|---|
| 329 |
{ AggregateDeclaration *cd = NULL; |
|---|
| 330 |
Dsymbol *smemberparent = smember->toParent(); |
|---|
| 331 |
if (smemberparent) |
|---|
| 332 |
cd = smemberparent->isAggregateDeclaration(); |
|---|
| 333 |
|
|---|
| 334 |
#if LOG |
|---|
| 335 |
printf("AggregateDeclaration::hasPrivateAccess(class %s, member %s)\n", |
|---|
| 336 |
toChars(), smember->toChars()); |
|---|
| 337 |
#endif |
|---|
| 338 |
|
|---|
| 339 |
if (this == cd) // smember is a member of this class |
|---|
| 340 |
{ |
|---|
| 341 |
#if LOG |
|---|
| 342 |
printf("\tyes 1\n"); |
|---|
| 343 |
#endif |
|---|
| 344 |
return 1; // so we get private access |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
// If both are members of the same module, grant access |
|---|
| 348 |
while (1) |
|---|
| 349 |
{ Dsymbol *sp = smember->toParent(); |
|---|
| 350 |
if (sp->isFuncDeclaration() && smember->isFuncDeclaration()) |
|---|
| 351 |
smember = sp; |
|---|
| 352 |
else |
|---|
| 353 |
break; |
|---|
| 354 |
} |
|---|
| 355 |
if (!cd && toParent() == smember->toParent()) |
|---|
| 356 |
{ |
|---|
| 357 |
#if LOG |
|---|
| 358 |
printf("\tyes 2\n"); |
|---|
| 359 |
#endif |
|---|
| 360 |
return 1; |
|---|
| 361 |
} |
|---|
| 362 |
if (!cd && getModule() == smember->getModule()) |
|---|
| 363 |
{ |
|---|
| 364 |
#if LOG |
|---|
| 365 |
printf("\tyes 3\n"); |
|---|
| 366 |
#endif |
|---|
| 367 |
return 1; |
|---|
| 368 |
} |
|---|
| 369 |
} |
|---|
| 370 |
#if LOG |
|---|
| 371 |
printf("\tno\n"); |
|---|
| 372 |
#endif |
|---|
| 373 |
return 0; |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
/**************************************** |
|---|
| 377 |
* Check access to d for expression e.d |
|---|
| 378 |
*/ |
|---|
| 379 |
|
|---|
| 380 |
void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d) |
|---|
| 381 |
{ |
|---|
| 382 |
#if LOG |
|---|
| 383 |
if (e) |
|---|
| 384 |
{ printf("accessCheck(%s . %s)\n", e->toChars(), d->toChars()); |
|---|
| 385 |
printf("\te->type = %s\n", e->type->toChars()); |
|---|
| 386 |
} |
|---|
| 387 |
else |
|---|
| 388 |
{ |
|---|
| 389 |
//printf("accessCheck(%s)\n", d->toChars()); |
|---|
| 390 |
} |
|---|
| 391 |
#endif |
|---|
| 392 |
if (!e) |
|---|
| 393 |
{ |
|---|
| 394 |
if (d->prot() == PROTprivate && d->getModule() != sc->module || |
|---|
| 395 |
d->prot() == PROTpackage && !hasPackageAccess(sc, d)) |
|---|
| 396 |
|
|---|
| 397 |
error(loc, "%s %s.%s is not accessible from %s", |
|---|
| 398 |
d->kind(), d->getModule()->toChars(), d->toChars(), sc->module->toChars()); |
|---|
| 399 |
} |
|---|
| 400 |
else if (e->type->ty == Tclass) |
|---|
| 401 |
{ // Do access check |
|---|
| 402 |
ClassDeclaration *cd; |
|---|
| 403 |
|
|---|
| 404 |
cd = (ClassDeclaration *)(((TypeClass *)e->type)->sym); |
|---|
| 405 |
#if 1 |
|---|
| 406 |
if (e->op == TOKsuper) |
|---|
| 407 |
{ ClassDeclaration *cd2; |
|---|
| 408 |
|
|---|
| 409 |
cd2 = sc->func->toParent()->isClassDeclaration(); |
|---|
| 410 |
if (cd2) |
|---|
| 411 |
cd = cd2; |
|---|
| 412 |
} |
|---|
| 413 |
#endif |
|---|
| 414 |
cd->accessCheck(loc, sc, d); |
|---|
| 415 |
} |
|---|
| 416 |
else if (e->type->ty == Tstruct) |
|---|
| 417 |
{ // Do access check |
|---|
| 418 |
StructDeclaration *cd; |
|---|
| 419 |
|
|---|
| 420 |
cd = (StructDeclaration *)(((TypeStruct *)e->type)->sym); |
|---|
| 421 |
cd->accessCheck(loc, sc, d); |
|---|
| 422 |
} |
|---|
| 423 |
} |
|---|