| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // Method.cs
- // (C) Sergey Chaban ([email protected])
- using System;
- using System.Collections;
- using System.Reflection;
- using System.Reflection.Emit;
- namespace Mono.ILASM {
- public class MethodName {
- private static int methodCount = 0;
- private bool isCtor;
- private string name;
- /// <summary>
- /// </summary>
- public MethodName () : this ("M_" + (methodCount++))
- {
- }
- /// <summary>
- /// </summary>
- /// <param name="name"></param>
- public MethodName (string name) : this (name, false)
- {
- }
- /// <summary>
- /// </summary>
- /// <param name="name"></param>
- /// <param name="ctor"></param>
- public MethodName (string name, bool ctor)
- {
- this.name = name;
- this.isCtor = ctor;
- }
- /// <summary>
- /// </summary>
- public string Name {
- get {
- return name;
- }
- set {
- name = value;
- }
- }
- /// <summary>
- /// </summary>
- public bool IsCtor {
- get {
- return isCtor;
- }
- set {
- isCtor = value;
- }
- }
- }
- /// <summary>
- /// </summary>
- public class Method {
- private MethodName name;
- private MethodAttributes attrs;
- private CallingConventions callConv;
- private string retType;
- private ArrayList instructions;
- /// <summary>
- /// </summary>
- public Method ()
- {
- name = new MethodName ();
- attrs = 0;
- }
- /// <summary>
- /// </summary>
- public string Name {
- get {
- return name.Name;
- }
- set {
- name.Name = value;
- }
- }
- /// <summary>
- /// </summary>
- /// <param name="name"></param>
- public void SetMethodName (MethodName name)
- {
- this.name = name;
- }
- /// <summary>
- /// </summary>
- public bool IsCtor {
- get {
- return name.IsCtor;
- }
- set {
- name.IsCtor = value;
- }
- }
- /// <summary>
- /// </summary>
- public string RetType {
- get {
- return retType;
- }
- set {
- retType = value;
- }
- }
- /// <summary>
- /// </summary>
- public MethodAttributes Attrs {
- get {
- return attrs;
- }
- set {
- attrs = value;
- }
- }
- /// <summary>
- /// </summary>
- public CallingConventions CallConv {
- get {
- return callConv;
- }
- set {
- callConv = value;
- }
- }
- /// <summary>
- /// </summary>
- /// <param name="instr"></param>
- public void AddInstruction (InstrBase instr)
- {
- if (instr == null) {
- throw new System.NullReferenceException ("<null> instruction");
- }
- if (instructions == null) {
- this.instructions = new ArrayList ();
- }
- instructions.Add (instr);
- }
- /// <summary>
- /// </summary>
- public int InstrCount {
- get {
- return (instructions != null) ? instructions.Count : 0;
- }
- }
- /// <summary>
- /// </summary>
- /// <returns></returns>
- public override string ToString ()
- {
- return String.Format ("IL.Method [Name: {0}, Attrs: {1}, CallConv: {2}, RetType: {3}, Instr: {4}]",
- Name, Attrs, CallConv, RetType, InstrCount);
- }
- /// <summary>
- /// </summary>
- /// <param name="tb"></param>
- public void Emit (Class host)
- {
- TypeBuilder tb = host.TypeBuilder;
- if (IsCtor) {
- } else {
- Type rt = host.CodeGen.RefTypes.Lookup (RetType);
- MethodBuilder mb = tb.DefineMethod (Name, Attrs, CallConv, rt, null);
- ILGenerator ilgen = mb.GetILGenerator ();
- foreach (InstrBase instr in instructions) {
- instr.Emit (ilgen);
- }
- }
- }
- }
- }
|