2 // (C) Sergey Chaban (serge@wildwestsoftware.com)
5 using System
.Collections
;
6 using System
.Reflection
;
7 using System
.Reflection
.Emit
;
12 public class MethodName
{
13 private static int methodCount
= 0;
20 public MethodName () : this ("M_" + (methodCount
++))
26 /// <param name="name"></param>
27 public MethodName (string name
) : this (name
, false)
33 /// <param name="name"></param>
34 /// <param name="ctor"></param>
35 public MethodName (string name
, bool ctor
)
73 private MethodName name
;
74 private MethodAttributes attrs
;
75 private CallingConventions callConv
;
76 private string retType
;
77 private MethodBuilder method_builder
;
78 private bool entry_point
= false;
80 private ArrayList param_list
;
81 private ArrayList instructions
;
82 private ArrayList local_list
;
88 name
= new MethodName ();
107 /// <param name="name"></param>
108 public void SetMethodName (MethodName name
)
128 public string RetType
{
140 public MethodAttributes Attrs
{
152 public CallingConventions CallConv
{
164 public bool IsEntryPoint
{
175 /// <param name="instr"></param>
176 public void AddInstruction (InstrBase instr
)
179 throw new InternalErrorException ("<null> instruction");
182 if (instructions
== null) {
183 this.instructions
= new ArrayList ();
186 instructions
.Add (instr
);
189 public void AddLocal (DictionaryEntry local
)
191 if (local_list
== null)
192 local_list
= new ArrayList ();
194 local_list
.Add (local
);
198 public void SetParamList (ArrayList param_list
)
200 this.param_list
= param_list
;
205 public int InstrCount
{
207 return (instructions
!= null) ? instructions
.Count
: 0;
213 /// <returns></returns>
214 public override string ToString ()
216 return String
.Format ("IL.Method [Name: {0}, Attrs: {1}, CallConv: {2}, RetType: {3}, Instr: {4}]",
217 Name
, Attrs
, CallConv
, RetType
, InstrCount
);
220 public MethodBuilder Builder
{
222 return method_builder
;
226 public void Resolve (Class host
)
228 Type return_type
= host
.CodeGen
.TypeManager
[RetType
];
229 method_builder
= host
.TypeBuilder
.DefineMethod (Name
, Attrs
,
230 CallConv
, return_type
, CreateTypeList (host
.CodeGen
.TypeManager
));
235 /// <param name="tb"></param>
236 public void Emit (Class host
)
238 TypeBuilder tb
= host
.TypeBuilder
;
242 ILGenerator ilgen
= method_builder
.GetILGenerator ();
244 if (local_list
!= null) {
245 foreach (DictionaryEntry local
in local_list
) {
246 Type local_type
= host
.CodeGen
.TypeManager
[(string)local
.Key
];
247 if (local_type
== null) {
248 Console
.WriteLine ("Could not find type: {0}", local
.Key
);
251 ilgen
.DeclareLocal (local_type
);
255 if (instructions
!= null) {
256 foreach (InstrBase instr
in instructions
)
257 instr
.Emit (ilgen
, host
);
262 private Type
[] CreateTypeList (TypeManager type_manager
)
264 if (param_list
== null)
267 int count
= param_list
.Count
;
268 Type
[] type_list
= new Type
[count
];
270 for (int i
=0; i
<count
; i
++) {
271 type_list
[i
] = type_manager
[(string) param_list
[i
]];