Commit e88319e5 authored by Bednárek David RNDr. Ph.D.'s avatar Bednárek David RNDr. Ph.D.
Browse files

fmwk support for enums

parent d59c45b2
......@@ -58,6 +58,35 @@ namespace cecko {
}
}
CIDecl CKEnumType::declaration(bool is_const, const CIDecl& dtor) const { return decl_const(is_const) + "enum " + get_name() + decl_dtor(false, false, dtor); }
void CKEnumType::finalize(CKConstantObsVector items)
{
assert(!defined_);
elements_ordered_ = std::move(items);
defined_ = true;
}
void CKEnumType::dump(CIOStream& os) const
{
if (defined_)
{
os << "enum " << get_name();
std::string delim = "{";
for (auto&& a : elements_ordered_)
{
os << delim << CIEndl;
os << "\t" << a->declaration();
delim = ",";
}
if ( delim == "{" )
os << delim;
else
os << CIEndl;
os << "};" << CIEndl;
}
}
CKFunctionType::CKFunctionType(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic)
: ret_type_(ret_type), arg_types_(std::move(arg_types)), variadic_(variadic), irt_(nullptr)
{
......@@ -120,16 +149,29 @@ namespace cecko {
arrts_.for_each(dlambda);
fncts_.for_each(dlambda);
*/
os << "// --- ENUMS ---" << std::endl;
enmts_.for_each([&os](auto&& a) {
a->dump(os);
});
os << "// --- STRUCTS ---" << std::endl;
strts_.for_each([&os](auto&& a) {
a->dump(os);
});
}
std::string CKConstant::declaration() const
{
return get_name() + "=" + std::to_string(value_->getValue().getZExtValue());
}
CKTypedefConstObs CKGlobalTable::declare_typedef(const std::string& name, const CKTypeRefPack& type_pack)
{
return typedefs_.try_emplace(name, type_pack);
}
CKConstantConstObs CKGlobalTable::declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
{
return constants_.try_emplace(name, type, value);
}
CKGlobalVarObs CKGlobalTable::varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack)
{
auto irtp = type_pack.type->get_ir();
......@@ -167,6 +209,9 @@ namespace cecko {
}
CKNamedObs CKGlobalTable::find(const CIName& n)
{
auto pc = constants_.find(n);
if (pc)
return pc;
auto q = vars_.find(n);
if (!!q)
return q;
......@@ -227,6 +272,10 @@ namespace cecko {
{
return typedefs_.try_emplace(name, type_pack);
}
CKConstantConstObs CKLocalTable::declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
{
return constants_.try_emplace(name, type, value);
}
CKLocalVarObs CKLocalTable::varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack)
{
auto var = builder.CreateAlloca(type_pack.type->get_ir(), nullptr, name);
......@@ -242,9 +291,12 @@ namespace cecko {
}
CKNamedObs CKLocalTable::find(const CIName& n)
{
auto p = vars_.find(n);
if (p)
return p;
auto pc = constants_.find(n);
if (pc)
return pc;
auto pv = vars_.find(n);
if (pv)
return pv;
return parent_scope_->find(n);
}
void CKLocalTable::dump(CIOStream& os) const
......@@ -368,6 +420,17 @@ namespace cecko {
return globtable_->declare_typedef(name, type_pack);
}
}
CKConstantConstObs CKContext::define_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value)
{
if (!!loctable_)
{
return loctable_->declare_constant(name, type, value);
}
else
{
return globtable_->declare_constant(name, type, value);
}
}
CKVarObs CKContext::define_var(const std::string& name, const CKTypeRefPack& type_pack)
{
if (!!loctable_)
......
......@@ -215,6 +215,7 @@ namespace cecko {
virtual bool is_bool() const { return false; }
virtual bool is_char() const { return false; }
virtual bool is_int() const { return false; }
virtual bool is_enum() const { return false; }
virtual bool is_array() const { return false; }
virtual bool is_function() const { return false; }
virtual bool is_pointer() const { return false; }
......@@ -392,6 +393,33 @@ namespace cecko {
CKIRStructTypeObs irt_;
};
class CKConstant;
using CKConstantConstObs = const CKConstant*;
using CKConstantObsVector = std::vector<CKConstantConstObs>;
class CKEnumType : public CIAbstractType, public CINamePtr {
public:
CKEnumType(CKTypeObs base_type)
: defined_(false), base_type_(base_type)
{}
void finalize(CKConstantObsVector items);
virtual std::size_t hash() const { return std::hash<CIName>{}(get_name()); }
virtual bool is_enum() const { return true; }
virtual bool is_defined() const { return defined_; }
virtual CITypeMangle mangle() const { return "E" + get_name() + '$'; }
virtual CIDecl declaration(bool is_const, const CIDecl& dtor) const;
virtual CKIRTypeObs get_ir() const { return base_type_->get_ir(); }
void dump(CIOStream& os) const;
private:
bool defined_;
CKTypeObs base_type_;
CKConstantObsVector elements_ordered_;
};
using CKTypeObsArray = std::vector<CKTypeObs>;
class CKFunctionType : public CIAbstractType {
......@@ -425,6 +453,7 @@ namespace cecko {
using CKArrayTypeObs = const CKArrayType*;
using CKFunctionTypeObs = const CKFunctionType*;
using CKStructTypeObs = CKStructType*;
using CKEnumTypeObs = CKEnumType*;
class CKTypeTable : CIImmovable {
public:
......@@ -443,6 +472,11 @@ namespace cecko {
return strts_.try_emplace(n, Context, n);
}
CKStructTypeObs find_struct_type(const CIName& n) { return strts_.find(n); }
CKEnumTypeObs declare_enum_type(const CIName& n, CKTypeObs base_type)
{
return enmts_.try_emplace(n, base_type);
}
CKEnumTypeObs find_enum_type(const CIName& n) { return enmts_.find(n); }
void dump(CIOStream& os) const;
private:
......@@ -454,6 +488,7 @@ namespace cecko {
CIHashedStorage< CKArrayType> arrts_;
CIHashedStorage< CKFunctionType> fncts_;
CINamedStorage< CKStructType> strts_;
CINamedStorage< CKEnumType> enmts_;
};
class CKAbstractNamed : public CINamePtr, CIImmovable {
......@@ -462,11 +497,11 @@ namespace cecko {
virtual bool is_typedef() const { return false; }
virtual bool is_var() const { return false; }
virtual bool is_function() const { return false; }
virtual bool is_constant() const { return false; }
virtual bool is_const() const { return false; }
virtual CKTypeObs get_type() const = 0;
virtual CKIRValueObs get_ir() const { return nullptr; };
virtual CKIRFunctionObs get_function_ir() const { return nullptr; };
virtual void dump(CIOStream& os) const = 0;
};
using CKNamedObs = CKAbstractNamed*;
......@@ -477,17 +512,33 @@ namespace cecko {
: type_pack_(type_pack)
{}
virtual bool is_typedef() const { return true; }
virtual bool is_var() const { return false; }
virtual CKTypeObs get_type() const { return type_pack_.type; }
virtual bool is_const() const { return type_pack_.is_const; }
const CKTypeRefPack& get_type_pack() const { return type_pack_; }
virtual void dump(CIOStream& os) const;
void dump(CIOStream& os) const;
private:
CKTypeRefPack type_pack_;
};
using CKTypedefConstObs = const CKTypedef*;
class CKConstant : public CKAbstractNamed {
public:
CKConstant(CKTypeObs type, CKIRConstantIntObs value)
: type_pack_(type, true), value_(value)
{}
virtual bool is_constant() const { return true; }
virtual CKTypeObs get_type() const { return type_pack_.type; }
virtual bool is_const() const { return type_pack_.is_const; }
const CKTypeRefPack& get_type_pack() const { return type_pack_; }
CKIRConstantIntObs get_constant_value() const { return value_; }
virtual CKIRValueObs get_ir() const { return value_; }
std::string declaration() const;
private:
CKTypeRefPack type_pack_;
CKIRConstantIntObs value_;
};
class CKVar : public CKAbstractNamed {
public:
CKVar(CKTypeRefPack type_pack)
......@@ -497,7 +548,7 @@ namespace cecko {
virtual CKTypeObs get_type() const { return type_pack_.type; }
virtual bool is_const() const { return type_pack_.is_const; }
const CKTypeRefPack& get_type_pack() const { return type_pack_; }
virtual void dump(CIOStream& os) const;
void dump(CIOStream& os) const;
private:
CKTypeRefPack type_pack_;
};
......@@ -556,7 +607,7 @@ namespace cecko {
virtual CKIRFunctionObs get_function_ir() const { return irf_; }
const CKFunctionFormalPack& get_formal_pack(std::size_t ix) const { return formal_packs_[ix]; }
CKLocalTableObs define(CKAbstractScopeObs parent, CKIRBuilderRef builder, CKFunctionFormalPackArray formal_packs);
virtual void dump(CIOStream& os) const;
void dump(CIOStream& os) const;
private:
CKFunctionTypeObs type_;
CKIRFunctionObs irf_;
......@@ -572,6 +623,7 @@ namespace cecko {
CKGlobalTable()
{}
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKConstantConstObs declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKGlobalVarObs varDefine(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack);
CKGlobalVarObs declare_extern_variable(CKIRModuleObs M, const std::string& name, const CKTypeRefPack& type_pack);
CKFunctionObs declare_function(const CIName& n, CKIRModuleObs M, CKFunctionTypeObs type);
......@@ -584,6 +636,7 @@ namespace cecko {
void dump(CIOStream& os) const;
private:
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKConstant> constants_;
CINamedStorage< CKFunction> fncs_;
CINamedStorage< CKGlobalVar> vars_;
};
......@@ -612,6 +665,7 @@ namespace cecko {
void varsFromArgs(CKIRBuilderRef builder, CKFunctionObs f, const CKFunctionFormalPackArray& formal_packs);
CKTypedefConstObs declare_typedef(const CIName& name, const CKTypeRefPack& type_pack);
CKConstantConstObs declare_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKLocalVarObs varDefine(CKIRBuilderRef builder, const std::string& name, const CKTypeRefPack& type_pack);
virtual CKTypedefConstObs find_typedef(const CIName& n) const;
......@@ -623,6 +677,7 @@ namespace cecko {
CKAbstractScopeObs parent_scope_;
CKFunctionObs function_;
CINamedStorage< CKTypedef> typedefs_;
CINamedStorage< CKConstant> constants_;
CINamedStorage< CKLocalVar> vars_;
};
......@@ -694,8 +749,11 @@ namespace cecko {
CKFunctionTypeObs get_function_type(CKTypeObs ret_type, CKTypeObsArray arg_types, bool variadic = false) { return typetable_->get_function_type(ret_type, std::move(arg_types), variadic); }
CKStructTypeObs declare_struct_type(const CIName& n) { return typetable_->declare_struct_type(n, module_->getContext()); }
CKStructTypeObs find_struct_type(const CIName& n) { return typetable_->find_struct_type(n); }
CKEnumTypeObs declare_enum_type(const CIName& n) { return typetable_->declare_enum_type(n, get_int_type()); }
CKEnumTypeObs find_enum_type(const CIName& n) { return typetable_->find_enum_type(n); }
CKVarObs define_var(const std::string& name, const CKTypeRefPack& type_pack);
CKTypedefConstObs define_typedef(const std::string& name, const CKTypeRefPack& type_pack);
CKConstantConstObs define_constant(const std::string& name, CKTypeObs type, CKIRConstantIntObs value);
CKFunctionObs declare_function(const CIName& n, CKFunctionTypeObs type)
{
return globtable_->declare_function(n, module_, type);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment