-
Notifications
You must be signed in to change notification settings - Fork 497
/
const.h
83 lines (63 loc) · 2.25 KB
/
const.h
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
// Copyright (C) 2022-2024 Exaloop Inc. <https://exaloop.io>
#pragma once
#include "codon/cir/module.h"
#include "codon/cir/value.h"
namespace codon {
namespace ir {
/// CIR constant base. Once created, constants are immutable.
class Const : public AcceptorExtend<Const, Value> {
private:
/// the type
types::Type *type;
public:
static const char NodeId;
/// Constructs a constant.
/// @param type the type
/// @param name the name
explicit Const(types::Type *type, std::string name = "")
: AcceptorExtend(std::move(name)), type(type) {}
private:
types::Type *doGetType() const override { return type; }
std::vector<types::Type *> doGetUsedTypes() const override { return {type}; }
int doReplaceUsedType(const std::string &name, types::Type *newType) override;
};
template <typename ValueType>
class TemplatedConst : public AcceptorExtend<TemplatedConst<ValueType>, Const> {
private:
ValueType val;
public:
static const char NodeId;
using AcceptorExtend<TemplatedConst<ValueType>, Const>::getModule;
using AcceptorExtend<TemplatedConst<ValueType>, Const>::getSrcInfo;
using AcceptorExtend<TemplatedConst<ValueType>, Const>::getType;
TemplatedConst(ValueType v, types::Type *type, std::string name = "")
: AcceptorExtend<TemplatedConst<ValueType>, Const>(type, std::move(name)),
val(v) {}
/// @return the internal value.
ValueType getVal() const { return val; }
/// Sets the value.
/// @param v the value
void setVal(ValueType v) { val = v; }
};
using IntConst = TemplatedConst<int64_t>;
using FloatConst = TemplatedConst<double>;
using BoolConst = TemplatedConst<bool>;
using StringConst = TemplatedConst<std::string>;
template <typename T> const char TemplatedConst<T>::NodeId = 0;
template <>
class TemplatedConst<std::string>
: public AcceptorExtend<TemplatedConst<std::string>, Const> {
private:
std::string val;
public:
static const char NodeId;
TemplatedConst(std::string v, types::Type *type, std::string name = "")
: AcceptorExtend(type, std::move(name)), val(std::move(v)) {}
/// @return the internal value.
std::string getVal() const { return val; }
/// Sets the value.
/// @param v the value
void setVal(std::string v) { val = std::move(v); }
};
} // namespace ir
} // namespace codon