| 1 |
/** |
| 2 |
* Enforce visibility contrains such as `public` and `private`. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/attribute.html#visibility_attributes, Visibility Attributes) |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/access.d, _access.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_access.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/access.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.access; |
| 15 |
|
| 16 |
import dmd.aggregate; |
| 17 |
import dmd.astenums; |
| 18 |
import dmd.dclass; |
| 19 |
import dmd.dmodule; |
| 20 |
import dmd.dscope; |
| 21 |
import dmd.dstruct; |
| 22 |
import dmd.dsymbol; |
| 23 |
import dmd.errors; |
| 24 |
import dmd.expression; |
| 25 |
import dmd.funcsem : overloadApply; |
| 26 |
import dmd.location; |
| 27 |
import dmd.tokens; |
| 28 |
|
| 29 |
private enum LOG = false; |
| 30 |
|
| 31 |
|
| 32 |
/******************************* |
| 33 |
* Do access check for member of this class, this class being the |
| 34 |
* type of the 'this' pointer used to access smember. |
| 35 |
* Returns true if the member is not accessible. |
| 36 |
*/ |
| 37 |
bool checkAccess(AggregateDeclaration ad, Loc loc, Scope* sc, Dsymbol smember) |
| 38 |
{ |
| 39 |
static if (LOG) |
| 40 |
{ |
| 41 |
printf("AggregateDeclaration::checkAccess() for %s.%s in function %s() in scope %s\n", ad.toChars(), smember.toChars(), f ? f.toChars() : null, cdscope ? cdscope.toChars() : null); |
| 42 |
} |
| 43 |
|
| 44 |
const p = smember.toParent(); |
| 45 |
if (p && p.isTemplateInstance()) |
| 46 |
{ |
| 47 |
return false; // for backward compatibility |
| 48 |
} |
| 49 |
|
| 50 |
if (!symbolIsVisible(sc, smember)) |
| 51 |
{ |
| 52 |
error(loc, "%s `%s` %s `%s` is not accessible", ad.kind(), ad.toPrettyChars(), smember.kind(), smember.toChars()); |
| 53 |
//printf("smember = %s %s, vis = %d, semanticRun = %d\n", |
| 54 |
// smember.kind(), smember.toPrettyChars(), smember.visible() smember.semanticRun); |
| 55 |
return true; |
| 56 |
} |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
/**************************************** |
| 61 |
* Determine if scope sc has package level access to s. |
| 62 |
*/ |
| 63 |
private bool hasPackageAccess(Scope* sc, Dsymbol s) |
| 64 |
{ |
| 65 |
return hasPackageAccess(sc._module, s); |
| 66 |
} |
| 67 |
|
| 68 |
private bool hasPackageAccess(Module mod, Dsymbol s) |
| 69 |
{ |
| 70 |
static if (LOG) |
| 71 |
{ |
| 72 |
printf("hasPackageAccess(s = '%s', mod = '%s', s.visibility.pkg = '%s')\n", s.toChars(), mod.toChars(), s.visible().pkg ? s.visible().pkg.toChars() : "NULL"); |
| 73 |
} |
| 74 |
Package pkg = null; |
| 75 |
if (s.visible().pkg) |
| 76 |
pkg = s.visible().pkg; |
| 77 |
else |
| 78 |
{ |
| 79 |
// no explicit package for visibility, inferring most qualified one |
| 80 |
for (; s; s = s.parent) |
| 81 |
{ |
| 82 |
if (auto m = s.isModule()) |
| 83 |
{ |
| 84 |
DsymbolTable dst = Package.resolve(m.md ? m.md.packages : null, null, null); |
| 85 |
assert(dst); |
| 86 |
Dsymbol s2 = dst.lookup(m.ident); |
| 87 |
assert(s2); |
| 88 |
Package p = s2.isPackage(); |
| 89 |
if (p && p.isPackageMod()) |
| 90 |
{ |
| 91 |
pkg = p; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
else if ((pkg = s.isPackage()) !is null) |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
static if (LOG) |
| 100 |
{ |
| 101 |
if (pkg) |
| 102 |
printf("\tsymbol access binds to package '%s'\n", pkg.toChars()); |
| 103 |
} |
| 104 |
if (pkg) |
| 105 |
{ |
| 106 |
if (pkg == mod.parent) |
| 107 |
{ |
| 108 |
static if (LOG) |
| 109 |
{ |
| 110 |
printf("\tsc is in permitted package for s\n"); |
| 111 |
} |
| 112 |
return true; |
| 113 |
} |
| 114 |
if (pkg.isPackageMod() == mod) |
| 115 |
{ |
| 116 |
static if (LOG) |
| 117 |
{ |
| 118 |
printf("\ts is in same package.d module as sc\n"); |
| 119 |
} |
| 120 |
return true; |
| 121 |
} |
| 122 |
Dsymbol ancestor = mod.parent; |
| 123 |
for (; ancestor; ancestor = ancestor.parent) |
| 124 |
{ |
| 125 |
if (ancestor == pkg) |
| 126 |
{ |
| 127 |
static if (LOG) |
| 128 |
{ |
| 129 |
printf("\tsc is in permitted ancestor package for s\n"); |
| 130 |
} |
| 131 |
return true; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
static if (LOG) |
| 136 |
{ |
| 137 |
printf("\tno package access\n"); |
| 138 |
} |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
/**************************************** |
| 143 |
* Determine if scope sc has protected level access to cd. |
| 144 |
*/ |
| 145 |
private bool hasProtectedAccess(Scope* sc, Dsymbol s) |
| 146 |
{ |
| 147 |
if (auto cd = s.isClassMember()) // also includes interfaces |
| 148 |
{ |
| 149 |
for (auto scx = sc; scx; scx = scx.enclosing) |
| 150 |
{ |
| 151 |
if (!scx.scopesym) |
| 152 |
continue; |
| 153 |
auto cd2 = scx.scopesym.isClassDeclaration(); |
| 154 |
if (cd2 && cd.isBaseOf(cd2, null)) |
| 155 |
return true; |
| 156 |
} |
| 157 |
} |
| 158 |
return sc._module == s.getAccessModule(); |
| 159 |
} |
| 160 |
|
| 161 |
/**************************************** |
| 162 |
* Check access to d for expression e.d |
| 163 |
* Returns true if the declaration is not accessible. |
| 164 |
*/ |
| 165 |
bool checkAccess(Loc loc, Scope* sc, Expression e, Dsymbol d) |
| 166 |
{ |
| 167 |
if (sc.noAccessCheck) |
| 168 |
return false; |
| 169 |
static if (LOG) |
| 170 |
{ |
| 171 |
if (e) |
| 172 |
{ |
| 173 |
printf("checkAccess(%s . %s)\n", e.toChars(), d.toChars()); |
| 174 |
printf("\te.type = %s\n", e.type.toChars()); |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
printf("checkAccess(%s)\n", d.toPrettyChars()); |
| 179 |
} |
| 180 |
} |
| 181 |
if (d.isUnitTestDeclaration()) |
| 182 |
{ |
| 183 |
// Unittests are always accessible. |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
if (!e) |
| 188 |
return false; |
| 189 |
|
| 190 |
if (auto tc = e.type.isTypeClass()) |
| 191 |
{ |
| 192 |
// Do access check |
| 193 |
ClassDeclaration cd = tc.sym; |
| 194 |
if (e.op == EXP.super_) |
| 195 |
{ |
| 196 |
if (ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration()) |
| 197 |
cd = cd2; |
| 198 |
} |
| 199 |
return checkAccess(cd, loc, sc, d); |
| 200 |
} |
| 201 |
else if (auto ts = e.type.isTypeStruct()) |
| 202 |
{ |
| 203 |
// Do access check |
| 204 |
StructDeclaration cd = ts.sym; |
| 205 |
return checkAccess(cd, loc, sc, d); |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/**************************************** |
| 211 |
* Check access to package/module `p` from scope `sc`. |
| 212 |
* |
| 213 |
* Params: |
| 214 |
* sc = scope from which to access to a fully qualified package name |
| 215 |
* p = the package/module to check access for |
| 216 |
* Returns: true if the package is not accessible. |
| 217 |
* |
| 218 |
* Because a global symbol table tree is used for imported packages/modules, |
| 219 |
* access to them needs to be checked based on the imports in the scope chain |
| 220 |
* (see https://issues.dlang.org/show_bug.cgi?id=313). |
| 221 |
* |
| 222 |
*/ |
| 223 |
bool checkAccess(Scope* sc, Package p) |
| 224 |
{ |
| 225 |
if (sc._module == p) |
| 226 |
return false; |
| 227 |
for (; sc; sc = sc.enclosing) |
| 228 |
{ |
| 229 |
if (sc.scopesym && sc.scopesym.isPackageAccessible(p, Visibility(Visibility.Kind.private_))) |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Check whether symbols `s` is visible in `mod`. |
| 238 |
* |
| 239 |
* Params: |
| 240 |
* mod = lookup origin |
| 241 |
* s = symbol to check for visibility |
| 242 |
* Returns: true if s is visible in mod |
| 243 |
*/ |
| 244 |
bool symbolIsVisible(Module mod, Dsymbol s) |
| 245 |
{ |
| 246 |
// should sort overloads by ascending visibility instead of iterating here |
| 247 |
s = mostVisibleOverload(s); |
| 248 |
final switch (s.visible().kind) |
| 249 |
{ |
| 250 |
case Visibility.Kind.undefined: return true; |
| 251 |
case Visibility.Kind.none: return false; // no access |
| 252 |
case Visibility.Kind.private_: return s.getAccessModule() == mod; |
| 253 |
case Visibility.Kind.package_: return s.getAccessModule() == mod || hasPackageAccess(mod, s); |
| 254 |
case Visibility.Kind.protected_: return s.getAccessModule() == mod; |
| 255 |
case Visibility.Kind.public_, Visibility.Kind.export_: return true; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Same as above, but determines the lookup module from symbols `origin`. |
| 261 |
*/ |
| 262 |
bool symbolIsVisible(Dsymbol origin, Dsymbol s) |
| 263 |
{ |
| 264 |
return symbolIsVisible(origin.getAccessModule(), s); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Same as above but also checks for protected symbols visible from scope `sc`. |
| 269 |
* Used for qualified name lookup. |
| 270 |
* |
| 271 |
* Params: |
| 272 |
* sc = lookup scope |
| 273 |
* s = symbol to check for visibility |
| 274 |
* Returns: true if s is visible by origin |
| 275 |
*/ |
| 276 |
bool symbolIsVisible(Scope* sc, Dsymbol s) |
| 277 |
{ |
| 278 |
s = mostVisibleOverload(s); |
| 279 |
return checkSymbolAccess(sc, s); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Check if a symbol is visible from a given scope without taking |
| 284 |
* into account the most visible overload. |
| 285 |
* |
| 286 |
* Params: |
| 287 |
* sc = lookup scope |
| 288 |
* s = symbol to check for visibility |
| 289 |
* Returns: true if s is visible by origin |
| 290 |
*/ |
| 291 |
bool checkSymbolAccess(Scope* sc, Dsymbol s) |
| 292 |
{ |
| 293 |
final switch (s.visible().kind) |
| 294 |
{ |
| 295 |
case Visibility.Kind.undefined: return true; |
| 296 |
case Visibility.Kind.none: return false; // no access |
| 297 |
case Visibility.Kind.private_: return sc._module == s.getAccessModule(); |
| 298 |
case Visibility.Kind.package_: return sc._module == s.getAccessModule() || hasPackageAccess(sc._module, s); |
| 299 |
case Visibility.Kind.protected_: return hasProtectedAccess(sc, s); |
| 300 |
case Visibility.Kind.public_, Visibility.Kind.export_: return true; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Use the most visible overload to check visibility. Later perform an access |
| 306 |
* check on the resolved overload. This function is similar to overloadApply, |
| 307 |
* but doesn't recurse nor resolve aliases because visibility is an |
| 308 |
* attribute of the alias not the aliasee. |
| 309 |
*/ |
| 310 |
public Dsymbol mostVisibleOverload(Dsymbol s, Module mod = null) |
| 311 |
{ |
| 312 |
if (!s.isOverloadable()) |
| 313 |
return s; |
| 314 |
|
| 315 |
Dsymbol next, fstart = s, mostVisible = s; |
| 316 |
for (; s; s = next) |
| 317 |
{ |
| 318 |
// void func() {} |
| 319 |
// private void func(int) {} |
| 320 |
if (auto fd = s.isFuncDeclaration()) |
| 321 |
next = fd.overnext; |
| 322 |
// template temp(T) {} |
| 323 |
// private template temp(T:int) {} |
| 324 |
else if (auto td = s.isTemplateDeclaration()) |
| 325 |
next = td.overnext; |
| 326 |
// alias common = mod1.func1; |
| 327 |
// alias common = mod2.func2; |
| 328 |
else if (auto fa = s.isFuncAliasDeclaration()) |
| 329 |
next = fa.overnext; |
| 330 |
// alias common = mod1.templ1; |
| 331 |
// alias common = mod2.templ2; |
| 332 |
else if (auto od = s.isOverDeclaration()) |
| 333 |
next = od.overnext; |
| 334 |
// alias name = sym; |
| 335 |
// private void name(int) {} |
| 336 |
else if (auto ad = s.isAliasDeclaration()) |
| 337 |
{ |
| 338 |
assert(ad.isOverloadable || ad.type && ad.type.ty == Terror, |
| 339 |
"Non overloadable Aliasee in overload list"); |
| 340 |
// Yet unresolved aliases store overloads in overnext. |
| 341 |
if (ad.semanticRun < PASS.semanticdone) |
| 342 |
next = ad.overnext; |
| 343 |
else |
| 344 |
{ |
| 345 |
/* This is a bit messy due to the complicated implementation of |
| 346 |
* alias. Aliases aren't overloadable themselves, but if their |
| 347 |
* Aliasee is overloadable they can be converted to an overloadable |
| 348 |
* alias. |
| 349 |
* |
| 350 |
* This is done by replacing the Aliasee w/ FuncAliasDeclaration |
| 351 |
* (for functions) or OverDeclaration (for templates) which are |
| 352 |
* simply overloadable aliases w/ weird names. |
| 353 |
* |
| 354 |
* Usually aliases should not be resolved for visibility checking |
| 355 |
* b/c public aliases to private symbols are public. But for the |
| 356 |
* overloadable alias situation, the Alias (_ad_) has been moved |
| 357 |
* into its own Aliasee, leaving a shell that we peel away here. |
| 358 |
*/ |
| 359 |
auto aliasee = ad.toAlias(); |
| 360 |
if (aliasee.isFuncAliasDeclaration || aliasee.isOverDeclaration) |
| 361 |
next = aliasee; |
| 362 |
else |
| 363 |
{ |
| 364 |
/* A simple alias can be at the end of a function or template overload chain. |
| 365 |
* It can't have further overloads b/c it would have been |
| 366 |
* converted to an overloadable alias. |
| 367 |
*/ |
| 368 |
assert(ad.overnext is null, "Unresolved overload of alias"); |
| 369 |
break; |
| 370 |
} |
| 371 |
} |
| 372 |
// handled by dmd.func.overloadApply for unknown reason |
| 373 |
assert(next !is ad); // should not alias itself |
| 374 |
assert(next !is fstart); // should not alias the overload list itself |
| 375 |
} |
| 376 |
else |
| 377 |
break; |
| 378 |
|
| 379 |
/** |
| 380 |
* Return the "effective" visibility attribute of a symbol when accessed in a module. |
| 381 |
* The effective visibility attribute is the same as the regular visibility attribute, |
| 382 |
* except package() is "private" if the module is outside the package; |
| 383 |
* otherwise, "public". |
| 384 |
*/ |
| 385 |
static Visibility visibilitySeenFromModule(Dsymbol d, Module mod = null) |
| 386 |
{ |
| 387 |
Visibility vis = d.visible(); |
| 388 |
if (mod && vis.kind == Visibility.Kind.package_) |
| 389 |
{ |
| 390 |
return hasPackageAccess(mod, d) ? Visibility(Visibility.Kind.public_) : Visibility(Visibility.Kind.private_); |
| 391 |
} |
| 392 |
return vis; |
| 393 |
} |
| 394 |
|
| 395 |
if (next && |
| 396 |
visibilitySeenFromModule(mostVisible, mod) < visibilitySeenFromModule(next, mod)) |
| 397 |
mostVisible = next; |
| 398 |
} |
| 399 |
return mostVisible; |
| 400 |
} |