mini-llvm 0.1.0
Loading...
Searching...
No Matches
Type.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <concepts>
6#include <cstdint>
7#include <format>
8#include <memory>
9#include <string>
10
13
14namespace mini_llvm::ir {
15
16class Constant;
17
19public:
20 virtual ~Type() = default;
21
22 Type() = default;
23
24 Type(const Type &) = delete;
25 Type &operator=(const Type &) = delete;
26
27 Type(Type &&) = delete;
28 Type &operator=(Type &&) = delete;
29
30 virtual int size() const = 0;
31 virtual int alignment() const = 0;
32 virtual int size(int) const { return size(); }
33 virtual int alignment(int) const { return alignment(); }
34
35 virtual int bitSize() const {
36 return size() * 8;
37 }
38
39 virtual int bitAlignment() const {
40 return alignment() * 8;
41 }
42
43 virtual int bitSize(int pointerSize) const {
44 return size(pointerSize) * 8;
45 }
46
47 virtual int bitAlignment(int pointerAlignment) const {
48 return alignment(pointerAlignment) * 8;
49 }
50
51 virtual std::unique_ptr<Constant> zeroValue() const = 0;
52 virtual std::unique_ptr<Constant> constant(int64_t value) const = 0;
53 virtual std::unique_ptr<Type> promoted() const = 0;
54 virtual std::unique_ptr<Type> demoted() const = 0;
55 virtual std::string format() const = 0;
56 virtual std::unique_ptr<Type> clone() const = 0;
57 virtual void accept(TypeVisitor &visitor) = 0;
58 virtual void accept(TypeVisitor &visitor) const = 0;
59
60protected:
61 virtual bool equals(const Type &other) const = 0;
62
63 friend bool operator==(const Type &lhs, const Type &rhs);
64};
65
66inline bool operator==(const Type &lhs, const Type &rhs) {
67 return lhs.equals(rhs);
68}
69
70} // namespace mini_llvm::ir
71
72template <typename TypeT>
73 requires std::derived_from<TypeT, mini_llvm::ir::Type>
74struct std::formatter<TypeT> {
75 constexpr auto parse(std::format_parse_context &ctx) {
76 return ctx.begin();
77 }
78
79 template <typename FormatContext>
80 auto format(const TypeT &type, FormatContext &ctx) const {
81 return std::format_to(ctx.out(), "{}", type.format());
82 }
83};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition Constant.h:13
Definition TypeVisitor.h:25
virtual std::unique_ptr< Type > clone() const =0
virtual int size() const =0
virtual int alignment(int) const
Definition Type.h:33
virtual int bitAlignment(int pointerAlignment) const
Definition Type.h:47
virtual std::unique_ptr< Type > demoted() const =0
Type & operator=(const Type &)=delete
virtual ~Type()=default
Type(const Type &)=delete
friend bool operator==(const Type &lhs, const Type &rhs)
Definition Type.h:66
virtual std::unique_ptr< Constant > constant(int64_t value) const =0
virtual void accept(TypeVisitor &visitor) const =0
virtual int bitSize(int pointerSize) const
Definition Type.h:43
Type(Type &&)=delete
virtual std::unique_ptr< Constant > zeroValue() const =0
virtual int size(int) const
Definition Type.h:32
virtual int bitAlignment() const
Definition Type.h:39
virtual std::unique_ptr< Type > promoted() const =0
virtual std::string format() const =0
Type & operator=(Type &&)=delete
virtual bool equals(const Type &other) const =0
virtual void accept(TypeVisitor &visitor)=0
virtual int bitSize() const
Definition Type.h:35
virtual int alignment() const =0
Definition Argument.h:13
bool operator==(const Constant &lhs, const Constant &rhs)
Definition Constant.h:28
constexpr auto parse(std::format_parse_context &ctx)
Definition Type.h:75
auto format(const TypeT &type, FormatContext &ctx) const
Definition Type.h:80