Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions swift/extractor/infra/SwiftTagTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ MAP(swift::Expr, ExprTag)
MAP(swift::ImplicitConversionExpr, ImplicitConversionExprTag)
MAP(swift::LoadExpr, LoadExprTag)
MAP(swift::DestructureTupleExpr, DestructureTupleExprTag)
MAP(swift::UnresolvedTypeConversionExpr, UnresolvedTypeConversionExprTag)
MAP(swift::FunctionConversionExpr, FunctionConversionExprTag)
MAP(swift::CovariantFunctionConversionExpr, CovariantFunctionConversionExprTag)
MAP(swift::CovariantReturnConversionExpr, CovariantReturnConversionExprTag)
Expand Down Expand Up @@ -267,8 +266,7 @@ MAP(swift::TypeRepr, TypeReprTag)
MAP(swift::Type, TypeTag)
MAP(swift::TypeBase, TypeTag)
MAP(swift::ErrorType, ErrorTypeTag)
MAP(swift::UnresolvedType, UnresolvedTypeTag)
MAP(swift::PlaceholderType, void) // appears in ambiguous types but are then transformed to UnresolvedType
MAP(swift::PlaceholderType, void) // appears in ambiguous types but are then transformed to ErrorType
MAP(swift::BuiltinType, BuiltinTypeTag)
MAP(swift::AnyBuiltinIntegerType, AnyBuiltinIntegerTypeTag)
MAP(swift::BuiltinIntegerType, BuiltinIntegerTypeTag)
Expand All @@ -285,7 +283,8 @@ MAP(swift::TypeBase, TypeTag)
MAP(swift::BuiltinVectorType, BuiltinVectorTypeTag)
MAP(swift::BuiltinPackIndexType, void) // SIL type, cannot really appear in the frontend run
MAP(swift::BuiltinNonDefaultDistributedActorStorageType, void) // Does not appear in AST/SIL, only used during IRGen
MAP(swift::BuiltinFixedArrayType, BuiltinFixedArrayTypeTag)
MAP(swift::BuiltinGenericType, BuiltinGenericTypeTag)
MAP(swift::BuiltinFixedArrayType, BuiltinFixedArrayTypeTag)
MAP(swift::BuiltinUnboundGenericType, void) // Only used during type resolution
MAP(swift::BuiltinImplicitActorType, void) // SIL type
MAP(swift::TupleType, TupleTypeTag)
Expand Down
59 changes: 41 additions & 18 deletions swift/extractor/mangler/SwiftMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ std::string_view getTypeKindStr(const swift::TypeBase* type) {

} // namespace

std::unordered_map<const swift::Decl*, SwiftMangler::ExtensionIndex>
SwiftMangler::preloadedExtensionIndexes;
std::unordered_map<const swift::Decl*, SwiftMangler::ExtensionOrFilePrivateValueIndex>
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes;

SwiftMangledName SwiftMangler::initMangled(const swift::TypeBase* type) {
return {getTypeKindStr(type), '_'};
Expand Down Expand Up @@ -75,6 +75,11 @@ SwiftMangledName SwiftMangler::visitValueDecl(const swift::ValueDecl* decl, bool
if (decl->isStatic()) {
ret << "|static";
}
if (decl->getFormalAccess() == swift::AccessLevel::FilePrivate) {
auto parent = getParent(decl);
auto index = getExtensionofFilePrivateValueIndex(decl, parent);
ret << "|fileprivate" << index.index;
}
return ret;
}

Expand Down Expand Up @@ -105,17 +110,18 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de

auto parent = getParent(decl);
auto target = decl->getExtendedType();
auto index = getExtensionIndex(decl, parent);
auto index = getExtensionofFilePrivateValueIndex(decl, parent);
return initMangled(decl) << fetch(target) << index.index
<< (index.kind == ExtensionKind::clang ? "_clang" : "");
}

SwiftMangler::ExtensionIndex SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl,
const swift::Decl* parent) {
// to avoid iterating multiple times on the parent of multiple extensions, we preload extension
// indexes once for each encountered parent into the `preloadedExtensionIndexes` mapping.
if (auto found = SwiftMangler::preloadedExtensionIndexes.find(decl);
found != SwiftMangler::preloadedExtensionIndexes.end()) {
SwiftMangler::ExtensionOrFilePrivateValueIndex SwiftMangler::getExtensionofFilePrivateValueIndex(
const swift::Decl* decl,
const swift::Decl* parent) {
// to avoid iterating multiple times on the parent, we preload the indexes once for each
// encountered parent.
if (auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end()) {
return found->second;
}
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
Expand All @@ -131,18 +137,25 @@ SwiftMangler::ExtensionIndex SwiftMangler::getExtensionIndex(const swift::Extens
// TODO use a generic logging handle for Swift entities here, once it's available
CODEQL_ASSERT(false, "non-local context must be module or iterable decl context");
}
auto found = SwiftMangler::preloadedExtensionIndexes.find(decl);
auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
// TODO use a generic logging handle for Swift entities here, once it's available
CODEQL_ASSERT(found != SwiftMangler::preloadedExtensionIndexes.end(),
"extension not found within parent");
CODEQL_ASSERT(found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end(),
"declaration not found within parent");
return found->second;
}

void SwiftMangler::indexExtensions(llvm::ArrayRef<swift::Decl*> siblings) {
auto index = 0u;
for (auto sibling : siblings) {
if (sibling->getKind() == swift::DeclKind::Extension) {
SwiftMangler::preloadedExtensionIndexes.try_emplace(sibling, ExtensionKind::swift, index);
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
sibling, ExtensionKind::swift, index);
index++;
} else if (swift::isa<swift::ValueDecl>(sibling) &&
swift::dyn_cast<swift::ValueDecl>(sibling)->getFormalAccess() ==
swift::AccessLevel::FilePrivate) {
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
sibling, ExtensionKind::swift, index);
index++;
}
}
Expand All @@ -161,7 +174,8 @@ void SwiftMangler::indexClangExtensions(const clang::Module* clangModule,
swiftSubmodule->getTopLevelDecls(children);
for (const auto child : children) {
if (child->getKind() == swift::DeclKind::Extension) {
SwiftMangler::preloadedExtensionIndexes.try_emplace(child, ExtensionKind::clang, index);
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
child, ExtensionKind::clang, index);
index++;
}
}
Expand Down Expand Up @@ -202,6 +216,14 @@ SwiftMangledName SwiftMangler::visitBuiltinType(const swift::BuiltinType* type)
return initMangled(type) << type->getTypeName(buffer, /* prependBuiltinNamespace= */ false);
}

SwiftMangledName SwiftMangler::visitBuiltinFixedArrayType(
const swift::BuiltinFixedArrayType* type) {
auto ret = visitBuiltinType(type);
ret << fetch(type->getSize());
ret << fetch(type->getElementType());
return ret;
}

SwiftMangledName SwiftMangler::visitAnyGenericType(const swift::AnyGenericType* type) {
auto ret = initMangled(type);
auto decl = type->getDecl();
Expand Down Expand Up @@ -240,9 +262,6 @@ SwiftMangledName SwiftMangler::visitAnyFunctionType(const swift::AnyFunctionType
if (flags.isNonEphemeral()) {
ret << "_nonephermeral";
}
if (flags.isIsolated()) {
ret << "_isolated";
}
if (flags.isSending()) {
ret << "_sending";
}
Expand Down Expand Up @@ -309,9 +328,13 @@ SwiftMangledName SwiftMangler::visitAnyFunctionType(const swift::AnyFunctionType
if (type->hasGlobalActor()) {
ret << "_actor" << fetch(type->getGlobalActor());
}
if (type->getIsolation().isErased()) {
const auto& isolation = type->getIsolation();
if (isolation.isErased()) {
ret << "_isolated";
}
if (isolation.isNonIsolatedCaller()) {
ret << "_nonisolatednonsending";
}
// TODO: see if this needs to be used in identifying types, if not it needs to be removed from
// type printing in the Swift compiler code
assert(type->hasExtInfo() && "type must have ext info");
Expand Down
9 changes: 6 additions & 3 deletions swift/extractor/mangler/SwiftMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
SwiftMangledName visitModuleType(const swift::ModuleType* type);
SwiftMangledName visitTupleType(const swift::TupleType* type);
SwiftMangledName visitBuiltinType(const swift::BuiltinType* type);
SwiftMangledName visitBuiltinFixedArrayType(const swift::BuiltinFixedArrayType* type);
SwiftMangledName visitAnyGenericType(const swift::AnyGenericType* type);

// shouldn't be required, but they forgot to link `NominalType` to its direct superclass
Expand Down Expand Up @@ -111,12 +112,13 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
clang,
};

struct ExtensionIndex {
struct ExtensionOrFilePrivateValueIndex {
const ExtensionKind kind : 1;
const uint32_t index : 31;
};

static std::unordered_map<const swift::Decl*, ExtensionIndex> preloadedExtensionIndexes;
static std::unordered_map<const swift::Decl*, ExtensionOrFilePrivateValueIndex>
preloadedExtensionOrFilePrivateValueIndexes;

virtual SwiftMangledName fetch(const swift::Decl* decl) = 0;
virtual SwiftMangledName fetch(const swift::TypeBase* type) = 0;
Expand All @@ -125,7 +127,8 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
void indexExtensions(llvm::ArrayRef<swift::Decl*> siblings);
void indexClangExtensions(const clang::Module* clangModule,
swift::ClangModuleLoader* moduleLoader);
ExtensionIndex getExtensionIndex(const swift::ExtensionDecl* decl, const swift::Decl* parent);
ExtensionOrFilePrivateValueIndex getExtensionofFilePrivateValueIndex(const swift::Decl* decl,
const swift::Decl* parent);
static SwiftMangledName initMangled(const swift::TypeBase* type);
SwiftMangledName initMangled(const swift::Decl* decl);
SwiftMangledName visitTypeDiscriminatedValueDecl(const swift::ValueDecl* decl);
Expand Down
2 changes: 1 addition & 1 deletion swift/extractor/translators/StmtTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ codeql::CaseStmt StmtTranslator::translateCaseStmt(const swift::CaseStmt& stmt)
auto entry = dispatcher.createEntry(stmt);
entry.body = dispatcher.fetchLabel(stmt.getBody());
entry.labels = dispatcher.fetchRepeatedLabels(stmt.getCaseLabelItems());
entry.variables = dispatcher.fetchRepeatedLabels(stmt.getCaseBodyVariablesOrEmptyArray());
entry.variables = dispatcher.fetchRepeatedLabels(stmt.getCaseBodyVariables());
return entry;
}

Expand Down
12 changes: 8 additions & 4 deletions swift/extractor/translators/TypeTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ codeql::BuiltinIntegerType TypeTranslator::translateBuiltinIntegerType(
return entry;
}

codeql::BuiltinFixedArrayType TypeTranslator::translateBuiltinFixedArrayType(
const swift::BuiltinFixedArrayType& type) {
// currently the translate dispatching mechanism does not go up more than one step in the
// hierarchy so we need to put this explicitly here, as BuiltinFixedArrayType derives from
// BuiltinGenericType which then derives from BuiltinType
return translateBuiltinType(type);
}

codeql::ExistentialArchetypeType TypeTranslator::translateExistentialArchetypeType(
const swift::ExistentialArchetypeType& type) {
auto entry = createTypeEntry(type);
Expand All @@ -258,10 +266,6 @@ codeql::ErrorType TypeTranslator::translateErrorType(const swift::ErrorType& typ
return createTypeEntry(type);
}

codeql::UnresolvedType TypeTranslator::translateUnresolvedType(const swift::UnresolvedType& type) {
return createTypeEntry(type);
}

codeql::ParameterizedProtocolType TypeTranslator::translateParameterizedProtocolType(
const swift::ParameterizedProtocolType& type) {
auto entry = createTypeEntry(type);
Expand Down
3 changes: 2 additions & 1 deletion swift/extractor/translators/TypeTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ class TypeTranslator : public TypeTranslatorBase<TypeTranslator> {
codeql::BuiltinIntegerLiteralType translateBuiltinIntegerLiteralType(
const swift::BuiltinIntegerLiteralType& type);
codeql::BuiltinIntegerType translateBuiltinIntegerType(const swift::BuiltinIntegerType& type);
codeql::BuiltinFixedArrayType translateBuiltinFixedArrayType(
const swift::BuiltinFixedArrayType& type);
codeql::ExistentialArchetypeType translateExistentialArchetypeType(
const swift::ExistentialArchetypeType& type);
codeql::ModuleType translateModuleType(const swift::ModuleType& type);
codeql::OpaqueTypeArchetypeType translateOpaqueTypeArchetypeType(
const swift::OpaqueTypeArchetypeType& type);
codeql::ErrorType translateErrorType(const swift::ErrorType& type);
codeql::UnresolvedType translateUnresolvedType(const swift::UnresolvedType& type);
codeql::ParameterizedProtocolType translateParameterizedProtocolType(
const swift::ParameterizedProtocolType& type);
codeql::PackArchetypeType translatePackArchetypeType(const swift::PackArchetypeType& type);
Expand Down
Loading
Loading