mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler. This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size. Despite the large number of files and lines affected, this change is NFC.
97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
//===--- AccessScope.h - Swift Access Scope ---------------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_ACCESSSCOPE_H
|
|
#define SWIFT_ACCESSSCOPE_H
|
|
|
|
#include "swift/AST/AttrKind.h"
|
|
#include "swift/AST/DeclContext.h"
|
|
#include "swift/Basic/Debug.h"
|
|
#include "llvm/ADT/PointerIntPair.h"
|
|
|
|
namespace swift {
|
|
|
|
/// The wrapper around the outermost DeclContext from which
|
|
/// a particular declaration can be accessed.
|
|
class AccessScope {
|
|
/// The declaration context (if not public) along with a bit saying
|
|
/// whether this scope is private (or not).
|
|
llvm::PointerIntPair<const DeclContext *, 1, bool> Value;
|
|
public:
|
|
AccessScope(const DeclContext *DC, bool isPrivate = false);
|
|
|
|
static AccessScope getPublic() { return AccessScope(nullptr); }
|
|
|
|
/// Check if private access is allowed. This is a lexical scope check in Swift
|
|
/// 3 mode. In Swift 4 mode, declarations and extensions of the same type will
|
|
/// also allow access.
|
|
static bool allowsPrivateAccess(const DeclContext *useDC, const DeclContext *sourceDC);
|
|
|
|
/// Returns nullptr if access scope is public.
|
|
const DeclContext *getDeclContext() const { return Value.getPointer(); }
|
|
|
|
bool operator==(AccessScope RHS) const { return Value == RHS.Value; }
|
|
bool operator!=(AccessScope RHS) const { return !(*this == RHS); }
|
|
bool hasEqualDeclContextWith(AccessScope RHS) const {
|
|
return getDeclContext() == RHS.getDeclContext();
|
|
}
|
|
|
|
bool isPublic() const { return !Value.getPointer(); }
|
|
bool isPrivate() const { return Value.getInt(); }
|
|
bool isFileScope() const;
|
|
bool isInternal() const;
|
|
|
|
/// Returns true if this is a child scope of the specified other access scope.
|
|
///
|
|
/// \see DeclContext::isChildContextOf
|
|
bool isChildOf(AccessScope AS) const {
|
|
if (!isPublic() && !AS.isPublic())
|
|
return allowsPrivateAccess(getDeclContext(), AS.getDeclContext());
|
|
if (isPublic() && AS.isPublic())
|
|
return false;
|
|
return AS.isPublic();
|
|
}
|
|
|
|
/// Returns the associated access level for diagnostic purposes.
|
|
AccessLevel accessLevelForDiagnostics() const;
|
|
|
|
/// Returns the minimum access level required to access
|
|
/// associated DeclContext for diagnostic purposes.
|
|
AccessLevel requiredAccessForDiagnostics() const {
|
|
return isFileScope()
|
|
? AccessLevel::FilePrivate
|
|
: accessLevelForDiagnostics();
|
|
}
|
|
|
|
/// Returns the narrowest access scope if this and the specified access scope
|
|
/// have common intersection, or None if scopes don't intersect.
|
|
const Optional<AccessScope> intersectWith(AccessScope accessScope) const {
|
|
if (hasEqualDeclContextWith(accessScope)) {
|
|
if (isPrivate())
|
|
return *this;
|
|
return accessScope;
|
|
}
|
|
if (isChildOf(accessScope))
|
|
return *this;
|
|
if (accessScope.isChildOf(*this))
|
|
return accessScope;
|
|
|
|
return None;
|
|
}
|
|
|
|
SWIFT_DEBUG_DUMP;
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif
|