summaryrefslogtreecommitdiff
path: root/src/hir/hir.hpp
blob: 7633d916516d998327b5f3b7a079c28205ac62a5 (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
141
142
143
/*
 * High-level intermediate representation
 *
 * Contains the expanded and desugared AST
 */
#pragma once

#include <cassert>
#include <unordered_map>
#include <vector>
#include <memory>

#include <tagged_union.hpp>

#include <macros.hpp>   // DAMNIT - Why can't I have it be incomplete

#include <hir/type.hpp>
#include <hir/path.hpp>

namespace HIR {

class Expr {
};

class Crate;
class Module;

class Function;
class Static;

class ValueItem;
class TypeItem;

template<typename Ent>
struct VisEnt
{
    bool is_public;
    Ent ent;
};


struct GenericParams
{
};

// --------------------------------------------------------------------
// Type structures
// --------------------------------------------------------------------
struct Static
{
    GenericParams   m_params;
};
struct Function
{
    GenericParams   m_params;
};

// --------------------------------------------------------------------
// Type structures
// --------------------------------------------------------------------
struct TypeAlias
{
    GenericParams   m_params;
    ::HIR::TypeRef  m_type;
};
struct Enum
{
    TAGGED_UNION(Variant, Unit,
        (Unit, struct{}),
        (Value, ::HIR::Expr),
        (Tuple, ::std::vector<::HIR::TypeRef>),
        (Struct, ::std::pair< ::std::string, ::HIR::TypeRef>)
        );
    GenericParams   m_params;
    ::std::vector< Variant >    m_variants;
};
struct Struct
{
    TAGGED_UNION(Data, Unit,
        (Unit, struct {}),
        (Tuple, ::std::vector< VisEnt<::HIR::TypeRef> >),
        (Named, ::std::vector< ::std::pair< ::std::string, VisEnt<::HIR::TypeRef> > >)
        );
    GenericParams   m_params;
    Data  m_data;
};

struct AssociatedType
{
    GenericParams   m_params;   // For bounds, and maybe HKT?
    ::HIR::TypeRef  m_default;
};
TAGGED_UNION(TraitValueItem, Static,
    (Static,    Static),
    (Function,  Function)
    );
struct Trait
{
    GenericParams   m_params;
    ::std::vector< ::HIR::GenericPath >  m_parent_traits;
    
    ::std::unordered_map< ::std::string, AssociatedType >   m_types;
    ::std::unordered_map< ::std::string, TraitValueItem >   m_values;
};

class Module
{
public:
    // Contains all values and functions (including type constructors)
    ::std::unordered_map< ::std::string, ::std::unique_ptr<VisEnt<ValueItem>> > m_value_items;
    // Contains types, traits, and modules
    ::std::unordered_map< ::std::string, ::std::unique_ptr<VisEnt<TypeItem>> > m_mod_items;
    // Glob imports
    ::std::vector< VisEnt<::HIR::Path> >   m_glob_imports;
};

// --------------------------------------------------------------------

TAGGED_UNION(TypeItem, Import,
    (Import, ::HIR::Path),
    (Module, Module),
    (TypeAlias, TypeAlias), // NOTE: These don't introduce new values
    (Enum,      Enum),
    (Struct,    Struct),
    (Trait,     Trait)
    );
TAGGED_UNION(ValueItem, Import,
    (Import,    ::HIR::Path),
    (Static,    Static),
    (StructConstant,    struct { ::HIR::TypeRef ty; }),
    (Function,  Function),
    (StructConstructor, struct { ::HIR::TypeRef ty; })
    );

class Crate
{
public:
    Module  m_root_module;
    
    ::std::unordered_map< ::std::string, ::MacroRules >   m_exported_macros;
};

}   // namespace HIR