Files
swift-mirror/include/swift/AST/ExistentialLayout.h
Slava Pestov db58e02cb2 Sema: Hook up layout constraints to the solver
There were various problems with layout constraints either
being ignored or handled incorrectly. Now that I've exercised
this support with an upcoming patch, there are some fixes
here.

Also, introduce a new ExistentialLayout::getLayoutConstriant()
which returns a value for existentials which are class-constrained
but don't have a superclass or any class-constrained protocols;
an example would be AnyObject, or AnyObject & P for some
non-class protocol P.

NFC for now, since these layout-constrained existentials cannot
be constructed yet.
2017-04-13 21:17:05 -07:00

88 lines
2.5 KiB
C++

//===--- ExistentialLayout.h - Existential type decomposition ---*- 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 the ExistentialLayout struct.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_EXISTENTIAL_LAYOUT_H
#define SWIFT_EXISTENTIAL_LAYOUT_H
#include "swift/AST/ASTContext.h"
#include "swift/AST/Type.h"
#include "llvm/ADT/SmallVector.h"
namespace swift {
class ProtocolDecl;
class ProtocolType;
class ProtocolCompositionType;
struct ExistentialLayout {
ExistentialLayout() {
requiresClass = false;
requiresClassImplied = false;
containsNonObjCProtocol = false;
singleProtocol = nullptr;
}
ExistentialLayout(ProtocolType *type);
ExistentialLayout(ProtocolCompositionType *type);
/// The superclass constraint, if any.
Type superclass;
/// Whether the existential requires a class, either via an explicit
/// '& AnyObject' member or because of a superclass or protocol constraint.
bool requiresClass : 1;
/// Whether the class constraint was implied by another constraint and therefore
/// does not need to be stated explicitly.
bool requiresClassImplied : 1;
/// Whether any protocol members are non-@objc.
bool containsNonObjCProtocol : 1;
bool isAnyObject() const;
bool isObjC() const {
// FIXME: Does the superclass have to be @objc?
return requiresClass && !containsNonObjCProtocol;
}
// Does this existential contain the Error protocol?
bool isExistentialWithError(ASTContext &ctx) const;
// Does this existential consist of an Error protocol only with no other
// constraints?
bool isErrorExistential() const;
ArrayRef<ProtocolType *> getProtocols() const {
if (singleProtocol)
return ArrayRef<ProtocolType *>{&singleProtocol, 1};
return multipleProtocols;
}
LayoutConstraint getLayoutConstraint() const;
private:
// Inline storage for 'protocols' member above when computing
// layout of a single ProtocolType
ProtocolType *singleProtocol;
/// Zero or more protocol constraints.
ArrayRef<ProtocolType *> multipleProtocols;
};
}
#endif // SWIFT_EXISTENTIAL_LAYOUT_H