|
| 1 | +//===--- CSFix.cpp - Constraint Fixes -------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file implements the \c ConstraintFix class and its related types, |
| 14 | +// which is used by constraint solver to attempt to fix constraints to be |
| 15 | +// able to produce a solution which is easily diagnosable. |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#include "CSFix.h" |
| 20 | +#include "CSDiagnostics.h" |
| 21 | +#include "ConstraintLocator.h" |
| 22 | +#include "ConstraintSystem.h" |
| 23 | +#include "swift/AST/Expr.h" |
| 24 | +#include "swift/AST/Type.h" |
| 25 | +#include "swift/Basic/SourceManager.h" |
| 26 | +#include "llvm/ADT/SmallString.h" |
| 27 | +#include "llvm/Support/raw_ostream.h" |
| 28 | +#include <string> |
| 29 | + |
| 30 | +using namespace swift; |
| 31 | +using namespace constraints; |
| 32 | + |
| 33 | +ConstraintFix::~ConstraintFix() {} |
| 34 | + |
| 35 | +Expr *ConstraintFix::getAnchor() const { return getLocator()->getAnchor(); } |
| 36 | + |
| 37 | +void ConstraintFix::print(llvm::raw_ostream &Out, SourceManager *sm) const { |
| 38 | + Out << "[fix: "; |
| 39 | + Out << getName(); |
| 40 | + Out << ']'; |
| 41 | + Out << " @ "; |
| 42 | + getLocator()->dump(sm, Out); |
| 43 | +} |
| 44 | + |
| 45 | +void ConstraintFix::dump(SourceManager *sm) const { print(llvm::errs(), sm); } |
| 46 | + |
| 47 | +std::string ForceDowncast::getName() const { |
| 48 | + llvm::SmallString<16> name; |
| 49 | + name += "force downcast (as! "; |
| 50 | + name += DowncastTo->getString(); |
| 51 | + name += ")"; |
| 52 | + return name.c_str(); |
| 53 | +} |
| 54 | + |
| 55 | +bool ForceDowncast::diagnose(Expr *expr, const Solution &solution) const { |
| 56 | + MissingExplicitConversionFailure failure(expr, solution, getLocator(), |
| 57 | + DowncastTo); |
| 58 | + return failure.diagnose(); |
| 59 | +} |
| 60 | + |
| 61 | +ForceDowncast *ForceDowncast::create(ConstraintSystem &cs, Type toType, |
| 62 | + ConstraintLocator *locator) { |
| 63 | + return new (cs.getAllocator()) ForceDowncast(toType, locator); |
| 64 | +} |
| 65 | + |
| 66 | +bool ForceOptional::diagnose(Expr *root, const Solution &solution) const { |
| 67 | + MissingOptionalUnwrapFailure failure(root, solution, getLocator()); |
| 68 | + return failure.diagnose(); |
| 69 | +} |
| 70 | + |
| 71 | +ForceOptional *ForceOptional::create(ConstraintSystem &cs, |
| 72 | + ConstraintLocator *locator) { |
| 73 | + return new (cs.getAllocator()) ForceOptional(locator); |
| 74 | +} |
| 75 | + |
| 76 | +bool UnwrapOptionalBase::diagnose(Expr *root, const Solution &solution) const { |
| 77 | + bool resultIsOptional = |
| 78 | + getKind() == FixKind::UnwrapOptionalBaseWithOptionalResult; |
| 79 | + MemberAccessOnOptionalBaseFailure failure(root, solution, getLocator(), |
| 80 | + MemberName, resultIsOptional); |
| 81 | + return failure.diagnose(); |
| 82 | +} |
| 83 | + |
| 84 | +UnwrapOptionalBase *UnwrapOptionalBase::create(ConstraintSystem &cs, |
| 85 | + DeclName member, |
| 86 | + ConstraintLocator *locator) { |
| 87 | + return new (cs.getAllocator()) |
| 88 | + UnwrapOptionalBase(FixKind::UnwrapOptionalBase, member, locator); |
| 89 | +} |
| 90 | + |
| 91 | +UnwrapOptionalBase *UnwrapOptionalBase::createWithOptionalResult( |
| 92 | + ConstraintSystem &cs, DeclName member, ConstraintLocator *locator) { |
| 93 | + return new (cs.getAllocator()) UnwrapOptionalBase( |
| 94 | + FixKind::UnwrapOptionalBaseWithOptionalResult, member, locator); |
| 95 | +} |
| 96 | + |
| 97 | +bool AddAddressOf::diagnose(Expr *root, const Solution &solution) const { |
| 98 | + MissingAddressOfFailure failure(root, solution, getLocator()); |
| 99 | + return failure.diagnose(); |
| 100 | +} |
| 101 | + |
| 102 | +AddAddressOf *AddAddressOf::create(ConstraintSystem &cs, |
| 103 | + ConstraintLocator *locator) { |
| 104 | + return new (cs.getAllocator()) AddAddressOf(locator); |
| 105 | +} |
| 106 | + |
| 107 | +bool CoerceToCheckedCast::diagnose(Expr *root, const Solution &solution) const { |
| 108 | + MissingForcedDowncastFailure failure(root, solution, getLocator()); |
| 109 | + return failure.diagnose(); |
| 110 | +} |
| 111 | + |
| 112 | +CoerceToCheckedCast *CoerceToCheckedCast::create(ConstraintSystem &cs, |
| 113 | + ConstraintLocator *locator) { |
| 114 | + return new (cs.getAllocator()) CoerceToCheckedCast(locator); |
| 115 | +} |
| 116 | + |
| 117 | +bool MarkExplicitlyEscaping::diagnose(Expr *root, |
| 118 | + const Solution &solution) const { |
| 119 | + NoEscapeFuncToTypeConversionFailure failure(root, solution, getLocator(), |
| 120 | + ConvertTo); |
| 121 | + return failure.diagnose(); |
| 122 | +} |
| 123 | + |
| 124 | +MarkExplicitlyEscaping * |
| 125 | +MarkExplicitlyEscaping::create(ConstraintSystem &cs, ConstraintLocator *locator, |
| 126 | + Type convertingTo) { |
| 127 | + return new (cs.getAllocator()) MarkExplicitlyEscaping(locator, convertingTo); |
| 128 | +} |
| 129 | + |
| 130 | +bool RelabelArguments::diagnose(Expr *root, const Solution &solution) const { |
| 131 | + LabelingFailure failure(solution, getLocator(), getLabels()); |
| 132 | + return failure.diagnose(); |
| 133 | +} |
| 134 | + |
| 135 | +RelabelArguments * |
| 136 | +RelabelArguments::create(ConstraintSystem &cs, |
| 137 | + llvm::ArrayRef<Identifier> correctLabels, |
| 138 | + ConstraintLocator *locator) { |
| 139 | + unsigned size = totalSizeToAlloc<Identifier>(correctLabels.size()); |
| 140 | + void *mem = cs.getAllocator().Allocate(size, alignof(RelabelArguments)); |
| 141 | + return new (mem) RelabelArguments(correctLabels, locator); |
| 142 | +} |
| 143 | + |
| 144 | +bool MissingConformance::diagnose(Expr *root, const Solution &solution) const { |
| 145 | + MissingConformanceFailure failure(root, solution, getLocator(), |
| 146 | + {NonConformingType, Protocol}); |
| 147 | + return failure.diagnose(); |
| 148 | +} |
| 149 | + |
| 150 | +MissingConformance *MissingConformance::create(ConstraintSystem &cs, Type type, |
| 151 | + ProtocolDecl *protocol, |
| 152 | + ConstraintLocator *locator) { |
| 153 | + return new (cs.getAllocator()) MissingConformance(type, protocol, locator); |
| 154 | +} |
0 commit comments