forked from smarr/som-java
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathClassGenerationContext.java
More file actions
164 lines (135 loc) · 5.47 KB
/
ClassGenerationContext.java
File metadata and controls
164 lines (135 loc) · 5.47 KB
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
156
157
158
159
160
161
162
163
164
/**
* Copyright (c) 2013 Stefan Marr, stefan.marr@vub.ac.be
* Copyright (c) 2009 Michael Haupt, michael.haupt@hpi.uni-potsdam.de
* Software Architecture Group, Hasso Plattner Institute, Potsdam, Germany
* http://www.hpi.uni-potsdam.de/swa/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package som.compiler;
import java.util.ArrayList;
import java.util.List;
import som.compiler.Parser.ParseError;
import som.vm.Universe;
import som.vmobjects.SArray;
import som.vmobjects.SClass;
import som.vmobjects.SInvokable;
import som.vmobjects.SSymbol;
public class ClassGenerationContext {
private final Universe universe;
public ClassGenerationContext(final Universe universe) {
this.universe = universe;
}
private SSymbol name;
private SSymbol superName;
private boolean classSide;
private final List<SSymbol> instanceFields = new ArrayList<SSymbol>();
private final List<SInvokable> instanceMethods = new ArrayList<SInvokable>();
private final List<SSymbol> classFields = new ArrayList<SSymbol>();
private final List<SInvokable> classMethods = new ArrayList<SInvokable>();
public SSymbol getName() {
return name;
}
public void setName(final SSymbol name) {
this.name = name;
}
public void setSuperName(final SSymbol superName) {
this.superName = superName;
}
public void setInstanceFieldsOfSuper(final SArray fieldNames) {
int numFields = fieldNames.getNumberOfIndexableFields();
for (int i = 0; i < numFields; i++) {
instanceFields.add((SSymbol) fieldNames.getIndexableField(i));
}
}
public void setClassFieldsOfSuper(final SArray fieldNames) {
int numFields = fieldNames.getNumberOfIndexableFields();
for (int i = 0; i < numFields; i++) {
classFields.add((SSymbol) fieldNames.getIndexableField(i));
}
}
public void addMethod(final som.vmobjects.SInvokable meth, final Parser parser) throws ParseError {
List<SInvokable> methods = classSide ? classMethods : instanceMethods;
for (SInvokable i : methods) {
if (i.getSignature() == meth.getSignature()) {
String msg = "A method with name " + meth.getSignature().getEmbeddedString() + " is already declared in " + name.getEmbeddedString();
throw new ParseError(msg, Symbol.NONE, parser);
}
}
if (classSide) {
classMethods.add(meth);
} else {
instanceMethods.add(meth);
}
}
public void startClassSide() {
classSide = true;
}
public void addField(final SSymbol field) {
if (classSide) {
classFields.add(field);
} else {
instanceFields.add(field);
}
}
public boolean hasField(final SSymbol field) {
return (isClassSide() ? classFields : instanceFields).contains(field);
}
public byte getFieldIndex(final SSymbol field) {
if (isClassSide()) {
return (byte) classFields.indexOf(field);
} else {
return (byte) instanceFields.indexOf(field);
}
}
public boolean isClassSide() {
return classSide;
}
public SClass assemble() throws ProgramDefinitionError {
// build class class name
String ccname = name.getEmbeddedString() + " class";
// Load the super class
SClass superClass = universe.loadClass(superName);
// Allocate the class of the resulting class
SClass resultClass = universe.newClass(universe.metaclassClass);
// Initialize the class of the resulting class
resultClass.setInstanceFields(universe.newArray(classFields));
resultClass.setInstanceInvokables(universe.newArray(classMethods));
resultClass.setName(universe.symbolFor(ccname));
SClass superMClass = superClass.getSOMClass();
resultClass.setSuperClass(superMClass);
// Allocate the resulting class
SClass result = universe.newClass(resultClass);
// Initialize the resulting class
result.setName(name);
result.setSuperClass(superClass);
result.setInstanceFields(universe.newArray(instanceFields));
result.setInstanceInvokables(universe.newArray(instanceMethods));
return result;
}
public SClass assembleSystemClass(final SClass systemClass) {
systemClass.setInstanceInvokables(universe.newArray(instanceMethods));
systemClass.setInstanceFields(universe.newArray(instanceFields));
// class-bound == class-instance-bound
SClass superMClass = systemClass.getSOMClass();
superMClass.setInstanceInvokables(universe.newArray(classMethods));
superMClass.setInstanceFields(universe.newArray(classFields));
return systemClass;
}
}