|
| 1 | +//===--- TypeLowering.h - Swift Type Lowering for Reflection ----*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Implements logic for computing in-memory layouts from TypeRefs loaded from |
| 14 | +// reflection metadata. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_REFLECTION_TYPELOWERING_H |
| 19 | +#define SWIFT_REFLECTION_TYPELOWERING_H |
| 20 | + |
| 21 | +#include "llvm/ADT/DenseMap.h" |
| 22 | + |
| 23 | +#include <iostream> |
| 24 | +#include <memory> |
| 25 | + |
| 26 | +namespace swift { |
| 27 | +namespace reflection { |
| 28 | + |
| 29 | +class TypeRef; |
| 30 | +class TypeRefBuilder; |
| 31 | + |
| 32 | +enum class TypeInfoKind : unsigned { |
| 33 | + Builtin, |
| 34 | + Struct, |
| 35 | + Tuple, |
| 36 | +}; |
| 37 | + |
| 38 | +class TypeInfo { |
| 39 | + TypeInfoKind Kind; |
| 40 | + unsigned Size, Alignment, Stride, NumExtraInhabitants; |
| 41 | + |
| 42 | +public: |
| 43 | + TypeInfo(TypeInfoKind Kind, |
| 44 | + unsigned Size, unsigned Alignment, |
| 45 | + unsigned Stride, unsigned NumExtraInhabitants) |
| 46 | + : Kind(Kind), Size(Size), Alignment(Alignment), Stride(Stride), |
| 47 | + NumExtraInhabitants(NumExtraInhabitants) {} |
| 48 | + |
| 49 | + TypeInfoKind getKind() const { return Kind; } |
| 50 | + |
| 51 | + unsigned getSize() const { return Size; } |
| 52 | + unsigned getAlignment() const { return Alignment; } |
| 53 | + unsigned getStride() const { return Stride; } |
| 54 | + unsigned getNumExtraInhabitants() const { return NumExtraInhabitants; } |
| 55 | + |
| 56 | + void dump() const; |
| 57 | + void dump(std::ostream &OS, unsigned Indent = 0) const; |
| 58 | +}; |
| 59 | + |
| 60 | +struct FieldInfo { |
| 61 | + std::string Name; |
| 62 | + unsigned Offset; |
| 63 | + const TypeInfo &TI; |
| 64 | +}; |
| 65 | + |
| 66 | +/// Class instances, structs, tuples |
| 67 | +class RecordTypeInfo : public TypeInfo { |
| 68 | + std::vector<FieldInfo> Fields; |
| 69 | + |
| 70 | +public: |
| 71 | + RecordTypeInfo(TypeInfoKind Kind, |
| 72 | + unsigned Size, unsigned Alignment, |
| 73 | + unsigned Stride, unsigned NumExtraInhabitants, |
| 74 | + const std::vector<FieldInfo> &Fields) |
| 75 | + : TypeInfo(Kind, Size, Alignment, Stride, NumExtraInhabitants), |
| 76 | + Fields(Fields) {} |
| 77 | + |
| 78 | + unsigned getNumFields() const { return Fields.size(); } |
| 79 | + const std::vector<FieldInfo> &getFields() const { return Fields; } |
| 80 | +}; |
| 81 | + |
| 82 | +/// This class owns the memory for all TypeInfo instances that it vends. |
| 83 | +class TypeConverter { |
| 84 | + TypeRefBuilder &Builder; |
| 85 | + std::vector<std::unique_ptr<const TypeInfo>> Pool; |
| 86 | + llvm::DenseMap<const TypeRef *, const TypeInfo *> Cache; |
| 87 | + |
| 88 | +public: |
| 89 | + explicit TypeConverter(TypeRefBuilder &Builder) : Builder(Builder) {} |
| 90 | + |
| 91 | + TypeRefBuilder &getBuilder() { return Builder; } |
| 92 | + |
| 93 | + const TypeInfo *getTypeInfo(const TypeRef *TR); |
| 94 | +}; |
| 95 | + |
| 96 | +} |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
0 commit comments