Skip to content

Commit

Permalink
Merge pull request #466 from swiftwasm/master
Browse files Browse the repository at this point in the history
[pull] swiftwasm from master
  • Loading branch information
pull[bot] authored Mar 22, 2020
2 parents f2b4932 + eb93cd6 commit 2dcf860
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 68 deletions.
4 changes: 2 additions & 2 deletions docs/CppInteroperabilityManifesto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ public:
// C++ header imported in Swift.
struct Employee {
func mutable_name() -> UnsafeMutablePointer<std.string>
func mutable_company() -> UnsafeMutablePointer<std.string>
}
```

Expand Down Expand Up @@ -1536,7 +1536,7 @@ public moveonly struct File {

func useArrayOfFiles() {
var files: [File] = ...
consumeFile(files[0].move())
consumeFile(files[0].cxxMove())
}
```

Expand Down
19 changes: 0 additions & 19 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,25 +461,6 @@ class SourceFile final : public FileUnit {

virtual bool walk(ASTWalker &walker) override;

/// @{

/// Look up the given operator in this file.
///
/// The file must be name-bound already. If the operator is not found, or if
/// there is an ambiguity, returns null.
///
/// \param isCascading If true, the lookup of this operator may affect
/// downstream files.
InfixOperatorDecl *lookupInfixOperator(Identifier name, bool isCascading,
SourceLoc diagLoc = {});
PrefixOperatorDecl *lookupPrefixOperator(Identifier name, bool isCascading,
SourceLoc diagLoc = {});
PostfixOperatorDecl *lookupPostfixOperator(Identifier name, bool isCascading,
SourceLoc diagLoc = {});
PrecedenceGroupDecl *lookupPrecedenceGroup(Identifier name, bool isCascading,
SourceLoc diagLoc = {});
/// @}

ReferencedNameTracker *getReferencedNameTracker() {
return ReferencedNames ? ReferencedNames.getPointer() : nullptr;
}
Expand Down
35 changes: 13 additions & 22 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,7 @@ void SourceFile::setSyntaxRoot(syntax::SourceFileSyntax &&Root) {

template<typename OP_DECL>
static Optional<OP_DECL *>
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
OperatorMap<OP_DECL *> SourceFile::*OP_MAP);
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name);

template<typename OP_DECL>
using ImportedOperatorsMap = llvm::SmallDenseMap<OP_DECL*, bool, 16>;
Expand Down Expand Up @@ -1066,8 +1065,7 @@ checkOperatorConflicts(const SourceFile &SF, SourceLoc loc,
template <typename OP_DECL>
static Optional<OP_DECL *>
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
Identifier Name, bool includePrivate,
OperatorMap<OP_DECL *> SourceFile::*OP_MAP) {
Identifier Name, bool includePrivate) {
switch (File.getKind()) {
case FileUnitKind::Builtin:
// The Builtin module declares no operators.
Expand All @@ -1084,6 +1082,7 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
assert(SF.ASTStage >= SourceFile::NameBound);

// Look for an operator declaration in the current module.
const auto OP_MAP = OperatorLookup<OP_DECL>::map_ptr;
auto found = (SF.*OP_MAP).find(Name);
if (found != (SF.*OP_MAP).end() && (includePrivate || found->second.getInt()))
return found->second.getPointer();
Expand All @@ -1105,7 +1104,7 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
continue;

Optional<OP_DECL *> maybeOp =
lookupOperatorDeclForName(imported.module.second, Loc, Name, OP_MAP);
lookupOperatorDeclForName<OP_DECL>(imported.module.second, Loc, Name);
if (!maybeOp)
return None;

Expand Down Expand Up @@ -1138,12 +1137,10 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,

template<typename OP_DECL>
static Optional<OP_DECL *>
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
OperatorMap<OP_DECL *> SourceFile::*OP_MAP)
{
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name) {
OP_DECL *result = nullptr;
for (const FileUnit *File : M->getFiles()) {
auto next = lookupOperatorDeclForName(*File, Loc, Name, false, OP_MAP);
auto next = lookupOperatorDeclForName<OP_DECL>(*File, Loc, Name, false);
if (!next.hasValue())
return next;

Expand All @@ -1159,9 +1156,9 @@ lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
template <typename OperatorType>
llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
Evaluator &evaluator, OperatorLookupDescriptor desc) const {
auto result = lookupOperatorDeclForName(*desc.SF, desc.diagLoc, desc.name,
/*includePrivate*/ true,
OperatorLookup<OperatorType>::map_ptr);
auto result = lookupOperatorDeclForName<OperatorType>(*desc.SF, desc.diagLoc,
desc.name,
/*includePrivate*/ true);
if (!result.hasValue())
return nullptr;
if (auto *tracker = desc.SF->getReferencedNameTracker()) {
Expand All @@ -1172,9 +1169,9 @@ llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
}
}
if (!result.getValue()) {
result = lookupOperatorDeclForName(desc.SF->getParentModule(), desc.diagLoc,
desc.name,
OperatorLookup<OperatorType>::map_ptr);
result = lookupOperatorDeclForName<OperatorType>(desc.SF->getParentModule(),
desc.diagLoc,
desc.name);
}
return result.hasValue() ? result.getValue() : nullptr;
}
Expand All @@ -1183,15 +1180,9 @@ llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
#define LOOKUP_OPERATOR(Kind) \
Kind##Decl *ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
auto result = \
lookupOperatorDeclForName(this, loc, name, &SourceFile::Kind##s); \
lookupOperatorDeclForName<Kind##Decl>(this, loc, name); \
return result ? *result : nullptr; \
} \
Kind##Decl *SourceFile::lookup##Kind(Identifier name, bool cascades, \
SourceLoc loc) { \
return evaluateOrDefault( \
getASTContext().evaluator, \
Lookup##Kind##Request{{this, name, cascades, loc}}, nullptr); \
} \
template llvm::Expected<Kind##Decl *> \
LookupOperatorRequest<Kind##Decl>::evaluate(Evaluator &e, \
OperatorLookupDescriptor d) const;
Expand Down
48 changes: 27 additions & 21 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,14 @@ static PrecedenceGroupDecl *
lookupPrecedenceGroup(const PrecedenceGroupDescriptor &descriptor) {
auto *dc = descriptor.dc;
if (auto sf = dc->getParentSourceFile()) {
bool cascading = dc->isCascadingContextForLookup(false);
return sf->lookupPrecedenceGroup(descriptor.ident, cascading,
descriptor.nameLoc);
OperatorLookupDescriptor desc{
sf,
descriptor.ident,
dc->isCascadingContextForLookup(false),
descriptor.nameLoc
};
return evaluateOrDefault(sf->getASTContext().evaluator,
LookupPrecedenceGroupRequest{desc}, nullptr);
} else {
return dc->getParentModule()->lookupPrecedenceGroup(descriptor.ident,
descriptor.nameLoc);
Expand Down Expand Up @@ -1725,26 +1730,27 @@ FunctionOperatorRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
FD->diagnose(diag::operator_in_local_scope);
}

OperatorLookupDescriptor desc{
FD->getDeclContext()->getParentSourceFile(),
operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc()
};
OperatorDecl *op = nullptr;
SourceFile &SF = *FD->getDeclContext()->getParentSourceFile();
if (FD->isUnaryOperator()) {
if (FD->getAttrs().hasAttribute<PrefixAttr>()) {
op = SF.lookupPrefixOperator(operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc());
op = evaluateOrDefault(evaluator,
LookupPrefixOperatorRequest{desc}, nullptr);
} else if (FD->getAttrs().hasAttribute<PostfixAttr>()) {
op = SF.lookupPostfixOperator(operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc());
op = evaluateOrDefault(evaluator,
LookupPostfixOperatorRequest{desc}, nullptr);
} else {
auto prefixOp =
SF.lookupPrefixOperator(operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc());
auto postfixOp =
SF.lookupPostfixOperator(operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc());
auto prefixOp = evaluateOrDefault(evaluator,
LookupPrefixOperatorRequest{desc},
nullptr);
auto postfixOp = evaluateOrDefault(evaluator,
LookupPostfixOperatorRequest{desc},
nullptr);

// If we found both prefix and postfix, or neither prefix nor postfix,
// complain. We can't fix this situation.
Expand Down Expand Up @@ -1790,9 +1796,9 @@ FunctionOperatorRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
static_cast<bool>(postfixOp));
}
} else if (FD->isBinaryOperator()) {
op = SF.lookupInfixOperator(operatorName,
FD->isCascadingContextForLookup(false),
FD->getLoc());
op = evaluateOrDefault(evaluator,
LookupInfixOperatorRequest{desc},
nullptr);
} else {
diags.diagnose(FD, diag::invalid_arg_count_for_operator);
return nullptr;
Expand Down
16 changes: 12 additions & 4 deletions lib/Sema/TypeCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "TypeChecker.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
Expand Down Expand Up @@ -129,12 +130,19 @@ Expr *TypeChecker::substituteInputSugarTypeForResult(ApplyExpr *E) {
static PrecedenceGroupDecl *lookupPrecedenceGroupForOperator(DeclContext *DC,
Identifier name,
SourceLoc loc) {
SourceFile *SF = DC->getParentSourceFile();
bool isCascading = DC->isCascadingContextForLookup(true);
if (auto op = SF->lookupInfixOperator(name, isCascading, loc)) {
OperatorLookupDescriptor desc{
DC->getParentSourceFile(),
name,
DC->isCascadingContextForLookup(true),
loc
};
auto &Ctx = DC->getASTContext();
if (auto op = evaluateOrDefault(Ctx.evaluator,
LookupInfixOperatorRequest{desc},
nullptr)) {
return op->getPrecedenceGroup();
} else {
DC->getASTContext().Diags.diagnose(loc, diag::unknown_binop);
Ctx.Diags.diagnose(loc, diag::unknown_binop);
}
return nullptr;
}
Expand Down
3 changes: 3 additions & 0 deletions test/IRGen/opaque_result_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// RUN: %{python} %utils/chex.py < %s > %t/opaque_result_type.swift
// RUN: %target-swift-frontend -enable-implicit-dynamic -disable-availability-checking -emit-ir %t/opaque_result_type.swift | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-NODEBUG %t/opaque_result_type.swift

// FIXME: Disabled because compilation is crashing in arm64e
// REQUIRES: rdar60734429

public protocol O {
func bar()
}
Expand Down
3 changes: 3 additions & 0 deletions test/Interpreter/opaque_return_type_protocol_ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

// REQUIRES: executable_test

// FIXME: Disabled because compilation is crashing in arm64e
// REQUIRES: rdar60734429

@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
protocol P {
associatedtype AT
Expand Down

0 comments on commit 2dcf860

Please sign in to comment.