|
| 1 | +//===--- TypeCheckCompletionCallback.h -----------------------------------===// |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See https://swift.org/LICENSE.txt for license information |
| 7 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +// |
| 11 | +/// \file |
| 12 | +/// Provides TypeCheckCompletionCallback implementations for the various kinds |
| 13 | +/// of code completion. These extract and persist information needed to compute |
| 14 | +/// completion results from the solutions formed during expression typechecking. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H |
| 19 | +#define SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H |
| 20 | + |
| 21 | +#include "swift/AST/Expr.h" |
| 22 | +#include "swift/AST/Type.h" |
| 23 | +#include "swift/Basic/LLVM.h" |
| 24 | +#include "llvm/ADT/DenseMap.h" |
| 25 | +#include "llvm/ADT/SmallVector.h" |
| 26 | + |
| 27 | +namespace swift { |
| 28 | +class Decl; |
| 29 | +class DeclContext; |
| 30 | +class Type; |
| 31 | +class ValueDecl; |
| 32 | +class CodeCompletionExpr; |
| 33 | + |
| 34 | +namespace constraints { |
| 35 | +class ConstraintSystem; |
| 36 | +class Solution; |
| 37 | +} // namespace constraints |
| 38 | + |
| 39 | +namespace ide { |
| 40 | + |
| 41 | +class TypeCheckCompletionCallback { |
| 42 | + bool GotCallback = false; |
| 43 | + |
| 44 | +protected: |
| 45 | + /// Subclasses of \c TypeCheckCompletionCallback handle solutions discovered |
| 46 | + /// by the constraint system in this function |
| 47 | + virtual void sawSolutionImpl(const constraints::Solution &solution) = 0; |
| 48 | + |
| 49 | +public: |
| 50 | + virtual ~TypeCheckCompletionCallback() {} |
| 51 | + |
| 52 | + /// Called for each solution produced while type-checking an expression |
| 53 | + /// that the code completion expression participates in. |
| 54 | + void sawSolution(const constraints::Solution &solution) { |
| 55 | + GotCallback = true; |
| 56 | + sawSolutionImpl(solution); |
| 57 | + }; |
| 58 | + |
| 59 | + /// True if at least one solution was passed via the \c sawSolution |
| 60 | + /// callback. |
| 61 | + bool gotCallback() const { return GotCallback; } |
| 62 | + |
| 63 | + /// Typecheck the code completion expression in its outermost expression |
| 64 | + /// context, calling \c sawSolution for each solution formed. |
| 65 | + virtual void fallbackTypeCheck(DeclContext *DC); |
| 66 | +}; |
| 67 | + |
| 68 | +// MARK: - Utility functions for subclasses of TypeCheckCompletionCallback |
| 69 | + |
| 70 | +Type getTypeForCompletion(const constraints::Solution &S, Expr *E); |
| 71 | + |
| 72 | +/// Whether the given completion expression is the only expression in its |
| 73 | +/// containing closure or function body and its value is implicitly returned. |
| 74 | +/// |
| 75 | +/// If these conditions are met, code completion needs to avoid penalizing |
| 76 | +/// completion results that don't match the expected return type when |
| 77 | +/// computing type relations, as since no return statement was explicitly |
| 78 | +/// written by the user, it's possible they intend the single expression not |
| 79 | +/// as the return value but merely the first entry in a multi-statement body |
| 80 | +/// they just haven't finished writing yet. |
| 81 | +bool isImplicitSingleExpressionReturn(constraints::ConstraintSystem &CS, |
| 82 | + Expr *CompletionExpr); |
| 83 | + |
| 84 | +/// Returns \c true iff the decl context \p DC allows calling async functions. |
| 85 | +bool isContextAsync(const constraints::Solution &S, DeclContext *DC); |
| 86 | + |
| 87 | +} // namespace ide |
| 88 | +} // namespace swift |
| 89 | + |
| 90 | +#endif // SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H |
0 commit comments