📄 svartype.cpp
字号:
// Copyright(C) 1999-2005 LiuTaoTao,bookaa@rorsoft.com
// exe2c project
#include "stdafx.h"
#include "00000.h"
#include "SVarType.h"
#include "strparse.h"
void define_replace(PSTR buf);
VarTypeID do_struct(PCSTR &p);
VarTypeID do_union(PCSTR &p);
CVarTypeMng* g_VarTypeManage = NULL;
void CExprManage_cpp_Init()
{
g_VarTypeManage = new CVarTypeMng;
}
void CExprManage_cpp_exit()
{
if (g_VarTypeManage)
{
delete g_VarTypeManage;
g_VarTypeManage = NULL;
}
}
CVarTypeMng::CVarTypeMng()
{
list = new VarTypeList;
nextfreeid = 1;
// 用这些缺省的类型填小于100的编号
NewBaseVarType(0, "void"); // id = 1 id_void
NewBaseVarType(1, "BYTE"); // id = 2
NewBaseVarType(2, "WORD"); // id = 3
NewBaseVarType(4, "DWORD"); // id = 4
NewBaseVarType(4, "double"); // id = 5
NewBaseVarType(8, "__int64"); // id = 6
NewSignedVarType(id_BYTE, "char"); // id = 7
NewSignedVarType(id_WORD, "short");
NewSignedVarType(id_DWORD, "int" );
NewSignedVarType(id_DWORD, "long" );
FuncType2VarID((CFuncType*)1);
//New_p(1, "PVOID"); // id = 8
nextfreeid = 50; // from 50
}
CVarTypeMng::~CVarTypeMng()
{
POSITION pos = list->GetHeadPosition();
while (pos)
{
SVarType* p = list->GetNext(pos);
switch (p->type)
{
case vtt_typedef:
delete p->m_typedef.name;
break;
case vtt_base:
delete p->m_base.name;
break;
case vtt_signed:
delete p->m_signed.name;
break;
case vtt_funcpoint:
//???? if (p->m_funcpoint.pFuncType != (CFuncType*)1) // 为1表明未知func
// delete p->m_funcpoint.pFuncType;
break;
case vtt_simple:
break;
}
delete p;
}
delete list;
list = NULL;
}
VarTypeID CVarTypeMng::New_p(VarTypeID id0)
{ // 新建一个数据类型,它指向id0
assert(id0);
VarTypeID id = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_point;
pnew->m_point.id_pointto = id0;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::FuncType2VarID(CFuncType* ft)
{
VarTypeID id = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_funcpoint;
pnew->m_funcpoint.pFuncType = ft;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::Enum2VarID(enum_st* newenum)
{
VarTypeID id = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_enum;
pnew->m_enum.m_penum = newenum;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::NewTypeDef(VarTypeID id0, PCSTR name)
{ // typedef 的数据类型也需要自己的 VarTypeID
assert(id0);
SVarType* p = this->id2_VarType(id0);
assert(p);
if (p->type == vtt_class
&& p->m_class.pClass->m_name[0] == '\0')
{ // 是一个没名的class或struct或union
strcpy(p->m_class.pClass->m_name, name);
return id0;
}
VarTypeID id = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_typedef;
pnew->m_typedef.name = new_str(name);
pnew->m_typedef.id_base = id0;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::NewSignedVarType(VarTypeID id, PSTR name)
{
VarTypeID id1 = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id1;
pnew->type = vtt_signed;
pnew->m_signed.id_base = id;
pnew->m_signed.name = new_str(name);
this->list->AddTail(pnew);
return id1;
}
VarTypeID CVarTypeMng::NewSimpleVarType(SIZEOF opsize)
{
POSITION pos = this->list->GetHeadPosition();
while (pos)
{
SVarType* p = this->list->GetNext(pos);
if (p->type == vtt_simple && p->m_simple.opsize == opsize)
return p->id;
}
//not find, new it
VarTypeID id = nextfreeid++;
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_simple;
pnew->m_simple.opsize = opsize;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::NewBaseVarType(SIZEOF opsize, PSTR name)
{
VarTypeID id = nextfreeid++;
if (id >= 10)
nop();
assert(id < 10);
// 除了我自己定义这几个基类外,其它中只能用
// New_p
// NewTypeDef
// 来定义,不可能用到这个
SVarType* pnew = new SVarType;
pnew->id = id;
pnew->type = vtt_base;
pnew->m_base.name = new_str(name);
pnew->m_base.opsize = opsize;
this->list->AddTail(pnew);
return id;
}
VarTypeID CVarTypeMng::GetBaseID(BOOL fsign, SIZEOF size)
{
POSITION pos = this->list->GetHeadPosition();
while (pos)
{
SVarType* p = list->GetNext(pos);
if (p->type == vtt_base
&& p->m_base.opsize == size)
{
if (fsign)
return this->Get_signed_id(p->id);
return p->id;
}
}
//assert(0);
return 0;
}
void CVarTypeMng::VarType_ID2Name(VarTypeID id, PSTR namebuf)
{
SVarType* p = id2_VarType(id);
switch (p->type)
{
case vtt_base:
strcpy(namebuf,p->m_base.name);
return;
case vtt_simple:
sprintf(namebuf,"bit%d",p->m_simple.opsize * 8);
return;
case vtt_signed:
strcpy(namebuf,p->m_signed.name);
return;
case vtt_typedef:
strcpy(namebuf,p->m_typedef.name);
return;
case vtt_point:
VarType_ID2Name(p->m_point.id_pointto,namebuf);
strcat(namebuf,"*");
return;
case vtt_array:
VarType_ID2Name(p->m_array.id_arrayitem, namebuf);
return;
case vtt_class:
if (p->m_class.pClass == NULL)
strcpy(namebuf,p->m_class.classname);
else
strcpy(namebuf,p->m_class.pClass->m_name);
return;
case vtt_enum:
if (p->m_enum.m_penum == NULL)
strcpy(namebuf,p->m_enum.enumname);
else
strcpy(namebuf,p->m_enum.m_penum->m_name);
return;
case vtt_const:
strcpy(namebuf,"const ");
VarType_ID2Name(p->m_const.id_base, namebuf+6);
return;
default:
assert(0);
}
}
VarTypeID CVarTypeMng::NoConst(VarTypeID id)
{
SVarType* p = id2_VarType(id);
assert(p);
if (p->type == vtt_const)
return p->m_const.id_base;
return id;
}
VarTypeID CVarTypeMng::GetPointTo(VarTypeID id)
{ // 如果id 是一个指针类型,返回它所指
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_point:
return NoConst(p->m_point.id_pointto);
//因为const char *所指的是char而不是const char
case vtt_const:
return GetPointTo(p->m_const.id_base);
case vtt_funcpoint:
case vtt_signed:
case vtt_base:
case vtt_simple:
case vtt_class:
case vtt_array:
case vtt_enum:
return 0;
case vtt_typedef:
return GetPointTo(p->m_typedef.id_base);
default:
assert(0);
}
return 0;
}
SVarType* CVarTypeMng::id2_VarType(VarTypeID id)
{
assert(id);
assert(id < 2500);
POSITION pos = list->GetHeadPosition();
while (pos)
{
SVarType* p = list->GetNext(pos);
if (p->id == id)
return p;
}
alert_prtf("not find Var id = %d", id);
assert(0);
return NULL; // why here
}
CFuncType* CVarTypeMng::get_funcptr(VarTypeID id)
{
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_const:
return get_funcptr(p->m_const.id_base);
case vtt_typedef:
return get_funcptr(p->m_typedef.id_base);
case vtt_funcpoint:
return p->m_funcpoint.pFuncType;
default:
return NULL;
}
}
bool CVarTypeMng::is_simple(VarTypeID id)
{
if (id == 0)
return false;
SVarType* p = id2_VarType(id);
assert(p);
if (p->type == vtt_simple)
return true;
return false;
}
Class_st* CVarTypeMng::is_classpoint(VarTypeID id)
{
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_const:
return is_classpoint(p->m_const.id_base);
case vtt_typedef:
return is_classpoint(p->m_typedef.id_base);
case vtt_point:
return id2_Class(p->m_point.id_pointto);
default:
return NULL;
}
}
enum_st* CVarTypeMng::id2_enum(VarTypeID id)
{
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_const:
return id2_enum(p->m_const.id_base);
case vtt_typedef:
return id2_enum(p->m_typedef.id_base);
case vtt_enum:
return p->m_enum.m_penum;
default:
return NULL;
}
}
Class_st* CVarTypeMng::is_class(VarTypeID id)
{
if (id == 0)
return NULL;
return id2_Class(id);
}
Class_st* CVarTypeMng::id2_Class(VarTypeID id)
{
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_const:
return id2_Class(p->m_const.id_base);
case vtt_typedef:
return id2_Class(p->m_typedef.id_base);
case vtt_class:
return p->m_class.pClass;
default:
return NULL;
}
}
BOOL CVarTypeMng::is_funcptr(VarTypeID id)
{
// if a pointer to function
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_const:
return is_funcptr(p->m_const.id_base);
case vtt_typedef:
return is_funcptr(p->m_typedef.id_base);
case vtt_funcpoint:
return TRUE;
default:
return FALSE;
}
}
SIZEOF CVarTypeMng::VarType_ID2Size(VarTypeID id)
{
assert(id);
SVarType* p = id2_VarType(id);
assert(p);
switch (p->type)
{
case vtt_base:
return p->m_base.opsize;
case vtt_simple:
return p->m_simple.opsize;
case vtt_point:
case vtt_funcpoint:
return BIT32_is_4;
case vtt_typedef:
return VarType_ID2Size(p->m_typedef.id_base);
case vtt_array:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -