#pragma once #include "module.h" #include "value.h" namespace codon { namespace ir { /// SIR constant base. Once created, constants are immutable. class Const : public AcceptorExtend { 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 doGetUsedTypes() const override { return {type}; } int doReplaceUsedType(const std::string &name, types::Type *newType) override; }; template class TemplatedConst : public AcceptorExtend, Const> { private: ValueType val; public: static const char NodeId; using AcceptorExtend, Const>::getModule; using AcceptorExtend, Const>::getSrcInfo; using AcceptorExtend, Const>::getType; TemplatedConst(ValueType v, types::Type *type, std::string name = "") : AcceptorExtend, 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; using FloatConst = TemplatedConst; using BoolConst = TemplatedConst; using StringConst = TemplatedConst; template const char TemplatedConst::NodeId = 0; template <> class TemplatedConst : public AcceptorExtend, 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