Yume
type_holder.cpp
Go to the documentation of this file.
1#include "type_holder.hpp"
3#include "ty/type.hpp"
4#include "util.hpp"
5#include <initializer_list>
6#include <llvm/ADT/StringRef.h>
7#include <string>
8#include <utility>
9
10namespace yume {
12 int j = 0;
13 for (const int i : {8, 16, 32, 64}) {
14 IntTypePair ints{};
15 for (const bool is_signed : {true, false}) {
16 const string type_name = (is_signed ? "I"s : "U"s) + std::to_string(i);
17 auto i_ty = std::make_unique<ty::Int>(type_name, i, is_signed);
18 (is_signed ? ints.s_ty : ints.u_ty) = i_ty.get();
19 known.insert({i_ty->name(), move(i_ty)});
20 }
21 int_types.at(j++) = ints;
22 }
23
24 auto bool_ty = std::make_unique<ty::Int>("Bool", 1, false);
25 bool_type = bool_ty.get();
26 known.insert({bool_ty->name(), move(bool_ty)});
27
28 auto nil_ty = std::make_unique<ty::Nil>();
29 nil_type = nil_ty.get();
30 known.insert({nil_ty->name(), move(nil_ty)});
31}
32
34 for (const bool is_signed : {true, false}) {
35 unsigned size_bits = compiler.ptr_bitsize();
36 const string type_name = (is_signed ? "I"s : "U"s) + "Size";
37 auto i_ty = std::make_unique<ty::Int>(type_name, size_bits, is_signed);
38 (is_signed ? size_type.s_ty : size_type.u_ty) = i_ty.get();
39 known.insert({i_ty->name(), move(i_ty)});
40 }
41}
42
43auto TypeHolder::find_or_create_fn_type(const vector<ty::Type>& args, optional<ty::Type> ret,
44 const vector<ty::Type>& closure) -> ty::Function* {
45 for (const auto& i : fn_types)
46 if (i->ret() == ret && i->args() == args && i->closure() == closure && !i->is_fn_ptr())
47 return i.get();
48
49 auto& new_fn = fn_types.emplace_back(std::make_unique<ty::Function>("", args, ret, closure, false));
50 return new_fn.get();
51}
52
53auto TypeHolder::find_or_create_fn_ptr_type(const vector<ty::Type>& args, optional<ty::Type> ret, bool c_varargs)
54 -> ty::Function* {
55 for (const auto& i : fn_types) {
56 if (i->ret() == ret && i->args() == args && c_varargs == i->is_c_varargs() && i->closure().empty() &&
57 i->is_fn_ptr()) {
58 return i.get();
59 }
60 }
61
62 auto& new_fn =
63 fn_types.emplace_back(std::make_unique<ty::Function>("", args, ret, vector<ty::Type>{}, true, c_varargs));
64 return new_fn.get();
65}
66} // namespace yume
The Compiler the the primary top-level type during compilation. A single instance is created during t...
Definition: compiler.hpp:45
auto ptr_bitsize() -> unsigned int
Definition: compiler.cpp:1369
A function pointer type.
Definition: type.hpp:129
Definition: ast.cpp:8
void declare_size_type(Compiler &)
Definition: type_holder.cpp:33
ty::Int * bool_type
Definition: type_holder.hpp:18
auto find_or_create_fn_ptr_type(const vector< ty::Type > &args, optional< ty::Type > ret, bool c_varargs=false) -> ty::Function *
Definition: type_holder.cpp:53
llvm::StringMap< unique_ptr< ty::BaseType > > known
Definition: type_holder.hpp:21
auto find_or_create_fn_type(const vector< ty::Type > &args, optional< ty::Type > ret, const vector< ty::Type > &closure) -> ty::Function *
Definition: type_holder.cpp:43
IntTypePair size_type
Definition: type_holder.hpp:19
array< IntTypePair, 4 > int_types
Definition: type_holder.hpp:17
ty::Nil * nil_type
Definition: type_holder.hpp:20