Skip to content

Add KnownSDKDecls and findLibraryFunction for other modules #37740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2021
Merged
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
10 changes: 10 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ class ASTContext final {
FuncDecl *get##Name() const;
#include "swift/AST/KnownDecls.def"

// Declare accessors for the known declarations.
#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) \
FuncDecl *get##Name() const;
#include "swift/AST/KnownSDKDecls.def"

/// Get the '+' function on two RangeReplaceableCollection.
FuncDecl *getPlusFunctionOnRangeReplaceableCollection() const;

Expand Down Expand Up @@ -591,6 +596,11 @@ class ASTContext final {
// Retrieve the declaration of Swift._stdlib_isOSVersionAtLeast.
FuncDecl *getIsOSVersionAtLeastDecl() const;

/// Look for the declaration with the given name within the
/// passed in module.
void lookupInModule(ModuleDecl *M, StringRef name,
SmallVectorImpl<ValueDecl *> &results) const;

/// Look for the declaration with the given name within the
/// Swift module.
void lookupInSwiftModule(StringRef name,
Expand Down
24 changes: 24 additions & 0 deletions include/swift/AST/KnownSDKDecls.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===--- KnownSDKDecls.def - Compiler decl metaprogramming --*- 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
//
//===----------------------------------------------------------------------===//
//
// This file defines macros used for macro-metaprogramming with compiler-known
// declarations, in modules other than the standard library.
//
//===----------------------------------------------------------------------===//

#ifndef KNOWN_SDK_FUNC_DECL
# define KNOWN_SDK_FUNC_DECL(Module, Name, Id)
#endif


#undef KNOWN_SDK_FUNC_DECL

57 changes: 48 additions & 9 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ struct ASTContext::Implementation {
// Declare cached declarations for each of the known declarations.
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
#include "swift/AST/KnownDecls.def"

// Declare cached declarations for each of the known declarations.
#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) FuncDecl *Get##Name = nullptr;
#include "swift/AST/KnownSDKDecls.def"

/// func <Int, Int) -> Bool
FuncDecl *LessThanIntDecl = nullptr;
Expand Down Expand Up @@ -694,10 +698,10 @@ Identifier ASTContext::getIdentifier(StringRef Str) const {
return Identifier(I->getKeyData());
}

void ASTContext::lookupInSwiftModule(
StringRef name,
SmallVectorImpl<ValueDecl *> &results) const {
ModuleDecl *M = getStdlibModule();
void ASTContext::lookupInModule(
ModuleDecl *M,
StringRef name,
SmallVectorImpl<ValueDecl *> &results) const {
if (!M)
return;

Expand All @@ -706,6 +710,12 @@ void ASTContext::lookupInSwiftModule(
M->lookupValue(identifier, NLKind::UnqualifiedLookup, results);
}

void ASTContext::lookupInSwiftModule(
StringRef name,
SmallVectorImpl<ValueDecl *> &results) const {
lookupInModule(getStdlibModule(), name, results);
}

FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
if (getImpl().PlusFunctionOnRangeReplaceableCollection) {
return getImpl().PlusFunctionOnRangeReplaceableCollection;
Expand Down Expand Up @@ -1028,16 +1038,24 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
return nullptr;
}

/// Find the implementation for the given "intrinsic" library function.
/// Find the implementation for the given "intrinsic" library function,
/// in the passed in module.
static FuncDecl *findLibraryIntrinsic(const ASTContext &ctx,
ModuleDecl *M,
StringRef name) {
SmallVector<ValueDecl *, 1> results;
ctx.lookupInSwiftModule(name, results);
ctx.lookupInModule(M, name, results);
if (results.size() == 1)
return dyn_cast_or_null<FuncDecl>(results.front());
return nullptr;
}

/// Find the implementation for the given "intrinsic" library function.
static FuncDecl *findLibraryIntrinsic(const ASTContext &ctx,
StringRef name) {
return findLibraryIntrinsic(ctx, ctx.getStdlibModule(), name);
}

/// Returns the type of an intrinsic function if it is not generic, otherwise
/// returns nullptr.
static FunctionType *
Expand Down Expand Up @@ -1487,7 +1505,7 @@ ASTContext::associateInfixOperators(PrecedenceGroupDecl *left,
}

// Find library intrinsic function.
static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
StringRef name) {
if (cache) return cache;

Expand All @@ -1496,12 +1514,33 @@ static FuncDecl *findLibraryFunction(const ASTContext &ctx, FuncDecl *&cache,
return cache;
}

#define FUNC_DECL(Name, Id) \
FuncDecl *ASTContext::get##Name() const { \
// Find library intrinsic function in passed in module
static FuncDecl *findLibraryFunction(const ASTContext &ctx,
ModuleDecl *M, FuncDecl *&cache,
StringRef name) {
if (cache) return cache;

// Look for a generic function.
cache = findLibraryIntrinsic(ctx, M, name);
return cache;
}

#define FUNC_DECL(Name, Id) \
FuncDecl *ASTContext::get##Name() const { \
return findLibraryFunction(*this, getImpl().Get##Name, Id); \
}
#include "swift/AST/KnownDecls.def"

#define KNOWN_SDK_FUNC_DECL(Module, Name, Id) \
FuncDecl *ASTContext::get##Name() const { \
if (ModuleDecl *M = getLoadedModule(Id_##Module)) { \
return findLibraryFunction(*this, M, getImpl().Get##Name, Id); \
} else { \
return findLibraryFunction(*this, getImpl().Get##Name, Id); \
} \
}
#include "swift/AST/KnownSDKDecls.def"

bool ASTContext::hasOptionalIntrinsics() const {
return getOptionalDecl() &&
getOptionalSomeDecl() &&
Expand Down
13 changes: 12 additions & 1 deletion lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,23 @@ static FuncDecl *diagnoseMissingIntrinsic(SILGenModule &sgm,

#define FUNC_DECL(NAME, ID) \
FuncDecl *SILGenModule::get##NAME(SILLocation loc) { \
if (auto fn = getASTContext().get##NAME()) \
if (auto fn = getASTContext().get##NAME()) \
return fn; \
return diagnoseMissingIntrinsic(*this, loc, ID); \
}
#include "swift/AST/KnownDecls.def"

#define KNOWN_SDK_FUNC_DECL(MODULE, NAME, ID) \
FuncDecl *SILGenModule::get##NAME(SILLocation loc) { \
if (ModuleDecl *M = getASTContext().getLoadedModule( \
getASTContext().Id_##MODULE)) { \
if (auto fn = getASTContext().get##NAME()) \
return fn; \
} \
return diagnoseMissingIntrinsic(*this, loc, ID); \
}
#include "swift/AST/KnownSDKDecls.def"

ProtocolDecl *SILGenModule::getObjectiveCBridgeable(SILLocation loc) {
if (ObjectiveCBridgeable)
return *ObjectiveCBridgeable;
Expand Down
4 changes: 4 additions & 0 deletions lib/SILGen/SILGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
#define FUNC_DECL(NAME, ID) \
FuncDecl *get##NAME(SILLocation loc);
#include "swift/AST/KnownDecls.def"

#define KNOWN_SDK_FUNC_DECL(MODULE, NAME, ID) \
FuncDecl *get##NAME(SILLocation loc);
#include "swift/AST/KnownSDKDecls.def"

/// Retrieve the _ObjectiveCBridgeable protocol definition.
ProtocolDecl *getObjectiveCBridgeable(SILLocation loc);
Expand Down