//===--- CSSimplify.cpp - Constraint Simplification -----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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 implements simplifications of constraints within the constraint
// system.
//
//===----------------------------------------------------------------------===//
#include "CSDiagnostics.h"
#include "TypeCheckConcurrency.h"
#include "TypeCheckEffects.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/PackExpansionMatcher.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/Requirement.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/StringExtras.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Sema/CSFix.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/Compiler.h"
using namespace swift;
using namespace constraints;
MatchCallArgumentListener::~MatchCallArgumentListener() { }
bool MatchCallArgumentListener::extraArgument(unsigned argIdx) { return true; }
std::optional<unsigned>
MatchCallArgumentListener::missingArgument(unsigned paramIdx,
unsigned argInsertIdx) {
return std::nullopt;
}
bool MatchCallArgumentListener::missingLabel(unsigned paramIdx) { return true; }
bool MatchCallArgumentListener::extraneousLabel(unsigned paramIdx) {
return true;
}
bool MatchCallArgumentListener::incorrectLabel(unsigned paramIdx) {
return true;
}
bool MatchCallArgumentListener::outOfOrderArgument(
unsigned argIdx, unsigned prevArgIdx, ArrayRef<ParamBinding> bindings) {
return true;
}
bool MatchCallArgumentListener::relabelArguments(ArrayRef<Identifier> newNames){
return true;
}
bool MatchCallArgumentListener::shouldClaimArgDuringRecovery(unsigned argIdx) {
return true;
}
bool MatchCallArgumentListener::canClaimArgIgnoringNameMismatch(
const AnyFunctionType::Param &arg) {
return false;
}
/// Produce a score (smaller is better) comparing a parameter name and
/// potentially-typo'd argument name.
///
/// \param paramName The name of the parameter.
/// \param argName The name of the argument.
/// \param maxScore The maximum score permitted by this comparison, or
/// 0 if there is no limit.
///
/// \returns the score, if it is good enough to even consider this a match.
/// Otherwise, an empty optional.
///
static std::optional<unsigned> scoreParamAndArgNameTypo(StringRef paramName,
StringRef argName,
unsigned maxScore) {
using namespace camel_case;
// Compute the edit distance.
unsigned dist = argName.edit_distance(paramName, /*AllowReplacements=*/true,
/*MaxEditDistance=*/maxScore);
// If the edit distance would be too long, we're done.
if (maxScore != 0 && dist > maxScore)
return std::nullopt;
// The distance can be zero due to the "with" transformation above.
if (dist == 0)
return 1;
// If this is just a single character label on both sides,
// simply return distance.
if (paramName.size() == 1 && argName.size() == 1)
return dist;
// Only allow about one typo for every two properly-typed characters, which
// prevents completely-wacky suggestions in many cases.
if (dist > (argName.size() + 1) / 3)
return std::nullopt;
return dist;
}
bool constraints::isPackExpansionType(Type type) {
if (type->is<PackExpansionType>())
return true;
if (auto *typeVar = type->getAs<TypeVariableType>())
return typeVar->getImpl().isPackExpansion();
return false;
}
bool constraints::isSingleUnlabeledPackExpansionTuple(Type type) {
auto *tuple = type->getRValueType()->getAs<TupleType>();
return tuple && (tuple->getNumElements() == 1) &&
isPackExpansionType(tuple->getElementType(0)) &&
!tuple->getElement(0).hasName();
}
Type constraints::getPatternTypeOfSingleUnlabeledPackExpansionTuple(Type type) {
if (isSingleUnlabeledPackExpansionTuple(type)) {
auto tuple = type->getRValueType()->castTo<TupleType>();
const auto &tupleElement = tuple->getElementType(0);
if (auto *expansion = tupleElement->getAs<PackExpansionType>()) {
return expansion->getPatternType();
}
if (auto *typeVar = tupleElement->getAs<TypeVariableType>()) {
auto *locator = typeVar->getImpl().getLocator();
if (auto expansionElement =
locator->getLastElementAs<LocatorPathElt::PackExpansionType>()) {
return expansionElement->getOpenedType()->getPatternType();
}
}
}
return {};
}
bool constraints::containsPackExpansionType(ArrayRef<AnyFunctionType::Param> params) {
return llvm::any_of(params, [&](const auto ¶m) {
return isPackExpansionType(param.getPlainType());
});
}
bool constraints::containsPackExpansionType(TupleType *tuple) {
return llvm::any_of(tuple->getElements(), [&](const auto &elt) {
return isPackExpansionType(elt.getType());
});
}
bool constraints::doesMemberRefApplyCurriedSelf(Type baseTy,
const ValueDecl *decl) {
assert(decl->getDeclContext()->isTypeContext() &&
"Expected a member reference");
// For a reference to an instance method on a metatype, we want to keep the
// curried self.
if (decl->isInstanceMember()) {
assert(baseTy);
if (isa<AbstractFunctionDecl>(decl) &&
baseTy->getRValueType()->is<AnyMetatypeType>())
return false;
}
// Otherwise the reference applies self.
return true;
}
static bool areConservativelyCompatibleArgumentLabels(
ConstraintSystem &cs, OverloadChoice choice,
SmallVectorImpl<FunctionType::Param> &args,
MatchCallArgumentListener &listener,
std::optional<unsigned> unlabeledTrailingClosureArgIndex) {
ValueDecl *decl = nullptr;
switch (choice.getKind()) {
case OverloadChoiceKind::Decl:
case OverloadChoiceKind::DeclViaBridge:
case OverloadChoiceKind::DeclViaDynamic:
case OverloadChoiceKind::DeclViaUnwrappedOptional:
decl = choice.getDecl();
break;
// KeyPath application is not filtered in `performMemberLookup`.
case OverloadChoiceKind::KeyPathApplication:
case OverloadChoiceKind::DynamicMemberLookup:
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
case OverloadChoiceKind::TupleIndex:
case OverloadChoiceKind::MaterializePack:
case OverloadChoiceKind::ExtractFunctionIsolation:
return true;
}
// If this is a member lookup, the call arguments (if we have any) will
// generally be applied to the second level of parameters, with the member
// lookup applying the curried self at the first level. But there are cases
// where we can get an unapplied declaration reference back.
auto hasAppliedSelf =
decl->hasCurriedSelf() &&
doesMemberRefApplyCurriedSelf(choice.getBaseType(), decl);
AnyFunctionType *fnType = nullptr;
if (decl->hasParameterList()) {
fnType = decl->getInterfaceType()->castTo<AnyFunctionType>();
if (hasAppliedSelf) {
fnType = fnType->getResult()->getAs<AnyFunctionType>();
assert(fnType && "Parameter list curry level does not match type");
}
} else if (auto *VD = dyn_cas
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一套基于Swift语言的iOS和OS X应用开发设计源码,涵盖了19280个文件,包括13652个Swift源文件、1572个头文件(.h)、916个C++源文件(.cpp)、756个Swift接口定义文件(.sil)、313个预期文件(.expected)、232个文本文件(.txt)、225个Python脚本(.py)、201个GYB模板文件(.gyb)、144个JSON配置文件、141个模块映射文件(.modulemap),以及少量其他类型的文件。Swift是苹果公司为OS X和iOS应用开发而推出的现代编程语言,以其简洁、快速、安全和高效的特性超越了Objective-C,同时保持与Objective-C的兼容性。
资源推荐
资源详情
资源评论
收起资源包目录
基于Swift语言的iOS和OS X应用开发设计源码 (2000个子文件)
CSSimplify.cpp 615KB
Decl.cpp 423KB
CSApply.cpp 386KB
ImportDecl.cpp 374KB
ParseDecl.cpp 365KB
SILVerifier.cpp 325KB
IRGenSIL.cpp 316KB
CSDiagnostics.cpp 314KB
Deserialization.cpp 311KB
GenEnum.cpp 311KB
ParseSIL.cpp 305KB
SILGenPoly.cpp 305KB
ClangImporter.cpp 296KB
ConstraintSystem.cpp 293KB
SILGenApply.cpp 288KB
TypeCheckAttr.cpp 287KB
SILGenExpr.cpp 275KB
GenMeta.cpp 269KB
Serialization.cpp 263KB
ASTPrinter.cpp 259KB
TypeCheckConcurrency.cpp 255KB
TypeCheckProtocol.cpp 254KB
MiscDiagnostics.cpp 246KB
GenDecl.cpp 240KB
GenCall.cpp 239KB
ASTContext.cpp 237KB
TypeCheckType.cpp 236KB
SILGenLValue.cpp 232KB
TypeLowering.cpp 212KB
CSGen.cpp 199KB
SILFunctionType.cpp 199KB
Type.cpp 193KB
DeserializeSIL.cpp 186KB
AddressLowering.cpp 170KB
ASTMangler.cpp 168KB
GenProto.cpp 167KB
LoadableByAddress.cpp 167KB
AssociatedTypeInference.cpp 165KB
TypeCheckAvailability.cpp 164KB
TypeCheckDeclPrimary.cpp 157KB
MoveOnlyAddressCheckerUtils.cpp 155KB
MetadataRequest.cpp 155KB
IRGenDebugInfo.cpp 154KB
SILGenPattern.cpp 153KB
NameLookup.cpp 153KB
PullbackCloner.cpp 151KB
ASTDumper.cpp 148KB
SILPrinter.cpp 145KB
Demangler.cpp 145KB
DefiniteInitialization.cpp 144KB
TypeCheckStorage.cpp 143KB
Remangler.cpp 141KB
Module.cpp 141KB
ImportType.cpp 141KB
TypeLayout.cpp 141KB
TypeCheckDeclObjC.cpp 140KB
SimplifyCFG.cpp 139KB
SILInstructions.cpp 139KB
RegionAnalysis.cpp 139KB
SerializeSIL.cpp 138KB
TypeCheckEffects.cpp 137KB
Generics.cpp 136KB
ParseExpr.cpp 134KB
ASTVerifier.cpp 132KB
NodePrinter.cpp 130KB
Builtins.cpp 125KB
CompletionLookup.cpp 123KB
GenExistential.cpp 122KB
GenClass.cpp 120KB
Driver.cpp 118KB
GenType.cpp 114KB
TypeCheckStmt.cpp 113KB
PredictableMemOpt.cpp 112KB
GenFunc.cpp 111KB
Attr.cpp 110KB
Formatting.cpp 110KB
OldRemangler.cpp 109KB
TypeCheckDecl.cpp 107KB
Lexer.cpp 107KB
ASTBridging.cpp 106KB
AbstractionPattern.cpp 104KB
ConsumeOperatorCopyableAddressesChecker.cpp 103KB
TypeCheckAccess.cpp 103KB
ModuleAnalyzerNodes.cpp 102KB
CSFix.cpp 102KB
SwiftDeclSynthesizer.cpp 99KB
ParseStmt.cpp 99KB
Expr.cpp 99KB
MemAccessUtils.cpp 96KB
CSBindings.cpp 96KB
swift_api_digester_main.cpp 95KB
ConstExpr.cpp 94KB
ImportName.cpp 94KB
ConstantFolding.cpp 93KB
CSSyntacticElement.cpp 93KB
SILGenBridging.cpp 93KB
SILGenBuiltin.cpp 93KB
PreCheckExpr.cpp 91KB
CSSolver.cpp 91KB
TypeCheckDeclOverride.cpp 90KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
xyq2024
- 粉丝: 2373
- 资源: 5440
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功