[193] | 1 | #include <jenga/include/smoothie/Type.h>
|
---|
| 2 | #include <jenga/include/smoothie/SmoothieException.h>
|
---|
| 3 |
|
---|
[184] | 4 | #include <Compiler.h>
|
---|
| 5 |
|
---|
[195] | 6 | Compiler compiler;
|
---|
[193] | 7 |
|
---|
| 8 | bool Compiler::StringToType( const string &typeName, Type &type ){
|
---|
| 9 | type.SetIndex( -1 );
|
---|
| 10 |
|
---|
| 11 | if( typeName[0] == '*' ){
|
---|
| 12 | if( typeName.size() >= 3
|
---|
| 13 | && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) ){
|
---|
| 14 | //関数ポインタ(*Function)
|
---|
| 15 | type.SetBasicType( DEF_PTR_PROC );
|
---|
| 16 | type.SetIndex( compiler.GetMeta().GetProcPointers().Add( typeName ) );
|
---|
| 17 | return true;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | const string &nextTypeName = typeName.substr( 1 );
|
---|
| 21 |
|
---|
| 22 | if( !StringToType( nextTypeName, type ) ){
|
---|
| 23 | return false;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | type.PtrLevelUp();
|
---|
| 27 |
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | {
|
---|
| 32 | int basicType;
|
---|
| 33 | if( Type::StringToBasicType( typeName, basicType ) ){
|
---|
| 34 | // 基本型だったとき
|
---|
| 35 | type.SetBasicType( basicType );
|
---|
| 36 | return true;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | // Object型だったとき
|
---|
| 41 | if( typeName == "Object" ){
|
---|
| 42 | type.SetType( DEF_OBJECT, compiler.GetMeta().GetClasses().GetObjectClassPtr() );
|
---|
| 43 | return true;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | // String型だったとき
|
---|
| 47 | if( typeName == "String" ){
|
---|
| 48 | type.SetType( DEF_OBJECT, compiler.GetMeta().GetClasses().GetStringClassPtr() );
|
---|
| 49 | return true;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | ////////////////////
|
---|
| 54 | // TypeDefされた型
|
---|
| 55 | ////////////////////
|
---|
| 56 | int i=compiler.GetMeta().GetTypeDefs().GetIndex( typeName );
|
---|
| 57 | if(i!=-1){
|
---|
| 58 | type = compiler.GetMeta().GetTypeDefs()[i].GetBaseType();
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //クラス
|
---|
| 63 | const CClass *pobj_c = compiler.GetMeta().GetClasses().Find( typeName );
|
---|
| 64 | if(pobj_c){
|
---|
| 65 | type.SetClassPtr( pobj_c );
|
---|
| 66 |
|
---|
| 67 | if( pobj_c->IsStructure() ){
|
---|
| 68 | type.SetBasicType( DEF_STRUCT );
|
---|
| 69 | }
|
---|
| 70 | else{
|
---|
| 71 | type.SetBasicType( DEF_OBJECT );
|
---|
| 72 | }
|
---|
| 73 | return true;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | const string Compiler::TypeToString( const Type &type )
|
---|
| 80 | {
|
---|
| 81 | if( PTR_LEVEL( type.GetBasicType() ) ){
|
---|
| 82 | //ポインタレベルが1以上の場合
|
---|
| 83 | Type tempType( type );
|
---|
| 84 | tempType.PtrLevelDown();
|
---|
| 85 |
|
---|
[196] | 86 | return (string)"*" + TypeToString( tempType );
|
---|
[193] | 87 | }
|
---|
| 88 | else if( type.IsObject() || type.IsStruct() ){
|
---|
| 89 | //オブジェクトまたは構造体
|
---|
| 90 |
|
---|
| 91 | if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ){
|
---|
| 92 | if( type.GetClass().GetNamespaceScopes().size() >= 1 )
|
---|
| 93 | {
|
---|
| 94 | return type.GetClass().GetNamespaceScopes().ToString() + "." + type.GetClass().GetName();
|
---|
| 95 | }
|
---|
| 96 | return type.GetClass().GetName();
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | else if( type.IsProcPtr() ){
|
---|
| 100 | if( type.GetIndex() == 0 || type.GetIndex() == -1 ){
|
---|
| 101 | return "VoidPtr";
|
---|
| 102 | }
|
---|
| 103 | else{
|
---|
| 104 | if( compiler.GetMeta().GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
|
---|
| 105 | return "*Sub";
|
---|
| 106 | }
|
---|
| 107 | return "*Function";
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | else{
|
---|
| 111 | // 基本型
|
---|
| 112 | const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
|
---|
| 113 | if( lpszTypeName )
|
---|
| 114 | {
|
---|
| 115 | return (const string)lpszTypeName;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | SmoothieException::Throw( 1 );
|
---|
| 120 |
|
---|
| 121 | return (string)"(null)";
|
---|
| 122 | }
|
---|