1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include "stdafx.h"
#include "Visibility.h"
#include "Named.h"
#include "Type.h"
#include "Package.h"
#include "Engine.h"
namespace storm {
/**
* Utilities.
*/
// Find the first parent of 'at' that is of type T. This includes 'at' itself.
template <class T>
static MAYBE(T *) firstOf(NameLookup *at) {
T *result = as<T>(at);
while (at && !result) {
at = at->parent();
result = as<T>(at);
}
return result;
}
// Fidn the first parent of 'at' that is of type T, excluding 'at'.
template <class T>
static MAYBE(T *) firstParent(NameLookup *at) {
if (at)
return firstOf<T>(at->parent());
else
return null;
}
/**
* Base class.
*/
Visibility::Visibility() {}
Bool Visibility::visible(Named *check, NameLookup *source) {
assert(false, L"Override Visibility::visible!");
return false;
}
/**
* Public.
*/
Public::Public() {}
Bool Public::visible(Named *check, NameLookup *source) {
return true;
}
void Public::toS(StrBuf *to) const {
*to << S("public");
}
/**
* Private within types.
*/
TypePrivate::TypePrivate() {}
Bool TypePrivate::visible(Named *check, NameLookup *source) {
Type *type = firstParent<Type>(check);
return source->hasParent(type);
}
void TypePrivate::toS(StrBuf *to) const {
*to << S("private");
}
/**
* Protected.
*/
TypeProtected::TypeProtected() {}
Bool TypeProtected::visible(Named *check, NameLookup *source) {
Type *type = firstParent<Type>(check);
Type *src = firstOf<Type>(source);
if (!type || !src)
return false;
return src->isA(type);
}
void TypeProtected::toS(StrBuf *to) const {
*to << S("protected");
}
/**
* Private within a package.
*/
PackagePrivate::PackagePrivate() {}
Bool PackagePrivate::visible(Named *check, NameLookup *source) {
Package *package = firstParent<Package>(check);
return source->hasParent(package);
}
void PackagePrivate::toS(StrBuf *to) const {
*to << S("package private");
}
/**
* Private within a file.
*/
FilePrivate::FilePrivate() {}
static SrcPos findPos(NameLookup *source) {
while (source) {
if (LookupPos *n = as<LookupPos>(source))
return n->pos;
source = source->parent();
}
return SrcPos();
}
Bool FilePrivate::visible(Named *check, NameLookup *source) {
SrcPos src = findPos(source);
// If both come from unknown files, they are probably not from the same file.
if (!check->pos.file || !src.file)
return false;
// If they are the same object, we don't need the expensive equal-check.
if (check->pos.file == src.file)
return true;
return *check->pos.file == *src.file;
}
void FilePrivate::toS(StrBuf *to) const {
*to << S("file private");
}
/**
* Object access.
*/
Visibility *STORM_NAME(allPublic, public)(EnginePtr e) {
return e.v.visibility(Engine::vPublic);
}
Visibility *typePrivate(EnginePtr e) {
return e.v.visibility(Engine::vTypePrivate);
}
Visibility *typeProtected(EnginePtr e) {
return e.v.visibility(Engine::vTypeProtected);
}
Visibility *packagePrivate(EnginePtr e) {
return e.v.visibility(Engine::vPackagePrivate);
}
Visibility *filePrivate(EnginePtr e) {
return e.v.visibility(Engine::vFilePrivate);
}
}
|