00001 #include "lirutil.h"
00002
00003
00004 char* LirUtil::new_id(char *s) {
00005 char * buf = new char[80];
00006 snprintf(buf, 80, "__%s%s", CBZ::get_temp_id_str().c_str(), s);
00007 return buf;
00008 }
00009
00010 declNode * LirUtil::new_static_global(typeNode * the_type,
00011 exprNode * init) {
00012 declNode * ret = new declNode(LirUtil::new_id(),
00013 declNode::STATIC,
00014 the_type,
00015 init,
00016 NULL);
00017 ret->decl_location(declNode::TOP);
00018 return ret;
00019 }
00020
00021 declNode * LirUtil::new_auto_decl(typeNode * the_type) {
00022 declNode * ret = new declNode(LirUtil::new_id(),
00023 declNode::AUTO,
00024 the_type,
00025 NULL, NULL);
00026 ret->decl_location(declNode::BLOCK);
00027 return ret;
00028 }
00029
00030
00031 string LirUtil::getTypeString(typeNode * the_type) {
00032 if ( !the_type )
00033 return "unknown";
00034
00035 if ( primNode * the_prim = dynamic_cast<primNode *>(the_type) ) {
00036 const basic_type & the_basic = the_prim->basic();
00037 if ( the_basic.is_integer() ) {
00038 if ( the_basic.is_char() )
00039 if ( the_basic.is_unsigned() )
00040 return "uchar";
00041 else
00042 return "char";
00043 else if ( the_basic.is_short() )
00044 if ( the_basic.is_unsigned() )
00045 return "ushort";
00046 else
00047 return "short";
00048 else if ( the_basic.is_long() )
00049 if ( the_basic.is_unsigned() )
00050 return "ulong";
00051 else
00052 return "long";
00053 else
00054 if ( the_basic.is_unsigned() )
00055 return "uint";
00056 else
00057 return "int";
00058 } else if ( the_basic.is_float() ) {
00059 if ( the_basic.is_long() )
00060 return "double";
00061 else
00062 return "float";
00063 }
00064 }
00065
00066 if ( the_type->is_void() )
00067 return "void";
00068
00069 if ( the_type->is_pointer() )
00070 return "pointer";
00071
00072 if ( the_type->is_aggregate() )
00073 return "aggr";
00074
00075 if ( the_type->is_ellipsis() )
00076 return "ellipsis";
00077
00078 if ( funcNode * the_func = dynamic_cast<funcNode *>(the_type) )
00079 return "func_decl";
00080
00081 return "unknown";
00082 }
00083
00084 typeNode * LirUtil::newVoidPtr(void) {
00085 return new ptrNode(typeNode::NONE,
00086 new primNode(typeNode::NONE, basic_type::Void));
00087 }