summaryrefslogtreecommitdiff
path: root/src/hir/type.hpp
blob: 7c2d1d0e934f477f09c36dbb50108989c29bcae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

#ifndef _HIR_TYPE_HPP_
#define _HIR_TYPE_HPP_
#pragma once

#include <tagged_union.hpp>
#include <hir/path.hpp>
#include <hir/expr_ptr.hpp>

namespace HIR {

class Struct;
class Enum;

struct TypeRef;

enum class CoreType
{
    Usize, Isize,
    U8, I8,
    U16, I16,
    U32, I32,
    U64, I64,
    
    F32, F64,
    
    Bool,
    Char, Str,
};
extern ::std::ostream& operator<<(::std::ostream& os, const CoreType& ct);

enum class BorrowType
{
    Shared,
    Unique,
    Owned,
};

struct LifetimeRef
{
    ::std::string   name;
};

struct FunctionType
{
    bool    is_unsafe;
    ::std::string   m_abi;
    ::std::unique_ptr<TypeRef>  m_rettype;
    ::std::vector<TypeRef>  m_arg_types;
};

struct TypeRef
{
    // Options:
    // - Primitive
    // - Parameter
    // - Path
    
    // - Array
    // - Tuple
    // - Borrow
    // - Pointer

    TAGGED_UNION(TypePathBinding, Unbound,
    (Unbound, struct {}),   // Not yet bound (state during first part of lowering) - TODO kill
    (Opaque, struct {}),
    (Struct, const ::HIR::Struct*),
    (Enum, const ::HIR::Enum*)
    );

    TAGGED_UNION(Data, Infer,
    (Infer, struct {
        unsigned int index = 0;
        }),
    (Diverge, struct {}),
    (Primitive, ::HIR::CoreType),
    (Path, struct {
        ::HIR::Path path;
        TypePathBinding binding;
        }),
    (Generic, struct {
        ::std::string   name;
        unsigned int    binding;
        }),
    (TraitObject, struct {
        ::std::vector< ::HIR::GenericPath > m_traits;
        ::HIR::LifetimeRef  m_lifetime;
        }),
    (Array, struct {
        ::std::unique_ptr<TypeRef>  inner;
        ::HIR::ExprPtr size;
        size_t  size_val;
        }),
    (Slice, struct {
        ::std::unique_ptr<TypeRef>  inner;
        }),
    (Tuple, ::std::vector<TypeRef>),
    (Borrow, struct {
        ::HIR::BorrowType   type;
        ::std::unique_ptr<TypeRef>  inner;
        }),
    (Pointer, struct {
        bool    is_mut;
        ::std::unique_ptr<TypeRef>  inner;
        }),
    (Function, FunctionType)
    );
    
    Data   m_data;
    
    TypeRef() {}
    TypeRef(TypeRef&& ) = default;
    TypeRef(const TypeRef& ) = delete;
    TypeRef& operator=(TypeRef&& ) = default;
    TypeRef& operator=(const TypeRef&) = delete;
    
    TypeRef(::std::string name, unsigned int slot):
        m_data( Data::make_Generic({ mv$(name), slot }) )
    {}
    TypeRef(::HIR::TypeRef::Data x):
        m_data( mv$(x) )
    {}
    TypeRef(::HIR::CoreType ct):
        m_data( Data::make_Primitive(mv$(ct)) )
    {}
    TypeRef(::HIR::Path p):
        m_data( Data::make_Path( {mv$(p), TypePathBinding()} ) )
    {}
    
    TypeRef clone() const;
    
    void fmt(::std::ostream& os) const;
};

extern ::std::ostream& operator<<(::std::ostream& os, const ::HIR::TypeRef& ty);

}   // namespace HIR

#endif