summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/emit/AssemblyBuilder.scala
blob: 6bf4c7d1daeea2454d04aa47b80fa20158205710 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * System.Reflection.Emit-like API for writing .NET assemblies to MSIL
 */


package ch.epfl.lamp.compiler.msil.emit

import ch.epfl.lamp.compiler.msil._
import java.io.IOException

/**
 * Defines and represents a dynamic assembly.
 * A dynamic assembly is an assembly that is created using the compiler.msil
 * emit APIs. The dynamic modules in the assembly are saved when the dynamic
 * assembly is saved using the Save method. To generate an executable, the
 * SetEntryPoint method must be called to identify the method that is the
 * entry point to the assembly. Assemblies are saved as DLL by default,
 * unless SetEntryPoint requests the generation of a console application
 * or a Windows-based application.
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 */
class AssemblyBuilder(name: AssemblyName)
      extends Assembly(name)
      with ICustomAttributeSetter
      with Visitable
{
    //##########################################################################
    // public methods

    /**
     * Defines a dynamic module with the given name that will be saved
     * to the specified file. No symbol information is emitted.
     */
    def DefineDynamicModule(name: String, fileName: String): ModuleBuilder = {
	val module = new ModuleBuilder(name, fileName, "" + null, this)
	addModule(name, module)
	return module
    }

    /** Returns the dynamic module with the specified name. */
    def GetDynamicModule(name: String): ModuleBuilder = {
	return GetModule(name).asInstanceOf[ModuleBuilder]
    }

    /** Saves this dynamic assembly to disk. */
    @throws(classOf[IOException])
    def Save(fileName: String) {
    generatedFiles = scala.collection.mutable.ArrayBuffer.empty[String]
	ILPrinterVisitor.printAssembly(this, fileName)
    }

    @throws(classOf[IOException])
    def Save(destPath: String, sourceFilesPath: String) {
    generatedFiles = scala.collection.mutable.ArrayBuffer.empty[String]
    ILPrinterVisitor.printAssembly(this, destPath, sourceFilesPath)
    }

    /** Returns the list of generated files from calling Save(). */
    def GetGeneratedFiles(): Array[String] = {
       return generatedFiles.toArray // (new Array[String](generatedFiles.size())).asInstanceOf[Array[String]]
    }

    /** Sets the entry point for this dynamic assembly. */
    def SetEntryPoint(entryMethod: MethodInfo) {
	EntryPoint = entryMethod
    }

    /** Sets a custom attribute. */
    def SetCustomAttribute(constr: ConstructorInfo, value: Array[Byte]) {
	addCustomAttribute(constr, value)
    }

    //##########################################################################
    // protected members

    // all extern assemblies used in this assembly builder
    protected var externAssemblies = scala.collection.mutable.Set.empty[Assembly]

    // register an extern assembly
    protected def registerExternAssembly(assembly: Assembly) {
	externAssemblies += assembly
    }

    // get all extern Assemblies used in this Assembly Builder
    def getExternAssemblies(): Array[Assembly] = {
      externAssemblies = scala.collection.mutable.Set[Assembly]()
      val iter = Assembly.assemblies.values().iterator
      while (iter.hasNext) {
        externAssemblies += iter.next.asInstanceOf[Assembly]
    }
      externAssemblies -= this
      return externAssemblies.toArray
    }

    def loadModules() {}

    // contains list of generated .msil files after calling Save()
    var generatedFiles = scala.collection.mutable.ArrayBuffer.empty[String]

    //##########################################################################
    //##########################################################################

    /** the apply method for a visitor */
    @throws(classOf[IOException])
    def apply(v: Visitor) {
	v.caseAssemblyBuilder(this)
    }

    //##########################################################################
}

object AssemblyBuilderFactory {
    /**
     * Defines a dynamic assembly with the specified name.
     */
    def DefineDynamicAssembly(name: AssemblyName): AssemblyBuilder = {
    //Assembly.reset()
    return new AssemblyBuilder(name)
    }
}