summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/Module.java
blob: 8dd5e7119f21fb4d2ae66a2871803fd04be261b6 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * System.Reflection-like API for access to .NET assemblies (DLL & EXE)
 */


package ch.epfl.lamp.compiler.msil;

import java.util.Map;
import java.util.HashMap;

/**
 * Defines and represents a module. Get an instance of ModuleBuilder
 * by calling DefineDynamicModule
 * A module is a portable executable file of type .dll or .exe consisting
 * of one or more classes and interfaces. There may be multiple namespaces
 * contained in a single module, and a namespace may span multiple modules.
 * One or more modules deployed as a unit compose an assembly.
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 */
public abstract class Module extends CustomAttributeProvider {

    //##########################################################################
    // public fields

    /** String representing the name of the module with the path removed. */
    public final String Name;

    /** String representing the fully qualified name and path to this module. */
    public final String FullyQualifiedName;

    /** String representing the name of the module. */
    public String ScopeName;

    /** The Assembly the Module belongs to. */
    public final Assembly Assembly;

    //##########################################################################
    // constructor

    protected Module(String name, String filename,
		     String scopeName, Assembly assembly)
    {
	this.Name = name;
	this.FullyQualifiedName = filename;
	this.ScopeName = scopeName;
	this.Assembly = assembly;
    }

    //##########################################################################
    // public methods

    /** Returns the specified class, performing a case-sensitive search. */
    public Type GetType(String name) {
        initTypes();
	return (Type) typesMap.get(name);
    }

    /**
     * @return all the classes defined within this module.
     */
    public Type[] GetTypes() {
        initTypes();
	return (Type[]) types.clone();
    }

    /**
     * @return the global field with the specified name.
     */
    public FieldInfo GetField(String name) {
	for (int i = 0; i < fields.length; i++)
	    if (fields[i].Name.equals(name))
		return fields[i];
	return null;
    }

    /**
     * @return an array of the global fields of the module
     */
    public FieldInfo[] GetFields() {
	return (FieldInfo[]) fields.clone();
    }

    /**
     * @return - the global method with the specified name
     */
    public MethodInfo GetMethod(String name) {
	for (int i = 0; i < methods.length; i++)
	    if (methods[i].Name.equals(name))
		return methods[i];
	return null;
    }

    /**
     * @return - an array of all the global methods defined in this modules.
     */
    public MethodInfo[] GetMethods() {
	return (MethodInfo[]) methods.clone();
    }

    /**
     */
    public String toString() { return Name; }

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

    // all the types defined in this module
    protected final Map typesMap = new HashMap();

    // all the types defined in this module
    protected Type[] types;

    // the global fields of the module
    protected FieldInfo[] fields = FieldInfo.EMPTY_ARRAY;

    // the global methods of the module
    protected MethodInfo[] methods = MethodInfo.EMPTY_ARRAY;

    protected Type addType(Type type) {
	addType(type.FullName, type);
	Assembly.addType(type);
	return type;
    }

    protected Type addType(String name, Type type) {
	assert type!= null;
	typesMap.put(name, type);
	return type;
    }

    private boolean initTypes = true;
    protected final void initTypes() {
        if (initTypes) {
            loadTypes();
            initTypes = false;
        }
    }

    protected void loadTypes() {}

    private boolean initGlobals = true;
    protected final void initGlobals() {
        if (initGlobals) {
            loadGlobals();
            initGlobals = false;
        }
    }

    protected void loadGlobals() {}

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

}  // class Module