Files
swift-mirror/include/swift/SIL/SILBuiltinVisitor.h
Mark Lacey c0c848d2b3 Add Builtin.type_join* family of functions.
These will be used for unit-testing the Type::join functionality in the
type checker. The result of the join is replaced during constraint
generation with the actual type.

There is currently no checking for whether the arguments can be used to
statically compute the value, so bad things will likely happen if
e.g. they are type variables. Once more of the basic functionality of
Type::join is working I'll make this a bit more bullet-proof in that
regard.

They include:
  // Compute the join of T and U and return the metatype of that type.
  Builtin.type_join<T, U, V>(_: T.Type, _: U.Type) -> V.Type

  // Compute the join of &T and U and return the metatype of that type.
  Builtin.type_join_inout<T, U, V>(_: inout T, _: U.Type) -> V.Type

  // Compute the join of T.Type and U.Type and return that type.
  Builtin.type_join_meta<T, U, V>(_: T.Type, _: U.Type) -> V.Type

I've added a couple simple tests to start off, based on what currently
works (aka doesn't cause an assert, crash, etc.).
2017-09-21 14:43:26 -07:00

83 lines
2.8 KiB
C++

//===--- SILBuiltinVisitor.h ------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file contains SILBuiltinVisitor, a visitor for visiting all possible
/// builtins and llvm intrinsics able to be used by BuiltinInst.
///
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SIL_SILBUILTINVISITOR_H
#define SWIFT_SIL_SILBUILTINVISITOR_H
#include "swift/SIL/SILInstruction.h"
#include <type_traits>
namespace swift {
template <typename ImplClass, typename ValueRetTy = void>
class SILBuiltinVisitor {
public:
ImplClass &asImpl() { return static_cast<ImplClass &>(*this); }
/// Perform any required pre-processing before visiting.
///
/// Sub-classes can override this method to provide custom pre-processing.
void beforeVisit(BuiltinInst *BI) {}
ValueRetTy visit(BuiltinInst *BI) {
asImpl().beforeVisit(BI);
if (auto BuiltinKind = BI->getBuiltinKind()) {
switch (BuiltinKind.getValue()) {
// BUILTIN_TYPE_CHECKER_OPERATION does not live past the type checker.
#define BUILTIN_TYPE_CHECKER_OPERATION(ID, NAME) \
case BuiltinValueKind::ID: \
llvm_unreachable("Unexpected type checker operation seen in SIL!");
#define BUILTIN(ID, NAME, ATTRS) \
case BuiltinValueKind::ID: \
return asImpl().visit##ID(BI, ATTRS);
#include "swift/AST/Builtins.def"
case BuiltinValueKind::None:
llvm_unreachable("None case");
}
llvm_unreachable("Not all cases handled?!");
}
if (auto IntrinsicID = BI->getIntrinsicID()) {
return asImpl().visitLLVMIntrinsic(BI, IntrinsicID.getValue());
}
llvm_unreachable("Not all cases handled?!");
}
ValueRetTy visitLLVMIntrinsic(BuiltinInst *BI, llvm::Intrinsic::ID ID) {
return ValueRetTy();
}
ValueRetTy visitBuiltinValueKind(BuiltinInst *BI, BuiltinValueKind Kind,
StringRef Attrs) {
return ValueRetTy();
}
#define BUILTIN(ID, NAME, ATTRS) \
ValueRetTy visit##ID(BuiltinInst *BI, StringRef) { \
return asImpl().visitBuiltinValueKind(BI, BuiltinValueKind::ID, ATTRS); \
}
#include "swift/AST/Builtins.def"
};
} // end swift namespace
#endif