source: dev/trunk/abdev/BasicCompiler_Common/src/Compiler.cpp@ 290

Last change on this file since 290 was 290, checked in by dai_9181, 17 years ago

ジェネリクスのベースを実装

File size: 4.6 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/SmoothieException.h>
4
5#include <Compiler.h>
6#include <Type.h>
7
8Compiler compiler;
9
10void Compiler::StaticLink( ObjectModules &staticLibraries )
11{
12 BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries )
13 {
14 // メタ情報
15 pNowObjectModule->StaticLink( *pStaticLibrary );
16 }
17}
18
19bool Compiler::StringToType( const string &typeName, Type &type ){
20 type.SetIndex( -1 );
21
22
23 /////////////////////////////////////////////////////////
24 // ☆★☆ ジェネリクスサポート ☆★☆
25
26 if( strstr( typeName.c_str(), "<" ) )
27 {
28 // ジェネリッククラスをインスタンス化した型の場合
29 int i = 0;
30 char className[VN_SIZE], typeParameter[VN_SIZE];
31 GetIdentifierToken( className, typeName.c_str(), i );
32 i++;
33 GetIdentifierToken( typeParameter, typeName.c_str(), i );
34
35 // ジェネリクスクラスを取得
36 const CClass *pGenericClass = compiler.GetObjectModule().meta.GetClasses().Find( className );
37
38 // 型パラメータの型情報を取得
39 Type baseType;
40 StringToType( typeParameter, baseType );
41
42 GenericTypes genericTypes;
43 genericTypes.push_back( GenericType( "(non support)", baseType ) );
44
45 // 基本型をセット
46 type.SetBasicType( DEF_OBJECT );
47
48 // 拡張情報をセット
49 type.SetClassPtr( pGenericClass );
50 type.SetActualGenericTypes( genericTypes );
51
52 return true;
53 }
54
55 //
56 /////////////////////////////////////////////////////////
57
58
59 if( typeName[0] == '*' ){
60 if( typeName.size() >= 3
61 && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) ){
62 //関数ポインタ(*Function)
63 type.SetBasicType( DEF_PTR_PROC );
64 type.SetIndex( compiler.GetObjectModule().meta.GetProcPointers().Add( typeName ) );
65 return true;
66 }
67
68 const string &nextTypeName = typeName.substr( 1 );
69
70 if( !StringToType( nextTypeName, type ) ){
71 return false;
72 }
73
74 type.PtrLevelUp();
75
76 return true;
77 }
78
79 {
80 int basicType;
81 if( Type::StringToBasicType( typeName, basicType ) ){
82 // 基本型だったとき
83 type.SetBasicType( basicType );
84 return true;
85 }
86 }
87
88 // Object型だったとき
89 if( typeName == "Object" ){
90 type.SetType( DEF_OBJECT, compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
91 return true;
92 }
93
94 // String型だったとき
95 if( typeName == "String" ){
96 type.SetType( DEF_OBJECT, compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() );
97 return true;
98 }
99
100
101 ////////////////////
102 // TypeDefされた型
103 ////////////////////
104 int i=compiler.GetObjectModule().meta.GetTypeDefs().GetIndex( typeName );
105 if(i!=-1){
106 type = compiler.GetObjectModule().meta.GetTypeDefs()[i].GetBaseType();
107 return true;
108 }
109
110 //クラス
111 const CClass *pobj_c = compiler.GetObjectModule().meta.GetClasses().Find( typeName );
112 if(pobj_c){
113 if( pobj_c->IsStructure() ){
114 type.SetBasicType( DEF_STRUCT );
115 }
116 else{
117 type.SetBasicType( DEF_OBJECT );
118 }
119 type.SetClassPtr( pobj_c );
120 return true;
121 }
122
123
124 /////////////////////////////////////////////////////////
125 // ☆★☆ ジェネリクスサポート ☆★☆
126
127 // 型パラメータ
128 if( compiler.pCompilingClass )
129 {
130 // クラスに属するメソッドをコンパイルしているとき
131 if( compiler.pCompilingClass->IsExistFormalGenericTypeParameter( typeName ) )
132 {
133 // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき
134 type.SetBasicType( DEF_TYPE_PARAMETER );
135 type.SetClassPtr( compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
136 return true;
137 }
138 }
139
140 //
141 /////////////////////////////////////////////////////////
142
143 return false;
144}
145
146const string Compiler::TypeToString( const Type &type )
147{
148 if( PTR_LEVEL( type.GetBasicType() ) ){
149 //ポインタレベルが1以上の場合
150 Type tempType( type );
151 tempType.PtrLevelDown();
152
153 return (string)"*" + TypeToString( tempType );
154 }
155 else if( type.IsObject() || type.IsStruct() ){
156 //オブジェクトまたは構造体
157
158 if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ){
159 if( type.GetClass().GetNamespaceScopes().size() >= 1 )
160 {
161 return type.GetClass().GetNamespaceScopes().ToString() + "." + type.GetClass().GetName();
162 }
163 return type.GetClass().GetName();
164 }
165 }
166 else if( type.IsProcPtr() ){
167 if( type.GetIndex() == 0 || type.GetIndex() == -1 ){
168 return "VoidPtr";
169 }
170 else{
171 if( compiler.GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
172 return "*Sub";
173 }
174 return "*Function";
175 }
176 }
177 else{
178 // 基本型
179 const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
180 if( lpszTypeName )
181 {
182 return (const string)lpszTypeName;
183 }
184 }
185
186 SmoothieException::Throw( 1 );
187
188 return (string)"(null)";
189}
Note: See TracBrowser for help on using the repository browser.