Browse Source

Moved Java compile-time tags to non-global state

Stan Hebben 6 years ago
parent
commit
f1bb71b3a1
51 changed files with 606 additions and 299 deletions
  1. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/WhitespaceInfo.java
  2. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/WhitespacePostComment.java
  3. 3
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeTag.java
  4. 4
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java
  5. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/CasterMemberRef.java
  6. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/DefinitionMemberRef.java
  7. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java
  8. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ImplementationMemberRef.java
  9. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/IteratorMemberRef.java
  10. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java
  11. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/VariantOptionRef.java
  12. 0
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/scope/FunctionScope.java
  13. 1
    0
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java
  14. 2
    0
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompiler.java
  15. 1
    0
      IDE/src/main/java/org/openzen/zenscript/ide/host/local/LocalTarget.java
  16. 19
    10
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java
  17. 6
    5
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/CompilerUtils.java
  18. 35
    61
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  19. 4
    4
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachWriter.java
  20. 10
    17
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreDecrementVisitor.java
  21. 10
    15
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreIncrementVisitor.java
  22. 5
    4
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java
  23. 16
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java
  24. 8
    9
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaDefinitionVisitor.java
  25. 15
    13
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaMemberVisitor.java
  26. 1
    0
      JavaShared/build.gradle
  27. 17
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaBaseCompiler.java
  28. 4
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaClass.java
  29. 132
    0
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaCompiledModule.java
  30. 77
    1
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java
  31. 1
    4
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeDescriptorVisitor.java
  32. 1
    1
      JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeInternalNameVisitor.java
  33. 14
    12
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareClassMethodVisitor.java
  34. 22
    19
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionMemberVisitor.java
  35. 25
    18
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionVisitor.java
  36. 11
    5
      JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareExpansionMethodVisitor.java
  37. 35
    12
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java
  38. 1
    3
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaExpansionMemberCompiler.java
  39. 16
    7
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java
  40. 9
    3
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceCompiler.java
  41. 16
    15
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java
  42. 21
    4
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java
  43. 5
    6
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceImporter.java
  44. 1
    1
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceStatementFormatter.java
  45. 3
    3
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceSyntheticTypeGenerator.java
  46. 5
    0
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/scope/JavaSourceStatementScope.java
  47. 17
    24
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java
  48. 6
    2
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java
  49. 1
    1
      Shared/src/main/java/org/openzen/zencode/shared/SourceFile.java
  50. 4
    0
      Shared/src/main/java/org/openzen/zencode/shared/Tag.java
  51. 3
    3
      Shared/src/main/java/org/openzen/zencode/shared/Taggable.java

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/WhitespaceInfo.java View File

7
 
7
 
8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
 import java.util.List;
9
 import java.util.List;
10
+import org.openzen.zencode.shared.Tag;
10
 import stdlib.Strings;
11
 import stdlib.Strings;
11
 
12
 
12
 /**
13
 /**
13
  *
14
  *
14
  * @author Hoofdgebruiker
15
  * @author Hoofdgebruiker
15
  */
16
  */
16
-public class WhitespaceInfo {
17
+public class WhitespaceInfo implements Tag {
17
 	public static WhitespaceInfo from(String whitespaceBefore, String lineAfter, boolean skipLineBefore) {
18
 	public static WhitespaceInfo from(String whitespaceBefore, String lineAfter, boolean skipLineBefore) {
18
 		int numNewLines = 0;
19
 		int numNewLines = 0;
19
 		for (char c : whitespaceBefore.toCharArray())
20
 		for (char c : whitespaceBefore.toCharArray())

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/WhitespacePostComment.java View File

7
 
7
 
8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
 import java.util.List;
9
 import java.util.List;
10
+import org.openzen.zencode.shared.Tag;
10
 import stdlib.Strings;
11
 import stdlib.Strings;
11
 
12
 
12
 /**
13
 /**
13
  *
14
  *
14
  * @author Hoofdgebruiker
15
  * @author Hoofdgebruiker
15
  */
16
  */
16
-public class WhitespacePostComment {
17
+public class WhitespacePostComment implements Tag {
17
 	public static WhitespacePostComment fromWhitespace(String whitespace) {
18
 	public static WhitespacePostComment fromWhitespace(String whitespace) {
18
 		List<String> comments = new ArrayList<>();
19
 		List<String> comments = new ArrayList<>();
19
 		for (String line : Strings.split(whitespace, '\n')) {
20
 		for (String line : Strings.split(whitespace, '\n')) {

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeTag.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.annotations;
6
 package org.openzen.zenscript.codemodel.annotations;
7
 
7
 
8
+import org.openzen.zencode.shared.Tag;
9
+
8
 /**
10
 /**
9
  *
11
  *
10
  * @author Hoofdgebruiker
12
  * @author Hoofdgebruiker
11
  */
13
  */
12
-public class NativeTag {
14
+public class NativeTag implements Tag {
13
 	public final String value;
15
 	public final String value;
14
 	
16
 	
15
 	public NativeTag(String value) {
17
 	public NativeTag(String value) {

+ 4
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java View File

6
 package org.openzen.zenscript.codemodel.member;
6
 package org.openzen.zenscript.codemodel.member;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.AccessScope;
10
 import org.openzen.zenscript.codemodel.AccessScope;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
43
 	
44
 	
44
 	public <C, R> R accept(C context, MemberVisitorWithContext<C, R> visitor);
45
 	public <C, R> R accept(C context, MemberVisitorWithContext<C, R> visitor);
45
 	
46
 	
46
-	public <T> T getTag(Class<T> tag);
47
+	public <T extends Tag> T getTag(Class<T> tag);
47
 	
48
 	
48
-	public <T> void setTag(Class<T> tag, T value);
49
+	public <T extends Tag> void setTag(Class<T> tag, T value);
49
 
50
 
50
-	<T> boolean hasTag(Class<T> tag);
51
+	<T extends Tag> boolean hasTag(Class<T> tag);
51
 	
52
 	
52
 	DefinitionMemberRef getOverrides();
53
 	DefinitionMemberRef getOverrides();
53
 
54
 

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/CasterMemberRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.Modifiers;
11
 import org.openzen.zenscript.codemodel.Modifiers;
11
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
12
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
46
 	}
47
 	}
47
 
48
 
48
 	@Override
49
 	@Override
49
-	public <T> T getTag(Class<T> type) {
50
+	public <T extends Tag> T getTag(Class<T> type) {
50
 		return member.getTag(type);
51
 		return member.getTag(type);
51
 	}
52
 	}
52
 	
53
 	

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/DefinitionMemberRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
12
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
20
 	
21
 	
21
 	String describe();
22
 	String describe();
22
 	
23
 	
23
-	default <T> T getTag(Class<T> type) {
24
+	default <T extends Tag> T getTag(Class<T> type) {
24
 		return getTarget().getTag(type);
25
 		return getTarget().getTag(type);
25
 	}
26
 	}
26
 	
27
 	
27
-	default boolean hasTag(Class<?> type) {
28
+	default <T extends Tag> boolean hasTag(Class<T> type) {
28
 		return getTarget().hasTag(type);
29
 		return getTarget().hasTag(type);
29
 	}
30
 	}
30
 	
31
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.CompareType;
10
 import org.openzen.zenscript.codemodel.CompareType;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
91
 	}
92
 	}
92
 	
93
 	
93
 	@Override
94
 	@Override
94
-	public <T> T getTag(Class<T> cls) {
95
+	public <T extends Tag> T getTag(Class<T> cls) {
95
 		return target.getTag(cls);
96
 		return target.getTag(cls);
96
 	}
97
 	}
97
 	
98
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ImplementationMemberRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
12
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
43
 	}
44
 	}
44
 
45
 
45
 	@Override
46
 	@Override
46
-	public <T> T getTag(Class<T> type) {
47
+	public <T extends Tag> T getTag(Class<T> type) {
47
 		return member.getTag(type);
48
 		return member.getTag(type);
48
 	}
49
 	}
49
 
50
 

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/IteratorMemberRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
11
 import org.openzen.zenscript.codemodel.member.IteratorMember;
12
 import org.openzen.zenscript.codemodel.member.IteratorMember;
43
 	}
44
 	}
44
 
45
 
45
 	@Override
46
 	@Override
46
-	public <T> T getTag(Class<T> type) {
47
+	public <T extends Tag> T getTag(Class<T> type) {
47
 		return target.getTag(type);
48
 		return target.getTag(type);
48
 	}
49
 	}
49
 	
50
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java View File

6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
9
+import org.openzen.zencode.shared.Tag;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.GenericMapper;
11
 import org.openzen.zenscript.codemodel.GenericMapper;
11
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
12
 import org.openzen.zenscript.codemodel.annotations.MemberAnnotation;
66
 	}
67
 	}
67
 
68
 
68
 	@Override
69
 	@Override
69
-	public final <T> T getTag(Class<T> type) {
70
+	public final <T extends Tag> T getTag(Class<T> type) {
70
 		return member.getTag(type);
71
 		return member.getTag(type);
71
 	}
72
 	}
72
 	
73
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/VariantOptionRef.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.member.ref;
6
 package org.openzen.zenscript.codemodel.member.ref;
7
 
7
 
8
+import org.openzen.zencode.shared.Tag;
8
 import org.openzen.zenscript.codemodel.definition.VariantDefinition;
9
 import org.openzen.zenscript.codemodel.definition.VariantDefinition;
9
 import org.openzen.zenscript.codemodel.type.StoredType;
10
 import org.openzen.zenscript.codemodel.type.StoredType;
10
 
11
 
31
 		return types[index];
32
 		return types[index];
32
 	}
33
 	}
33
 	
34
 	
34
-	public <T> T getTag(Class<T> type) {
35
+	public <T extends Tag> T getTag(Class<T> type) {
35
 		return option.getTag(type);
36
 		return option.getTag(type);
36
 	}
37
 	}
37
 
38
 

+ 0
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/scope/FunctionScope.java View File

38
 		this.outer = outer;
38
 		this.outer = outer;
39
 		this.header = header;
39
 		this.header = header;
40
 		this.thisType = outer.getThisType() == null || header.storage == null ? outer.getThisType() : outer.getThisType().type.stored(header.storage);
40
 		this.thisType = outer.getThisType() == null || header.storage == null ? outer.getThisType() : outer.getThisType().type.stored(header.storage);
41
-		if (header.storage != null)
42
-			System.out.println("Storage is " + header.storage);
43
 		
41
 		
44
 		if (outer.getLocalTypeParameters() == null)
42
 		if (outer.getLocalTypeParameters() == null)
45
 			throw new NullPointerException();
43
 			throw new NullPointerException();

+ 1
- 0
CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java View File

136
 		if (state != State.VALIDATED)
136
 		if (state != State.VALIDATED)
137
 			throw new IllegalStateException("Module is not yet validated");
137
 			throw new IllegalStateException("Module is not yet validated");
138
 		
138
 		
139
+		compiler.addModule(this);
139
 		for (HighLevelDefinition definition : definitions.getAll()) {
140
 		for (HighLevelDefinition definition : definitions.getAll()) {
140
 			compiler.addDefinition(definition, this);
141
 			compiler.addDefinition(definition, this);
141
 		}
142
 		}

+ 2
- 0
CompilerShared/src/main/java/org/openzen/zenscript/compiler/ZenCodeCompiler.java View File

13
  * @author Hoofdgebruiker
13
  * @author Hoofdgebruiker
14
  */
14
  */
15
 public interface ZenCodeCompiler {
15
 public interface ZenCodeCompiler {
16
+	void addModule(SemanticModule module);
17
+	
16
 	void addDefinition(HighLevelDefinition definition, SemanticModule module);
18
 	void addDefinition(HighLevelDefinition definition, SemanticModule module);
17
 	
19
 	
18
 	void addScriptBlock(ScriptBlock script);
20
 	void addScriptBlock(ScriptBlock script);

+ 1
- 0
IDE/src/main/java/org/openzen/zenscript/ide/host/local/LocalTarget.java View File

104
 			stdlib.validate(validationLogger);
104
 			stdlib.validate(validationLogger);
105
 			if (!stdlib.isValid())
105
 			if (!stdlib.isValid())
106
 				return compiler;
106
 				return compiler;
107
+			
107
 			stdlib.compile(compiler);
108
 			stdlib.compile(compiler);
108
 			compiledModules.add(stdlib.name);
109
 			compiledModules.add(stdlib.name);
109
 			
110
 			

+ 19
- 10
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

7
 
7
 
8
 import java.io.File;
8
 import java.io.File;
9
 import java.util.ArrayList;
9
 import java.util.ArrayList;
10
-
10
+import java.util.HashMap;
11
+import java.util.List;
12
+import java.util.Map;
11
 import org.objectweb.asm.ClassWriter;
13
 import org.objectweb.asm.ClassWriter;
12
 import org.objectweb.asm.Opcodes;
14
 import org.objectweb.asm.Opcodes;
13
 import org.openzen.zencode.shared.SourceFile;
15
 import org.openzen.zencode.shared.SourceFile;
14
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
16
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
15
 import org.openzen.zenscript.codemodel.ScriptBlock;
17
 import org.openzen.zenscript.codemodel.ScriptBlock;
16
 import org.openzen.zenscript.codemodel.statement.Statement;
18
 import org.openzen.zenscript.codemodel.statement.Statement;
19
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
17
 import org.openzen.zenscript.compiler.SemanticModule;
20
 import org.openzen.zenscript.compiler.SemanticModule;
18
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
21
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
19
 import org.openzen.zenscript.javabytecode.compiler.JavaClassWriter;
22
 import org.openzen.zenscript.javabytecode.compiler.JavaClassWriter;
21
 import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
24
 import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
22
 import org.openzen.zenscript.javabytecode.compiler.JavaWriter;
25
 import org.openzen.zenscript.javabytecode.compiler.JavaWriter;
23
 import org.openzen.zenscript.javabytecode.compiler.definitions.JavaDefinitionVisitor;
26
 import org.openzen.zenscript.javabytecode.compiler.definitions.JavaDefinitionVisitor;
24
-
25
-import java.util.HashMap;
26
-import java.util.List;
27
-import java.util.Map;
28
-import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
27
+import org.openzen.zenscript.javashared.JavaBaseCompiler;
29
 import org.openzen.zenscript.javashared.JavaClass;
28
 import org.openzen.zenscript.javashared.JavaClass;
29
+import org.openzen.zenscript.javashared.JavaContext;
30
 import org.openzen.zenscript.javashared.JavaMethod;
30
 import org.openzen.zenscript.javashared.JavaMethod;
31
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionMemberVisitor;
31
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionMemberVisitor;
32
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
32
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
34
 /**
34
 /**
35
  * @author Hoofdgebruiker
35
  * @author Hoofdgebruiker
36
  */
36
  */
37
-public class JavaCompiler implements ZenCodeCompiler {
37
+public class JavaCompiler extends JavaBaseCompiler implements ZenCodeCompiler {
38
 	private final JavaModule target;
38
 	private final JavaModule target;
39
 	private final Map<String, JavaScriptFile> scriptBlocks = new HashMap<>();
39
 	private final Map<String, JavaScriptFile> scriptBlocks = new HashMap<>();
40
 	private final JavaClassWriter scriptsClassWriter;
40
 	private final JavaClassWriter scriptsClassWriter;
58
 		scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
58
 		scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
59
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
59
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
60
 	}
60
 	}
61
+	
62
+	public JavaContext getContext() {
63
+		return context;
64
+	}
65
+	
66
+	@Override
67
+	public void addModule(SemanticModule module) {
68
+		context.addModule(module.module);
69
+	}
61
 
70
 
62
 	@Override
71
 	@Override
63
 	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
72
 	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
64
-		JavaPrepareDefinitionVisitor preparer = new JavaPrepareDefinitionVisitor(definition.position.getFilename(), null);
73
+		JavaPrepareDefinitionVisitor preparer = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), definition.position.getFilename(), null);
65
 		definition.accept(preparer);
74
 		definition.accept(preparer);
66
 		
75
 		
67
 		definitions.add(definition);
76
 		definitions.add(definition);
116
 		finished = true;
125
 		finished = true;
117
 		
126
 		
118
 		for (HighLevelDefinition definition : definitions) {
127
 		for (HighLevelDefinition definition : definitions) {
119
-			JavaPrepareDefinitionMemberVisitor memberPreparer = new JavaPrepareDefinitionMemberVisitor(context);
128
+			JavaPrepareDefinitionMemberVisitor memberPreparer = new JavaPrepareDefinitionMemberVisitor(context, context.getJavaModule(definition.module));
120
 			definition.accept(memberPreparer);
129
 			definition.accept(memberPreparer);
121
 		}
130
 		}
122
 		
131
 		
140
 			JavaMethod method = JavaMethod.getStatic(new JavaClass(script.pkg.fullName, className, JavaClass.Kind.CLASS), methodName, "()V", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
149
 			JavaMethod method = JavaMethod.getStatic(new JavaClass(script.pkg.fullName, className, JavaClass.Kind.CLASS), methodName, "()V", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
141
 			scriptFile.scriptMethods.add(method);
150
 			scriptFile.scriptMethods.add(method);
142
 
151
 
143
-			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, new JavaWriter(visitor, method, null, null, null));
152
+			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, context.getJavaModule(script.module), new JavaWriter(visitor, method, null, null, null));
144
 			statementVisitor.start();
153
 			statementVisitor.start();
145
 			for (Statement statement : script.statements) {
154
 			for (Statement statement : script.statements) {
146
 				statement.accept(statementVisitor);
155
 				statement.accept(statementVisitor);

+ 6
- 5
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/CompilerUtils.java View File

13
 import org.openzen.zenscript.javashared.JavaParameterInfo;
13
 import org.openzen.zenscript.javashared.JavaParameterInfo;
14
 
14
 
15
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
15
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
16
+import org.openzen.zenscript.javashared.JavaCompiledModule;
16
 
17
 
17
 public class CompilerUtils {
18
 public class CompilerUtils {
18
 	private CompilerUtils() {}
19
 	private CompilerUtils() {}
39
 		return out;
40
 		return out;
40
 	}
41
 	}
41
 
42
 
42
-    public static void tagMethodParameters(JavaBytecodeContext context, FunctionHeader header, boolean isStatic) {
43
+    public static void tagMethodParameters(JavaBytecodeContext context, JavaCompiledModule module, FunctionHeader header, boolean isStatic) {
43
         for (int i = 0; i < header.parameters.length; i++) {
44
         for (int i = 0; i < header.parameters.length; i++) {
44
             FunctionParameter parameter = header.parameters[i];
45
             FunctionParameter parameter = header.parameters[i];
45
             String parameterType = context.getDescriptor(parameter.type);
46
             String parameterType = context.getDescriptor(parameter.type);
46
-            parameter.setTag(JavaParameterInfo.class, new JavaParameterInfo(isStatic ? i : i + 1, parameterType));
47
+            module.setParameterInfo(parameter, new JavaParameterInfo(isStatic ? i : i + 1, parameterType));
47
         }
48
         }
48
     }
49
     }
49
 
50
 
50
-    public static void tagConstructorParameters(JavaBytecodeContext context, FunctionHeader header, boolean isEnum) {
51
+    public static void tagConstructorParameters(JavaBytecodeContext context, JavaCompiledModule module, FunctionHeader header, boolean isEnum) {
51
         for (int i = 0; i < header.parameters.length; i++) {
52
         for (int i = 0; i < header.parameters.length; i++) {
52
             FunctionParameter parameter = header.parameters[i];
53
             FunctionParameter parameter = header.parameters[i];
53
             String parameterType = context.getDescriptor(parameter.type);
54
             String parameterType = context.getDescriptor(parameter.type);
54
-            parameter.setTag(JavaParameterInfo.class, new JavaParameterInfo(isEnum ? i + 3 : i + 1, parameterType));
55
+			module.setParameterInfo(parameter, new JavaParameterInfo(isEnum ? i + 3 : i + 1, parameterType));
55
         }
56
         }
56
     }
57
     }
57
 
58
 
58
     public static void writeDefaultFieldInitializers(JavaBytecodeContext context, JavaWriter constructorWriter, HighLevelDefinition definition, boolean staticFields) {
59
     public static void writeDefaultFieldInitializers(JavaBytecodeContext context, JavaWriter constructorWriter, HighLevelDefinition definition, boolean staticFields) {
59
-        JavaExpressionVisitor expressionVisitor = new JavaExpressionVisitor(context, constructorWriter);
60
+        JavaExpressionVisitor expressionVisitor = new JavaExpressionVisitor(context, context.getJavaModule(definition.module), constructorWriter);
60
         for (final IDefinitionMember definitionMember : definition.members) {
61
         for (final IDefinitionMember definitionMember : definition.members) {
61
             if (!(definitionMember instanceof FieldMember))
62
             if (!(definitionMember instanceof FieldMember))
62
                 continue;
63
                 continue;

+ 35
- 61
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

11
 import org.openzen.zenscript.codemodel.CompareType;
11
 import org.openzen.zenscript.codemodel.CompareType;
12
 import org.openzen.zenscript.codemodel.expression.*;
12
 import org.openzen.zenscript.codemodel.expression.*;
13
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
13
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
14
-import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
15
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
14
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
16
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
15
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
17
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
16
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
26
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
25
 import org.openzen.zenscript.codemodel.type.storage.StorageTag;
27
 import org.openzen.zenscript.codemodel.type.storage.UniqueStorageTag;
26
 import org.openzen.zenscript.codemodel.type.storage.UniqueStorageTag;
28
 import org.openzen.zenscript.javashared.JavaClass;
27
 import org.openzen.zenscript.javashared.JavaClass;
28
+import org.openzen.zenscript.javashared.JavaCompiledModule;
29
 import org.openzen.zenscript.javashared.JavaField;
29
 import org.openzen.zenscript.javashared.JavaField;
30
 import org.openzen.zenscript.javashared.JavaMethod;
30
 import org.openzen.zenscript.javashared.JavaMethod;
31
 import org.openzen.zenscript.javashared.JavaTypeUtils;
31
 import org.openzen.zenscript.javashared.JavaTypeUtils;
155
 	protected final JavaWriter javaWriter;
155
 	protected final JavaWriter javaWriter;
156
 	private final JavaCapturedExpressionVisitor capturedExpressionVisitor = new JavaCapturedExpressionVisitor(this);
156
 	private final JavaCapturedExpressionVisitor capturedExpressionVisitor = new JavaCapturedExpressionVisitor(this);
157
 	private final JavaBytecodeContext context;
157
 	private final JavaBytecodeContext context;
158
+	private final JavaCompiledModule module;
158
 
159
 
159
-    public JavaExpressionVisitor(JavaBytecodeContext context, JavaWriter javaWriter) {
160
+    public JavaExpressionVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaWriter javaWriter) {
160
         this.javaWriter = javaWriter;
161
         this.javaWriter = javaWriter;
161
 		this.context = context;
162
 		this.context = context;
163
+		this.module = module;
162
     }
164
     }
163
 
165
 
164
     @Override
166
     @Override
1427
 	public Void visitConst(ConstExpression expression) {
1429
 	public Void visitConst(ConstExpression expression) {
1428
 		BuiltinID builtin = expression.constant.member.builtin;
1430
 		BuiltinID builtin = expression.constant.member.builtin;
1429
 		if (builtin == null) {
1431
 		if (builtin == null) {
1430
-			if (!checkAndGetFieldInfo(expression.constant, true))
1431
-				throw new IllegalStateException("Call target has no field info!");
1432
-
1432
+			javaWriter.getStaticField(context.getJavaField(expression.constant));
1433
 			return null;
1433
 			return null;
1434
 		}
1434
 		}
1435
 
1435
 
1511
 				break;
1511
 				break;
1512
 			case ENUM_VALUES: {
1512
 			case ENUM_VALUES: {
1513
 				DefinitionTypeID type = (DefinitionTypeID) expression.type.type;
1513
 				DefinitionTypeID type = (DefinitionTypeID) expression.type.type;
1514
-				JavaClass cls = type.definition.getTag(JavaClass.class);
1514
+				JavaClass cls = context.getJavaClass(type.definition);
1515
 				javaWriter.invokeStatic(JavaMethod.getNativeStatic(cls, "values", "()[L" + cls.internalName + ";"));
1515
 				javaWriter.invokeStatic(JavaMethod.getNativeStatic(cls, "values", "()[L" + cls.internalName + ";"));
1516
 				break;
1516
 				break;
1517
 			}
1517
 			}
1651
 
1651
 
1652
 	@Override
1652
 	@Override
1653
 	public Void visitFunction(FunctionExpression expression) {
1653
 	public Void visitFunction(FunctionExpression expression) {
1654
-		CompilerUtils.tagMethodParameters(context, expression.header, false);
1654
+		CompilerUtils.tagMethodParameters(context, module, expression.header, false);
1655
 
1655
 
1656
-        if (expression.header.parameters.length == 0 && expression.body instanceof ReturnStatement && expression.body.hasTag(MatchExpression.class) && expression.closure.captures.isEmpty()) {
1656
+        /*if (expression.header.parameters.length == 0 && expression.body instanceof ReturnStatement && expression.body.hasTag(MatchExpression.class) && expression.closure.captures.isEmpty()) {
1657
             ((ReturnStatement) expression.body).value.accept(this);
1657
             ((ReturnStatement) expression.body).value.accept(this);
1658
             return null;
1658
             return null;
1659
-        }
1659
+        }*/
1660
 		
1660
 		
1661
 		final String descriptor = context.getMethodDescriptor(expression.header);
1661
 		final String descriptor = context.getMethodDescriptor(expression.header);
1662
         final String signature = context.getMethodSignature(expression.header);
1662
         final String signature = context.getMethodSignature(expression.header);
1702
         functionWriter.start();
1702
         functionWriter.start();
1703
 
1703
 
1704
 
1704
 
1705
-		final JavaStatementVisitor CSV = new JavaStatementVisitor(context, new JavaExpressionVisitor(context, functionWriter) {
1705
+		final JavaStatementVisitor CSV = new JavaStatementVisitor(context, new JavaExpressionVisitor(context, module, functionWriter) {
1706
 			@Override
1706
 			@Override
1707
 			public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
1707
 			public Void visitGetLocalVariable(GetLocalVariableExpression varExpression) {
1708
 				final int position = calculateMemberPosition(varExpression, expression);
1708
 				final int position = calculateMemberPosition(varExpression, expression);
1772
 	@Override
1772
 	@Override
1773
 	public Void visitGetField(GetFieldExpression expression) {
1773
 	public Void visitGetField(GetFieldExpression expression) {
1774
 		expression.accept(this);
1774
 		expression.accept(this);
1775
-		if (!checkAndGetFieldInfo(expression.field, false))
1776
-			throw new IllegalStateException("Missing field info on a field member!");
1775
+		getField(expression.field);
1777
 		return null;
1776
 		return null;
1778
 	}
1777
 	}
1779
 
1778
 
1780
 	@Override
1779
 	@Override
1781
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
1780
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
1782
-		JavaParameterInfo parameter = expression.parameter.getTag(JavaParameterInfo.class);
1781
+		JavaParameterInfo parameter = module.getParameterInfo(expression.parameter);
1783
 
1782
 
1784
 		if (parameter == null)
1783
 		if (parameter == null)
1785
 			throw new RuntimeException(expression.position.toString() + ": Could not resolve lambda parameter" + expression.parameter);
1784
 			throw new RuntimeException(expression.position.toString() + ": Could not resolve lambda parameter" + expression.parameter);
1791
 	@Override
1790
 	@Override
1792
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
1791
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
1793
 		final Label label = new Label();
1792
 		final Label label = new Label();
1794
-		final JavaLocalVariableInfo tag = expression.variable.variable.getTag(JavaLocalVariableInfo.class);
1795
-		if (tag == null)
1796
-			throw new RuntimeException("Missing tag @ " + expression.position);
1793
+		final JavaLocalVariableInfo tag = javaWriter.getLocalVariable(expression.variable.variable);
1797
 		
1794
 		
1798
 		tag.end = label;
1795
 		tag.end = label;
1799
 		javaWriter.load(tag.type, tag.local);
1796
 		javaWriter.load(tag.type, tag.local);
1805
 	public Void visitGetMatchingVariantField(GetMatchingVariantField expression) {
1802
 	public Void visitGetMatchingVariantField(GetMatchingVariantField expression) {
1806
 		javaWriter.loadObject(0);
1803
 		javaWriter.loadObject(0);
1807
 		final StoredType type = expression.value.option.getParameterType(expression.index);
1804
 		final StoredType type = expression.value.option.getParameterType(expression.index);
1808
-		final JavaVariantOption tag = expression.value.option.getTag(JavaVariantOption.class);
1805
+		final JavaVariantOption tag = context.getJavaVariantOption(expression.value.option);
1809
 		javaWriter.checkCast(tag.variantOptionClass.internalName);
1806
 		javaWriter.checkCast(tag.variantOptionClass.internalName);
1810
 		javaWriter.getField(new JavaField(tag.variantOptionClass, "field" + expression.index, context.getDescriptor(type)));
1807
 		javaWriter.getField(new JavaField(tag.variantOptionClass, "field" + expression.index, context.getDescriptor(type)));
1811
 		return null;
1808
 		return null;
1813
 
1810
 
1814
 	@Override
1811
 	@Override
1815
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
1812
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
1816
-		if (!checkAndGetFieldInfo(expression.field, true))
1817
-			throw new IllegalStateException("Missing field info on a field member!");
1813
+		javaWriter.getStaticField(context.getJavaField(expression.field));
1818
 		return null;
1814
 		return null;
1819
 	}
1815
 	}
1820
 
1816
 
1824
 		
1820
 		
1825
 		BuiltinID builtin = expression.getter.member.builtin;
1821
 		BuiltinID builtin = expression.getter.member.builtin;
1826
 		if (builtin == null) {
1822
 		if (builtin == null) {
1827
-			if (expression.getter.hasTag(JavaField.class)) {
1828
-				javaWriter.getField(expression.getter.getTag(JavaField.class));
1823
+			if (context.hasJavaField(expression.getter)) {
1824
+				javaWriter.getField(context.getJavaField(expression.getter));
1829
 				return null;
1825
 				return null;
1830
 			}
1826
 			}
1831
 			if (!checkAndExecuteMethodInfo(expression.getter, expression.type))
1827
 			if (!checkAndExecuteMethodInfo(expression.getter, expression.type))
2075
 		for (MatchExpression.Case aCase : expression.cases) {
2071
 		for (MatchExpression.Case aCase : expression.cases) {
2076
 			if (aCase.key instanceof VariantOptionSwitchValue) {
2072
 			if (aCase.key instanceof VariantOptionSwitchValue) {
2077
 				VariantOptionSwitchValue variantOptionSwitchValue = (VariantOptionSwitchValue) aCase.key;
2073
 				VariantOptionSwitchValue variantOptionSwitchValue = (VariantOptionSwitchValue) aCase.key;
2078
-				JavaVariantOption option = variantOptionSwitchValue.option.getTag(JavaVariantOption.class);
2074
+				JavaVariantOption option = context.getJavaVariantOption(variantOptionSwitchValue.option);
2079
 				javaWriter.invokeVirtual(JavaMethod.getNativeVirtual(option.variantClass, "getDenominator", "()I"));
2075
 				javaWriter.invokeVirtual(JavaMethod.getNativeVirtual(option.variantClass, "getDenominator", "()I"));
2080
 				break;
2076
 				break;
2081
 			}
2077
 			}
2132
     @Override
2128
     @Override
2133
     public Void visitNew(NewExpression expression) {
2129
     public Void visitNew(NewExpression expression) {
2134
 		// TODO: this code is incorrect!
2130
 		// TODO: this code is incorrect!
2135
-		JavaMethod method = expression.constructor.getTag(JavaMethod.class);
2131
+		JavaMethod method = context.getJavaMethod(expression.constructor);
2136
 
2132
 
2137
         final String type;
2133
         final String type;
2138
         if (expression.type.type instanceof DefinitionTypeID)
2134
         if (expression.type.type instanceof DefinitionTypeID)
2244
 	public Void visitSetField(SetFieldExpression expression) {
2240
 	public Void visitSetField(SetFieldExpression expression) {
2245
 		expression.target.accept(this);
2241
 		expression.target.accept(this);
2246
 		expression.value.accept(this);
2242
 		expression.value.accept(this);
2247
-		if (!checkAndPutFieldInfo(expression.field, false))
2248
-			throw new IllegalStateException("Missing field info on a field member!");
2243
+		putField(expression.field);
2249
 		return null;
2244
 		return null;
2250
 	}
2245
 	}
2251
 
2246
 
2252
     @Override
2247
     @Override
2253
     public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
2248
     public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
2254
         expression.value.accept(this);
2249
         expression.value.accept(this);
2255
-        JavaParameterInfo parameter = expression.parameter.getTag(JavaParameterInfo.class);
2250
+        JavaParameterInfo parameter = module.getParameterInfo(expression.parameter);
2256
         javaWriter.store(context.getType(expression.type), parameter.index);
2251
         javaWriter.store(context.getType(expression.type), parameter.index);
2257
         return null;
2252
         return null;
2258
     }
2253
     }
2262
 		expression.value.accept(this);
2257
 		expression.value.accept(this);
2263
 		Label label = new Label();
2258
 		Label label = new Label();
2264
 		javaWriter.label(label);
2259
 		javaWriter.label(label);
2265
-		final JavaLocalVariableInfo tag = expression.variable.variable.getTag(JavaLocalVariableInfo.class);
2260
+		final JavaLocalVariableInfo tag = javaWriter.getLocalVariable(expression.variable.variable);
2266
 		tag.end = label;
2261
 		tag.end = label;
2267
 
2262
 
2268
 		javaWriter.store(tag.type, tag.local);
2263
 		javaWriter.store(tag.type, tag.local);
2273
 	@Override
2268
 	@Override
2274
 	public Void visitSetStaticField(SetStaticFieldExpression expression) {
2269
 	public Void visitSetStaticField(SetStaticFieldExpression expression) {
2275
 		expression.value.accept(this);
2270
 		expression.value.accept(this);
2276
-		
2277
-		if (!checkAndPutFieldInfo(expression.field, true))
2278
-			throw new IllegalStateException("Missing field info on a field member!");
2279
-		
2271
+		javaWriter.putStaticField(context.getJavaField(expression.field));
2280
 		return null;
2272
 		return null;
2281
 	}
2273
 	}
2282
 
2274
 
2289
 	public Void visitStaticGetter(StaticGetterExpression expression) {
2281
 	public Void visitStaticGetter(StaticGetterExpression expression) {
2290
 		BuiltinID builtin = expression.getter.member.builtin;
2282
 		BuiltinID builtin = expression.getter.member.builtin;
2291
 		if (builtin == null) {
2283
 		if (builtin == null) {
2292
-			if (expression.getter.hasTag(JavaField.class)) {
2293
-				javaWriter.getStaticField(expression.getter.getTag(JavaField.class));
2284
+			if (context.hasJavaField(expression.getter)) {
2285
+				javaWriter.getStaticField(context.getJavaField(expression.getter));
2294
 				return null;
2286
 				return null;
2295
 			}
2287
 			}
2296
 			
2288
 			
2369
 				break;
2361
 				break;
2370
 			case ENUM_VALUES: {
2362
 			case ENUM_VALUES: {
2371
 				DefinitionTypeID type = (DefinitionTypeID) expression.type.type;
2363
 				DefinitionTypeID type = (DefinitionTypeID) expression.type.type;
2372
-				JavaClass cls = type.definition.getTag(JavaClass.class);
2364
+				JavaClass cls = context.getJavaClass(type.definition);
2373
 				javaWriter.invokeStatic(JavaMethod.getNativeStatic(cls, "values", "()[L" + cls.internalName + ";"));
2365
 				javaWriter.invokeStatic(JavaMethod.getNativeStatic(cls, "values", "()[L" + cls.internalName + ";"));
2374
 				break;
2366
 				break;
2375
 			}
2367
 			}
2441
 
2433
 
2442
 	@Override
2434
 	@Override
2443
 	public Void visitVariantValue(VariantValueExpression expression) {
2435
 	public Void visitVariantValue(VariantValueExpression expression) {
2444
-		JavaVariantOption tag = expression.option.getTag(JavaVariantOption.class);
2436
+		JavaVariantOption tag = context.getJavaVariantOption(expression.option);
2445
 		final String internalName = tag.variantOptionClass.internalName;
2437
 		final String internalName = tag.variantOptionClass.internalName;
2446
 		javaWriter.newObject(internalName);
2438
 		javaWriter.newObject(internalName);
2447
 		javaWriter.dup();
2439
 		javaWriter.dup();
2475
 
2467
 
2476
     //Will return true if a JavaMethodInfo.class tag exists, and will compile that tag
2468
     //Will return true if a JavaMethodInfo.class tag exists, and will compile that tag
2477
     private boolean checkAndExecuteMethodInfo(DefinitionMemberRef member, StoredType resultType) {
2469
     private boolean checkAndExecuteMethodInfo(DefinitionMemberRef member, StoredType resultType) {
2478
-        JavaMethod methodInfo = member.getTag(JavaMethod.class);
2470
+        JavaMethod methodInfo = context.getJavaMethod(member);
2479
         if (methodInfo == null)
2471
         if (methodInfo == null)
2480
             return false;
2472
             return false;
2481
 
2473
 
2491
     }
2483
     }
2492
 
2484
 
2493
     //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
2485
     //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
2494
-    public boolean checkAndPutFieldInfo(FieldMemberRef field, boolean isStatic) {
2495
-		JavaField fieldInfo = field.getTag(JavaField.class);
2496
-        if (fieldInfo == null)
2497
-            return false;
2498
-        //TODO Remove isStatic
2499
-        if (field.isStatic() || isStatic) {
2486
+    public void putField(FieldMemberRef field) {
2487
+		JavaField fieldInfo = context.getJavaField(field);
2488
+        if (field.isStatic()) {
2500
             getJavaWriter().putStaticField(fieldInfo);
2489
             getJavaWriter().putStaticField(fieldInfo);
2501
         } else {
2490
         } else {
2502
             getJavaWriter().putField(fieldInfo);
2491
             getJavaWriter().putField(fieldInfo);
2503
-        }
2504
-        return true;
2492
+		}
2505
     }
2493
     }
2506
 
2494
 
2507
-	public boolean checkAndGetFieldInfo(ConstMemberRef field, boolean isStatic) {
2508
-		final JavaField fieldInfo = field.getTag(JavaField.class);
2509
-		if (fieldInfo == null)
2510
-			return false;
2511
-
2512
-		getJavaWriter().getStaticField(fieldInfo);
2513
-		return true;
2514
-	}
2515
-
2516
-	public boolean checkAndGetFieldInfo(FieldMemberRef field, boolean isStatic) {
2517
-		final JavaField fieldInfo = field.getTag(JavaField.class);
2518
-		if (fieldInfo == null)
2519
-			return false;
2520
-
2521
-		//TODO Remove isStatic
2522
-		if (field.isStatic() || isStatic) {
2495
+	public void getField(FieldMemberRef field) {
2496
+		final JavaField fieldInfo = context.getJavaField(field);
2497
+		if (field.isStatic()) {
2523
 			getJavaWriter().getStaticField(fieldInfo);
2498
 			getJavaWriter().getStaticField(fieldInfo);
2524
 		} else {
2499
 		} else {
2525
 			getJavaWriter().getField(fieldInfo);
2500
 			getJavaWriter().getField(fieldInfo);
2526
 		}
2501
 		}
2527
-		return true;
2528
 	}
2502
 	}
2529
 }
2503
 }

+ 4
- 4
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachWriter.java View File

29
 		javaWriter.swap();
29
 		javaWriter.swap();
30
 		javaWriter.getField("zsynthetic/IntRange", "from", "I");
30
 		javaWriter.getField("zsynthetic/IntRange", "from", "I");
31
 
31
 
32
-		final int z = variables[0].variable.getTag(JavaLocalVariableInfo.class).local;
32
+		final int z = javaWriter.getLocalVariable(variables[0].variable).local;
33
 		javaWriter.storeInt(z);
33
 		javaWriter.storeInt(z);
34
 		javaWriter.label(startLabel);
34
 		javaWriter.label(startLabel);
35
 		javaWriter.dup();
35
 		javaWriter.dup();
41
 	}
41
 	}
42
 
42
 
43
 	public void visitArrayValueIterator() {
43
 	public void visitArrayValueIterator() {
44
-		handleArray(javaWriter.local(int.class), variables[0].variable.getTag(JavaLocalVariableInfo.class));
44
+		handleArray(javaWriter.local(int.class), javaWriter.getLocalVariable(variables[0].variable));
45
 	}
45
 	}
46
 
46
 
47
 	public void visitArrayKeyValueIterator() {
47
 	public void visitArrayKeyValueIterator() {
48
-		handleArray(variables[0].variable.getTag(JavaLocalVariableInfo.class).local, variables[1].variable.getTag(JavaLocalVariableInfo.class));
48
+		handleArray(javaWriter.getLocalVariable(variables[0].variable).local, javaWriter.getLocalVariable(variables[1].variable));
49
 	}
49
 	}
50
 
50
 
51
 	public void visitStringCharacterIterator() {
51
 	public void visitStringCharacterIterator() {
52
 		//TODO UNTESTED!
52
 		//TODO UNTESTED!
53
 		javaWriter.invokeSpecial("java/lang/String", "toCharArray()", "()[C");
53
 		javaWriter.invokeSpecial("java/lang/String", "toCharArray()", "()[C");
54
-		handleArray(javaWriter.local(int.class), variables[0].variable.getTag(JavaLocalVariableInfo.class));
54
+		handleArray(javaWriter.local(int.class), javaWriter.getLocalVariable(variables[0].variable));
55
 	}
55
 	}
56
 
56
 
57
 	private void handleArray(final int z, final JavaLocalVariableInfo arrayTypeInfo) {
57
 	private void handleArray(final int z, final JavaLocalVariableInfo arrayTypeInfo) {

+ 10
- 17
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreDecrementVisitor.java View File

8
 import org.objectweb.asm.Type;
8
 import org.objectweb.asm.Type;
9
 import org.openzen.zenscript.codemodel.expression.*;
9
 import org.openzen.zenscript.codemodel.expression.*;
10
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
10
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
-import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
12
-import org.openzen.zenscript.javashared.JavaParameterInfo;
11
+import org.openzen.zenscript.javashared.JavaCompiledModule;
13
 
12
 
14
 /**
13
 /**
15
  *
14
  *
18
 public class JavaPreDecrementVisitor implements ExpressionVisitor<Void> {
17
 public class JavaPreDecrementVisitor implements ExpressionVisitor<Void> {
19
 	private final JavaExpressionVisitor expressionCompiler;
18
 	private final JavaExpressionVisitor expressionCompiler;
20
 	private final JavaWriter javaWriter;
19
 	private final JavaWriter javaWriter;
20
+	private final JavaCompiledModule module;
21
 	private final JavaBytecodeContext context;
21
 	private final JavaBytecodeContext context;
22
 	
22
 	
23
-	public JavaPreDecrementVisitor(JavaBytecodeContext context, JavaExpressionVisitor expressionCompiler) {
23
+	public JavaPreDecrementVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaExpressionVisitor expressionCompiler) {
24
 		this.expressionCompiler = expressionCompiler;
24
 		this.expressionCompiler = expressionCompiler;
25
 		this.context = context;
25
 		this.context = context;
26
+		this.module = module;
26
 		javaWriter = expressionCompiler.getJavaWriter();
27
 		javaWriter = expressionCompiler.getJavaWriter();
27
 	}
28
 	}
28
 
29
 
200
 		javaWriter.dup();
201
 		javaWriter.dup();
201
 		javaWriter.store(objectType, local);
202
 		javaWriter.store(objectType, local);
202
 		
203
 		
203
-        if (!expressionCompiler.checkAndGetFieldInfo(expression.field, false))
204
-            throw new IllegalStateException("Missing field info on a field member!");
204
+        expressionCompiler.getField(expression.field);
205
 		
205
 		
206
 		javaWriter.iConst1();
206
 		javaWriter.iConst1();
207
 		javaWriter.iSub();
207
 		javaWriter.iSub();
208
 		
208
 		
209
 		javaWriter.load(objectType, local);
209
 		javaWriter.load(objectType, local);
210
-        if (!expressionCompiler.checkAndPutFieldInfo(expression.field, false))
211
-            throw new IllegalStateException("Missing field info on a field member!");
212
-		
210
+        expressionCompiler.putField(expression.field);
213
 		return null;
211
 		return null;
214
 	}
212
 	}
215
 
213
 
216
 	@Override
214
 	@Override
217
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
215
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
218
-		javaWriter.idec(expression.parameter.getTag(JavaParameterInfo.class).index);
216
+		javaWriter.idec(module.getParameterInfo(expression.parameter).index);
219
 		return null;
217
 		return null;
220
 	}
218
 	}
221
 	
219
 	
222
 	@Override
220
 	@Override
223
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
221
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
224
-		javaWriter.idec(expression.variable.variable.getTag(JavaLocalVariableInfo.class).local);
222
+		javaWriter.idec(javaWriter.getLocalVariable(expression.variable.variable).local);
225
 		return null;
223
 		return null;
226
 	}
224
 	}
227
 
225
 
232
 
230
 
233
 	@Override
231
 	@Override
234
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
232
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
235
-		if (!expressionCompiler.checkAndGetFieldInfo(expression.field, false))
236
-            throw new IllegalStateException("Missing field info on a field member!");
237
-		
233
+		expressionCompiler.getField(expression.field);
238
 		javaWriter.iConst1();
234
 		javaWriter.iConst1();
239
 		javaWriter.iAdd();
235
 		javaWriter.iAdd();
240
-		
241
-		if (!expressionCompiler.checkAndPutFieldInfo(expression.field, false))
242
-            throw new IllegalStateException("Missing field info on a field member!");
243
-		
236
+		expressionCompiler.putField(expression.field);
244
 		return null;
237
 		return null;
245
 	}
238
 	}
246
 
239
 

+ 10
- 15
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreIncrementVisitor.java View File

9
 import org.openzen.zenscript.codemodel.expression.*;
9
 import org.openzen.zenscript.codemodel.expression.*;
10
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
10
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
11
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
12
+import org.openzen.zenscript.javashared.JavaCompiledModule;
12
 import org.openzen.zenscript.javashared.JavaParameterInfo;
13
 import org.openzen.zenscript.javashared.JavaParameterInfo;
13
 
14
 
14
 /**
15
 /**
18
 public class JavaPreIncrementVisitor implements ExpressionVisitor<Void> {
19
 public class JavaPreIncrementVisitor implements ExpressionVisitor<Void> {
19
 	private final JavaExpressionVisitor expressionCompiler;
20
 	private final JavaExpressionVisitor expressionCompiler;
20
 	private final JavaBytecodeContext context;
21
 	private final JavaBytecodeContext context;
22
+	private final JavaCompiledModule module;
21
 	private final JavaWriter javaWriter;
23
 	private final JavaWriter javaWriter;
22
 	
24
 	
23
-	public JavaPreIncrementVisitor(JavaBytecodeContext context, JavaExpressionVisitor expressionCompiler) {
25
+	public JavaPreIncrementVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaExpressionVisitor expressionCompiler) {
24
 		this.expressionCompiler = expressionCompiler;
26
 		this.expressionCompiler = expressionCompiler;
25
 		this.context = context;
27
 		this.context = context;
28
+		this.module = module;
26
 		javaWriter = expressionCompiler.getJavaWriter();
29
 		javaWriter = expressionCompiler.getJavaWriter();
27
 	}
30
 	}
28
 
31
 
200
 		javaWriter.dup();
203
 		javaWriter.dup();
201
 		javaWriter.store(objectType, local);
204
 		javaWriter.store(objectType, local);
202
 		
205
 		
203
-        if (!expressionCompiler.checkAndGetFieldInfo(expression.field, false))
204
-            throw new IllegalStateException("Missing field info on a field member!");
206
+        expressionCompiler.getField(expression.field);
205
 		
207
 		
206
 		javaWriter.iConst1();
208
 		javaWriter.iConst1();
207
 		javaWriter.iAdd();
209
 		javaWriter.iAdd();
208
 		
210
 		
209
 		javaWriter.load(objectType, local);
211
 		javaWriter.load(objectType, local);
210
 		javaWriter.dupX1();
212
 		javaWriter.dupX1();
211
-        if (!expressionCompiler.checkAndPutFieldInfo(expression.field, false))
212
-            throw new IllegalStateException("Missing field info on a field member!");
213
-		
213
+        expressionCompiler.putField(expression.field);
214
 		return null;
214
 		return null;
215
 	}
215
 	}
216
 
216
 
217
 	@Override
217
 	@Override
218
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
218
 	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
219
-		JavaParameterInfo parameter = expression.parameter.getTag(JavaParameterInfo.class);
219
+		JavaParameterInfo parameter = module.getParameterInfo(expression.parameter);
220
 		javaWriter.iinc(parameter.index);
220
 		javaWriter.iinc(parameter.index);
221
 		javaWriter.load(parameter);
221
 		javaWriter.load(parameter);
222
 		return null;
222
 		return null;
224
 
224
 
225
 	@Override
225
 	@Override
226
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
226
 	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
227
-		JavaLocalVariableInfo localVariable = expression.variable.variable.getTag(JavaLocalVariableInfo.class);
227
+		JavaLocalVariableInfo localVariable = javaWriter.getLocalVariable(expression.variable.variable);
228
 		javaWriter.iinc(localVariable.local);
228
 		javaWriter.iinc(localVariable.local);
229
 		javaWriter.load(localVariable);
229
 		javaWriter.load(localVariable);
230
 		return null;
230
 		return null;
237
 
237
 
238
 	@Override
238
 	@Override
239
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
239
 	public Void visitGetStaticField(GetStaticFieldExpression expression) {
240
-		if (!expressionCompiler.checkAndGetFieldInfo(expression.field, false))
241
-            throw new IllegalStateException("Missing field info on a field member!");
242
-		
240
+		expressionCompiler.getField(expression.field);
243
 		javaWriter.iConst1();
241
 		javaWriter.iConst1();
244
 		javaWriter.iAdd();
242
 		javaWriter.iAdd();
245
 		javaWriter.dup();
243
 		javaWriter.dup();
246
-		
247
-		if (!expressionCompiler.checkAndPutFieldInfo(expression.field, false))
248
-            throw new IllegalStateException("Missing field info on a field member!");
249
-		
244
+		expressionCompiler.putField(expression.field);
250
 		return null;
245
 		return null;
251
 	}
246
 	}
252
 
247
 

+ 5
- 4
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java View File

9
 import java.util.List;
9
 import java.util.List;
10
 import org.openzen.zenscript.codemodel.type.StringTypeID;
10
 import org.openzen.zenscript.codemodel.type.StringTypeID;
11
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
12
+import org.openzen.zenscript.javashared.JavaCompiledModule;
12
 
13
 
13
 public class JavaStatementVisitor implements StatementVisitor<Boolean> {
14
 public class JavaStatementVisitor implements StatementVisitor<Boolean> {
14
     private final JavaWriter javaWriter;
15
     private final JavaWriter javaWriter;
18
     /**
19
     /**
19
      * @param javaWriter the method writer that compiles the statement
20
      * @param javaWriter the method writer that compiles the statement
20
      */
21
      */
21
-    public JavaStatementVisitor(JavaBytecodeContext context, JavaWriter javaWriter) {
22
+    public JavaStatementVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaWriter javaWriter) {
22
         this.javaWriter = javaWriter;
23
         this.javaWriter = javaWriter;
23
 		this.context = context;
24
 		this.context = context;
24
-        this.expressionVisitor = new JavaExpressionVisitor(context, javaWriter);
25
+        this.expressionVisitor = new JavaExpressionVisitor(context, module, javaWriter);
25
     }
26
     }
26
 
27
 
27
     public JavaStatementVisitor(JavaBytecodeContext context, JavaExpressionVisitor expressionVisitor) {
28
     public JavaStatementVisitor(JavaBytecodeContext context, JavaExpressionVisitor expressionVisitor) {
103
             final Label variableStart = new Label();
104
             final Label variableStart = new Label();
104
             final JavaLocalVariableInfo info = new JavaLocalVariableInfo(type, javaWriter.local(type), variableStart, variable.name);
105
             final JavaLocalVariableInfo info = new JavaLocalVariableInfo(type, javaWriter.local(type), variableStart, variable.name);
105
             info.end = end;
106
             info.end = end;
106
-            variable.variable.setTag(JavaLocalVariableInfo.class, info);
107
+			javaWriter.setLocalVariable(variable.variable, info);
107
             javaWriter.addVariableInfo(info);
108
             javaWriter.addVariableInfo(info);
108
         }
109
         }
109
 
110
 
311
 		final Label variableStart = new Label();
312
 		final Label variableStart = new Label();
312
 		javaWriter.label(variableStart);
313
 		javaWriter.label(variableStart);
313
 		final JavaLocalVariableInfo info = new JavaLocalVariableInfo(type, local, variableStart, statement.name);
314
 		final JavaLocalVariableInfo info = new JavaLocalVariableInfo(type, local, variableStart, statement.name);
314
-		statement.variable.setTag(JavaLocalVariableInfo.class, info);
315
+		javaWriter.setLocalVariable(statement.variable, info);
315
 		javaWriter.addVariableInfo(info);
316
 		javaWriter.addVariableInfo(info);
316
 		return false;
317
 		return false;
317
 	}
318
 	}

+ 16
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

10
 import org.objectweb.asm.commons.LocalVariablesSorter;
10
 import org.objectweb.asm.commons.LocalVariablesSorter;
11
 import org.openzen.zencode.shared.CodePosition;
11
 import org.openzen.zencode.shared.CodePosition;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.statement.VarStatement;
14
+import org.openzen.zenscript.codemodel.statement.VariableID;
13
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
15
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
14
 import org.openzen.zenscript.javashared.JavaParameterInfo;
16
 import org.openzen.zenscript.javashared.JavaParameterInfo;
15
 import org.openzen.zenscript.javashared.JavaClass;
17
 import org.openzen.zenscript.javashared.JavaClass;
32
     private boolean nameVariables = true;
34
     private boolean nameVariables = true;
33
     private int labelIndex = 1;
35
     private int labelIndex = 1;
34
     private Map<Label, String> labelNames = new HashMap<>();
36
     private Map<Label, String> labelNames = new HashMap<>();
35
-
37
+	private final Map<VariableID, JavaLocalVariableInfo> localVariables = new HashMap<>();
38
+	
36
     public JavaWriter(
39
     public JavaWriter(
37
             ClassVisitor visitor,
40
             ClassVisitor visitor,
38
             boolean nameVariables,
41
             boolean nameVariables,
58
     public JavaWriter(ClassVisitor visitor, JavaMethod method, HighLevelDefinition forDefinition, String signature, String[] exceptions, String... annotations) {
61
     public JavaWriter(ClassVisitor visitor, JavaMethod method, HighLevelDefinition forDefinition, String signature, String[] exceptions, String... annotations) {
59
         this(visitor, true, method, forDefinition, signature, exceptions, annotations);
62
         this(visitor, true, method, forDefinition, signature, exceptions, annotations);
60
     }
63
     }
64
+	
65
+	public void setLocalVariable(VariableID variable, JavaLocalVariableInfo info) {
66
+		localVariables.put(variable, info);
67
+	}
68
+	
69
+	public JavaLocalVariableInfo getLocalVariable(VariableID variable) {
70
+		JavaLocalVariableInfo result = localVariables.get(variable);
71
+		if (result == null)
72
+			throw new IllegalStateException("Local variable unknown");
73
+		
74
+		return result;
75
+	}
61
 
76
 
62
     public void enableDebug() {
77
     public void enableDebug() {
63
         debug = true;
78
         debug = true;

+ 8
- 9
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaDefinitionVisitor.java View File

45
 	public byte[] visitClass(ClassDefinition definition) {
45
 	public byte[] visitClass(ClassDefinition definition) {
46
         final String superTypeInternalName = definition.getSuperType() == null ? "java/lang/Object" : context.getInternalName(definition.getSuperType());
46
         final String superTypeInternalName = definition.getSuperType() == null ? "java/lang/Object" : context.getInternalName(definition.getSuperType());
47
 
47
 
48
-		JavaClass toClass = definition.getTag(JavaClass.class);
48
+		JavaClass toClass = context.getJavaClass(definition);
49
 		JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
49
 		JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
50
 
50
 
51
         //TODO: Calculate signature from generic parameters
51
         //TODO: Calculate signature from generic parameters
68
 
68
 
69
 	@Override
69
 	@Override
70
 	public byte[] visitInterface(InterfaceDefinition definition) {
70
 	public byte[] visitInterface(InterfaceDefinition definition) {
71
-		JavaClass toClass = definition.getTag(JavaClass.class);
71
+		JavaClass toClass = context.getJavaClass(definition);
72
 		ClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
72
 		ClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
73
 
73
 
74
 		//TODO: Calculate signature from generic parameters
74
 		//TODO: Calculate signature from generic parameters
95
 
95
 
96
 		ClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
96
 		ClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
97
 
97
 
98
-		JavaClass toClass = definition.getTag(JavaClass.class);
98
+		JavaClass toClass = context.getJavaClass(definition);
99
 		writer.visit(Opcodes.V1_8, Opcodes.ACC_ENUM | Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL, toClass.internalName, "Ljava/lang/Enum<L" + toClass.internalName + ";>;", superTypeInternalName, null);
99
 		writer.visit(Opcodes.V1_8, Opcodes.ACC_ENUM | Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL, toClass.internalName, "Ljava/lang/Enum<L" + toClass.internalName + ";>;", superTypeInternalName, null);
100
 
100
 
101
 		//Enum Stuff(required!)
101
 		//Enum Stuff(required!)
136
 
136
 
137
 	@Override
137
 	@Override
138
 	public byte[] visitFunction(FunctionDefinition definition) {
138
 	public byte[] visitFunction(FunctionDefinition definition) {
139
-		CompilerUtils.tagMethodParameters(context, definition.header, true);
139
+		CompilerUtils.tagMethodParameters(context, context.getJavaModule(definition.module), definition.header, true);
140
 
140
 
141
         final String signature = context.getMethodSignature(definition.header);
141
         final String signature = context.getMethodSignature(definition.header);
142
-
143
-		final JavaMethod method = definition.caller.getTag(JavaMethod.class);
142
+		final JavaMethod method = context.getJavaMethod(definition.caller);
144
 
143
 
145
 		final JavaWriter writer = new JavaWriter(outerWriter, true, method, definition, signature, null);
144
 		final JavaWriter writer = new JavaWriter(outerWriter, true, method, definition, signature, null);
146
-        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, writer);
145
+        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, context.getJavaModule(definition.module), writer);
147
         statementVisitor.start();
146
         statementVisitor.start();
148
 		boolean returns = definition.caller.body.accept(statementVisitor);
147
 		boolean returns = definition.caller.body.accept(statementVisitor);
149
 		if (!returns) {
148
 		if (!returns) {
171
 
170
 
172
 	@Override
171
 	@Override
173
 	public byte[] visitVariant(VariantDefinition variant) {
172
 	public byte[] visitVariant(VariantDefinition variant) {
174
-		final JavaClass toClass = variant.getTag(JavaClass.class);
173
+		final JavaClass toClass = context.getJavaClass(variant);
175
 		final JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
174
 		final JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
176
 
175
 
177
 		final String variantName = variant.name;
176
 		final String variantName = variant.name;
188
 		final List<VariantDefinition.Option> options = variant.options;
187
 		final List<VariantDefinition.Option> options = variant.options;
189
 		//Each option is one of the possible child classes
188
 		//Each option is one of the possible child classes
190
 		for (final VariantDefinition.Option option : options) {
189
 		for (final VariantDefinition.Option option : options) {
191
-			JavaVariantOption optionTag = option.getTag(JavaVariantOption.class);
190
+			JavaVariantOption optionTag = context.getJavaVariantOption(option);
192
 			final JavaClassWriter optionWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
191
 			final JavaClassWriter optionWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
193
 			final String optionClassName = variantName + "$" + option.name;
192
 			final String optionClassName = variantName + "$" + option.name;
194
 			JavaClassWriter.registerSuperClass(optionClassName, variantName);
193
 			JavaClassWriter.registerSuperClass(optionClassName, variantName);

+ 15
- 13
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaMemberVisitor.java View File

10
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
10
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
11
 import org.openzen.zenscript.codemodel.expression.Expression;
11
 import org.openzen.zenscript.codemodel.expression.Expression;
12
 import org.openzen.zenscript.codemodel.member.*;
12
 import org.openzen.zenscript.codemodel.member.*;
13
-import org.openzen.zenscript.javashared.JavaParameterInfo;
14
 import org.openzen.zenscript.javabytecode.compiler.*;
13
 import org.openzen.zenscript.javabytecode.compiler.*;
15
 
14
 
16
 import java.util.List;
15
 import java.util.List;
17
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
16
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
18
 import org.openzen.zenscript.javashared.JavaClass;
17
 import org.openzen.zenscript.javashared.JavaClass;
18
+import org.openzen.zenscript.javashared.JavaCompiledModule;
19
 import org.openzen.zenscript.javashared.JavaField;
19
 import org.openzen.zenscript.javashared.JavaField;
20
 import org.openzen.zenscript.javashared.JavaMethod;
20
 import org.openzen.zenscript.javashared.JavaMethod;
21
 
21
 
26
     private final HighLevelDefinition definition;
26
     private final HighLevelDefinition definition;
27
     private final JavaStatementVisitor clinitStatementVisitor;
27
     private final JavaStatementVisitor clinitStatementVisitor;
28
     private EnumDefinition enumDefinition = null;
28
     private EnumDefinition enumDefinition = null;
29
+	private final JavaCompiledModule javaModule;
29
 
30
 
30
     public JavaMemberVisitor(JavaBytecodeContext context, ClassWriter writer, JavaClass toClass, HighLevelDefinition definition) {
31
     public JavaMemberVisitor(JavaBytecodeContext context, ClassWriter writer, JavaClass toClass, HighLevelDefinition definition) {
31
         this.writer = writer;
32
         this.writer = writer;
32
         this.toClass = toClass;
33
         this.toClass = toClass;
33
         this.definition = definition;
34
         this.definition = definition;
34
 		this.context = context;
35
 		this.context = context;
36
+		javaModule = context.getJavaModule(definition.module);
35
 
37
 
36
         final JavaWriter javaWriter = new JavaWriter(writer, new JavaMethod(toClass, JavaMethod.Kind.STATICINIT, "<clinit>", true, "()V", 0, false), definition, null, null);
38
         final JavaWriter javaWriter = new JavaWriter(writer, new JavaMethod(toClass, JavaMethod.Kind.STATICINIT, "<clinit>", true, "()V", 0, false), definition, null, null);
37
-        this.clinitStatementVisitor = new JavaStatementVisitor(context, javaWriter);
39
+        this.clinitStatementVisitor = new JavaStatementVisitor(context, javaModule, javaWriter);
38
         this.clinitStatementVisitor.start();
40
         this.clinitStatementVisitor.start();
39
         CompilerUtils.writeDefaultFieldInitializers(context, javaWriter, definition, true);
41
         CompilerUtils.writeDefaultFieldInitializers(context, javaWriter, definition, true);
40
     }
42
     }
41
 
43
 
42
 	@Override
44
 	@Override
43
 	public Void visitConst(ConstMember member) {
45
 	public Void visitConst(ConstMember member) {
44
-		JavaField field = member.getTag(JavaField.class);
46
+		JavaField field = context.getJavaField(member);
45
         writer.visitField(CompilerUtils.calcAccess(member.getEffectiveModifiers()), field.name, field.descriptor, field.signature, null).visitEnd();
47
         writer.visitField(CompilerUtils.calcAccess(member.getEffectiveModifiers()), field.name, field.descriptor, field.signature, null).visitEnd();
46
         return null;
48
         return null;
47
 	}
49
 	}
48
 
50
 
49
 	@Override
51
 	@Override
50
 	public Void visitField(FieldMember member) {
52
 	public Void visitField(FieldMember member) {
51
-		JavaField field = member.getTag(JavaField.class);
53
+		JavaField field = context.getJavaField(member);
52
         writer.visitField(CompilerUtils.calcAccess(member.getEffectiveModifiers()), field.name, field.descriptor, field.signature, null).visitEnd();
54
         writer.visitField(CompilerUtils.calcAccess(member.getEffectiveModifiers()), field.name, field.descriptor, field.signature, null).visitEnd();
53
         return null;
55
         return null;
54
     }
56
     }
56
     @Override
58
     @Override
57
     public Void visitConstructor(ConstructorMember member) {
59
     public Void visitConstructor(ConstructorMember member) {
58
         final boolean isEnum = definition instanceof EnumDefinition;
60
         final boolean isEnum = definition instanceof EnumDefinition;
59
-        final JavaMethod method = member.getTag(JavaMethod.class);
61
+        final JavaMethod method = context.getJavaMethod(member);
60
 
62
 
61
         final Label constructorStart = new Label();
63
         final Label constructorStart = new Label();
62
         final Label constructorEnd = new Label();
64
         final Label constructorEnd = new Label();
63
         final JavaWriter constructorWriter = new JavaWriter(writer, method, definition, context.getMethodSignature(member.header), null);
65
         final JavaWriter constructorWriter = new JavaWriter(writer, method, definition, context.getMethodSignature(member.header), null);
64
         constructorWriter.label(constructorStart);
66
         constructorWriter.label(constructorStart);
65
-        CompilerUtils.tagConstructorParameters(context, member.header, isEnum);
67
+        CompilerUtils.tagConstructorParameters(context, javaModule, member.header, isEnum);
66
         for (FunctionParameter parameter : member.header.parameters) {
68
         for (FunctionParameter parameter : member.header.parameters) {
67
             constructorWriter.nameVariable(
69
             constructorWriter.nameVariable(
68
-                    parameter.getTag(JavaParameterInfo.class).index,
70
+                    javaModule.getParameterInfo(parameter).index,
69
                     parameter.name,
71
                     parameter.name,
70
                     constructorStart,
72
                     constructorStart,
71
                     constructorEnd,
73
                     constructorEnd,
72
                     context.getType(parameter.type));
74
                     context.getType(parameter.type));
73
         }
75
         }
74
 
76
 
75
-        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, constructorWriter);
77
+        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, constructorWriter);
76
         statementVisitor.start();
78
         statementVisitor.start();
77
 
79
 
78
 		if (!member.isConstructorForwarded()) {
80
 		if (!member.isConstructorForwarded()) {
106
 		final JavaWriter destructorWriter = new JavaWriter(writer, method, definition, null, null);
108
 		final JavaWriter destructorWriter = new JavaWriter(writer, method, definition, null, null);
107
 		destructorWriter.label(constructorStart);
109
 		destructorWriter.label(constructorStart);
108
 
110
 
109
-        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, destructorWriter);
111
+        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, destructorWriter);
110
         statementVisitor.start();
112
         statementVisitor.start();
111
 		// TODO: destruction of members (to be done when memory tags are implemented)
113
 		// TODO: destruction of members (to be done when memory tags are implemented)
112
 		member.body.accept(statementVisitor);
114
 		member.body.accept(statementVisitor);
117
 
119
 
118
     @Override
120
     @Override
119
     public Void visitMethod(MethodMember member) {
121
     public Void visitMethod(MethodMember member) {
120
-        CompilerUtils.tagMethodParameters(context, member.header, member.isStatic());
122
+        CompilerUtils.tagMethodParameters(context, javaModule, member.header, member.isStatic());
121
 
123
 
122
         final boolean isAbstract = member.body == null || Modifiers.isAbstract(member.getEffectiveModifiers());
124
         final boolean isAbstract = member.body == null || Modifiers.isAbstract(member.getEffectiveModifiers());
123
-        final JavaMethod method = member.getTag(JavaMethod.class);
125
+        final JavaMethod method = context.getJavaMethod(member);
124
 
126
 
125
 		final Label methodStart = new Label();
127
 		final Label methodStart = new Label();
126
 		final Label methodEnd = new Label();
128
 		final Label methodEnd = new Label();
129
 		for (final FunctionParameter parameter : member.header.parameters) {
131
 		for (final FunctionParameter parameter : member.header.parameters) {
130
 			methodWriter.nameParameter(0, parameter.name);
132
 			methodWriter.nameParameter(0, parameter.name);
131
 			if (!isAbstract)
133
 			if (!isAbstract)
132
-				methodWriter.nameVariable(parameter.getTag(JavaParameterInfo.class).index, parameter.name, methodStart, methodEnd, context.getType(parameter.type));
134
+				methodWriter.nameVariable(javaModule.getParameterInfo(parameter).index, parameter.name, methodStart, methodEnd, context.getType(parameter.type));
133
 		}
135
 		}
134
 
136
 
135
-        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, methodWriter);
137
+        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
136
 
138
 
137
 		if (!isAbstract) {
139
 		if (!isAbstract) {
138
 			statementVisitor.start();
140
 			statementVisitor.start();

+ 1
- 0
JavaShared/build.gradle View File

15
 
15
 
16
 dependencies {
16
 dependencies {
17
 	compile project(':CodeModel')
17
 	compile project(':CodeModel')
18
+	compile project(':CompilerShared')
18
 }
19
 }

+ 17
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaBaseCompiler.java View File

1
+package org.openzen.zenscript.javashared;
2
+
3
+import org.openzen.zenscript.compiler.ZenCodeCompiler;
4
+
5
+/*
6
+ * To change this license header, choose License Headers in Project Properties.
7
+ * To change this template file, choose Tools | Templates
8
+ * and open the template in the editor.
9
+ */
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public abstract class JavaBaseCompiler implements ZenCodeCompiler {
16
+	
17
+}

+ 4
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaClass.java View File

134
 		return kind == Kind.ENUM;
134
 		return kind == Kind.ENUM;
135
 	}
135
 	}
136
 	
136
 	
137
+	public boolean isInterface() {
138
+		return kind == Kind.INTERFACE;
139
+	}
140
+	
137
 	public enum Kind {
141
 	public enum Kind {
138
 		CLASS,
142
 		CLASS,
139
 		INTERFACE,
143
 		INTERFACE,

+ 132
- 0
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaCompiledModule.java View File

1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.javashared;
7
+
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+import org.openzen.zenscript.codemodel.FunctionParameter;
11
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
+import org.openzen.zenscript.codemodel.Module;
13
+import org.openzen.zenscript.codemodel.definition.VariantDefinition;
14
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
15
+import org.openzen.zenscript.codemodel.member.ImplementationMember;
16
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
17
+
18
+/**
19
+ *
20
+ * @author Hoofdgebruiker
21
+ */
22
+public class JavaCompiledModule {
23
+	public final Module module;
24
+	
25
+	private final Map<HighLevelDefinition, JavaClass> classes = new HashMap<>();
26
+	private final Map<HighLevelDefinition, JavaNativeClass> nativeClasses = new HashMap<>();
27
+	private final Map<ImplementationMember, JavaImplementation> implementations = new HashMap<>();
28
+	private final Map<IDefinitionMember, JavaField> fields = new HashMap<>();
29
+	private final Map<IDefinitionMember, JavaMethod> methods = new HashMap<>();
30
+	private final Map<FunctionParameter, JavaParameterInfo> parameters = new HashMap<>();
31
+	private final Map<VariantDefinition.Option, JavaVariantOption> variantOptions = new HashMap<>();
32
+	
33
+	public JavaCompiledModule(Module module) {
34
+		this.module = module;
35
+	}
36
+	
37
+	public void setClassInfo(HighLevelDefinition definition, JavaClass cls) {
38
+		classes.put(definition, cls);
39
+	}
40
+	
41
+	public JavaClass getClassInfo(HighLevelDefinition definition) {
42
+		JavaClass cls = classes.get(definition);
43
+		if (cls == null)
44
+			throw new IllegalStateException("Missing class info for class " + definition.name);
45
+		
46
+		return cls;
47
+	}
48
+	
49
+	public JavaClass optClassInfo(HighLevelDefinition definition) {
50
+		return classes.get(definition);
51
+	}
52
+	
53
+	public boolean hasClassInfo(HighLevelDefinition definition) {
54
+		return classes.containsKey(definition);
55
+	}
56
+	
57
+	public void setNativeClassInfo(HighLevelDefinition definition, JavaNativeClass cls) {
58
+		nativeClasses.put(definition, cls);
59
+	}
60
+	
61
+	public JavaNativeClass getNativeClassInfo(HighLevelDefinition definition) {
62
+		return nativeClasses.get(definition);
63
+	}
64
+	
65
+	public void setVariantOption(VariantDefinition.Option option, JavaVariantOption value) {
66
+		variantOptions.put(option, value);
67
+	}
68
+	
69
+	public JavaVariantOption getVariantOption(VariantDefinition.Option option) {
70
+		JavaVariantOption result = variantOptions.get(option);
71
+		if (result == null)
72
+			throw new IllegalStateException("Variant option unknown: " + option.name);
73
+		
74
+		return result;
75
+	}
76
+	
77
+	public void setImplementationInfo(ImplementationMember member, JavaImplementation implementation) {
78
+		implementations.put(member, implementation);
79
+	}
80
+	
81
+	public JavaImplementation getImplementationInfo(ImplementationMember member) {
82
+		JavaImplementation implementation = implementations.get(member);
83
+		if (implementation == null)
84
+			throw new IllegalStateException("Implementation unknown: " + member.type);
85
+		
86
+		return implementation;
87
+	}
88
+	
89
+	public void setFieldInfo(IDefinitionMember member, JavaField field) {
90
+		fields.put(member, field);
91
+	}
92
+	
93
+	public JavaField optFieldInfo(IDefinitionMember member) {
94
+		return fields.get(member);
95
+	}
96
+	
97
+	public JavaField getFieldInfo(IDefinitionMember member) {
98
+		JavaField field = fields.get(member);
99
+		if (field == null)
100
+			throw new IllegalStateException("Missing field info for field " + member.getDefinition().name + "." + member.describe());
101
+		
102
+		return field;
103
+	}
104
+	
105
+	public void setMethodInfo(IDefinitionMember member, JavaMethod method) {
106
+		methods.put(member, method);
107
+	}
108
+	
109
+	public JavaMethod getMethodInfo(DefinitionMemberRef member) {
110
+		return getMethodInfo(member.getTarget());
111
+	}
112
+	
113
+	public JavaMethod getMethodInfo(IDefinitionMember member) {
114
+		JavaMethod method = methods.get(member);
115
+		if (method == null)
116
+			throw new IllegalStateException("Missing field info for field " + member.getDefinition().name + "." + member.describe());
117
+		
118
+		return method;
119
+	}
120
+	
121
+	public void setParameterInfo(FunctionParameter parameter, JavaParameterInfo info) {
122
+		parameters.put(parameter, info);
123
+	}
124
+	
125
+	public JavaParameterInfo getParameterInfo(FunctionParameter parameter) {
126
+		JavaParameterInfo info = parameters.get(parameter);
127
+		if (info == null)
128
+			throw new IllegalStateException("Missing parameter info for parameter " + parameter.name);
129
+		
130
+		return info;
131
+	}
132
+}

+ 77
- 1
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java View File

12
 import org.openzen.zencode.shared.CodePosition;
12
 import org.openzen.zencode.shared.CodePosition;
13
 import org.openzen.zenscript.codemodel.FunctionHeader;
13
 import org.openzen.zenscript.codemodel.FunctionHeader;
14
 import org.openzen.zenscript.codemodel.FunctionParameter;
14
 import org.openzen.zenscript.codemodel.FunctionParameter;
15
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
16
+import org.openzen.zenscript.codemodel.Module;
17
+import org.openzen.zenscript.codemodel.definition.VariantDefinition;
15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
18
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
19
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
20
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
21
+import org.openzen.zenscript.codemodel.member.ref.VariantOptionRef;
16
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
22
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
17
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
23
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
18
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
24
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
32
 	private final Map<String, JavaSynthesizedRange> ranges = new HashMap<>();
38
 	private final Map<String, JavaSynthesizedRange> ranges = new HashMap<>();
33
 	private boolean useShared = false;
39
 	private boolean useShared = false;
34
 	
40
 	
41
+	private final Map<Module, JavaCompiledModule> modules = new HashMap<>();
42
+	
35
 	public JavaContext(GlobalTypeRegistry registry) {
43
 	public JavaContext(GlobalTypeRegistry registry) {
36
 		this.registry = registry;
44
 		this.registry = registry;
37
 		
45
 		
73
 	}
81
 	}
74
 	
82
 	
75
 	protected abstract JavaSyntheticClassGenerator getTypeGenerator();
83
 	protected abstract JavaSyntheticClassGenerator getTypeGenerator();
76
-			
84
+		
77
 	public abstract String getDescriptor(TypeID type);
85
 	public abstract String getDescriptor(TypeID type);
78
 	
86
 	
79
 	public abstract String getDescriptor(StoredType type);
87
 	public abstract String getDescriptor(StoredType type);
80
 	
88
 	
89
+	public void addModule(Module module) {
90
+		modules.put(module, new JavaCompiledModule(module));
91
+	}
92
+	
93
+	public JavaCompiledModule getJavaModule(Module module) {
94
+		JavaCompiledModule javaModule = modules.get(module);
95
+		if (javaModule == null)
96
+			throw new IllegalStateException("Module not yet registered: " + module.name);
97
+		
98
+		return javaModule;
99
+	}
100
+	
101
+	public JavaClass getJavaClass(HighLevelDefinition definition) {
102
+		return getJavaModule(definition.module).getClassInfo(definition);
103
+	}
104
+	
105
+	public JavaClass optJavaClass(HighLevelDefinition definition) {
106
+		return getJavaModule(definition.module).optClassInfo(definition);
107
+	}
108
+	
109
+	public JavaNativeClass getJavaNativeClass(HighLevelDefinition definition) {
110
+		return getJavaModule(definition.module).getNativeClassInfo(definition);
111
+	}
112
+	
113
+	public boolean hasJavaClass(HighLevelDefinition definition) {
114
+		return getJavaModule(definition.module).hasClassInfo(definition);
115
+	}
116
+	
117
+	public void setJavaClass(HighLevelDefinition definition, JavaClass cls) {
118
+		getJavaModule(definition.module).setClassInfo(definition, cls);
119
+	}
120
+	
121
+	public void setJavaNativeClass(HighLevelDefinition definition, JavaNativeClass cls) {
122
+		getJavaModule(definition.module).setNativeClassInfo(definition, cls);
123
+	}
124
+	
125
+	public boolean hasJavaField(DefinitionMemberRef member) {
126
+		HighLevelDefinition definition = member.getTarget().getDefinition();
127
+		return getJavaModule(definition.module).optFieldInfo(member.getTarget()) != null;
128
+	}
129
+	
130
+	public JavaField getJavaField(IDefinitionMember member) {
131
+		HighLevelDefinition definition = member.getDefinition();
132
+		return getJavaModule(definition.module).getFieldInfo(member);
133
+	}
134
+	
135
+	public JavaField getJavaField(DefinitionMemberRef member) {
136
+		return getJavaField(member.getTarget());
137
+	}
138
+	
139
+	public JavaMethod getJavaMethod(IDefinitionMember member) {
140
+		HighLevelDefinition definition = member.getDefinition();
141
+		return getJavaModule(definition.module).getMethodInfo(member);
142
+	}
143
+	
144
+	public JavaMethod getJavaMethod(DefinitionMemberRef member) {
145
+		return getJavaMethod(member.getTarget());
146
+	}
147
+	
148
+	public JavaVariantOption getJavaVariantOption(VariantDefinition.Option option) {
149
+		HighLevelDefinition definition = option.variant;
150
+		return getJavaModule(definition.module).getVariantOption(option);
151
+	}
152
+	
153
+	public JavaVariantOption getJavaVariantOption(VariantOptionRef member) {
154
+		return getJavaVariantOption(member.getOption());
155
+	}
156
+	
81
 	public void useShared() {
157
 	public void useShared() {
82
 		if (useShared)
158
 		if (useShared)
83
 			return;
159
 			return;

+ 1
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeDescriptorVisitor.java View File

106
 
106
 
107
     @Override
107
     @Override
108
     public String visitDefinition(DefinitionTypeID definition) {
108
     public String visitDefinition(DefinitionTypeID definition) {
109
-		JavaClass cls = definition.definition.getTag(JavaClass.class);
110
-		if (cls == null)
111
-			throw new IllegalStateException("Class not yet initialized: " + definition.definition.name);
112
-		
109
+		JavaClass cls = context.getJavaClass(definition.definition);
113
 		return "L" + cls.internalName + ";";
110
 		return "L" + cls.internalName + ";";
114
     }
111
     }
115
 
112
 

+ 1
- 1
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeInternalNameVisitor.java View File

88
 
88
 
89
     @Override
89
     @Override
90
     public String visitDefinition(StoredType context, DefinitionTypeID definition) {
90
     public String visitDefinition(StoredType context, DefinitionTypeID definition) {
91
-		return definition.definition.getTag(JavaClass.class).internalName;
91
+		return this.context.getJavaClass(definition.definition).internalName;
92
     }
92
     }
93
 
93
 
94
     @Override
94
     @Override

+ 14
- 12
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareClassMethodVisitor.java View File

33
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
33
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
34
 import org.openzen.zenscript.javashared.JavaTypeNameVisitor;
34
 import org.openzen.zenscript.javashared.JavaTypeNameVisitor;
35
 import org.openzen.zenscript.javashared.JavaClass;
35
 import org.openzen.zenscript.javashared.JavaClass;
36
+import org.openzen.zenscript.javashared.JavaCompiledModule;
36
 import org.openzen.zenscript.javashared.JavaField;
37
 import org.openzen.zenscript.javashared.JavaField;
37
 import org.openzen.zenscript.javashared.JavaContext;
38
 import org.openzen.zenscript.javashared.JavaContext;
38
 import org.openzen.zenscript.javashared.JavaImplementation;
39
 import org.openzen.zenscript.javashared.JavaImplementation;
47
 	private static final boolean DEBUG_EMPTY = true;
48
 	private static final boolean DEBUG_EMPTY = true;
48
 	
49
 	
49
 	private final JavaContext context;
50
 	private final JavaContext context;
51
+	private final JavaCompiledModule module;
50
 	private final JavaClass cls;
52
 	private final JavaClass cls;
51
 	private final JavaNativeClass nativeClass;
53
 	private final JavaNativeClass nativeClass;
52
 	private final JavaPrepareDefinitionMemberVisitor memberPreparer;
54
 	private final JavaPrepareDefinitionMemberVisitor memberPreparer;
53
 	
55
 	
54
 	public JavaPrepareClassMethodVisitor(
56
 	public JavaPrepareClassMethodVisitor(
55
 			JavaContext context,
57
 			JavaContext context,
58
+			JavaCompiledModule module,
56
 			JavaClass cls,
59
 			JavaClass cls,
57
 			JavaNativeClass nativeClass,
60
 			JavaNativeClass nativeClass,
58
 			JavaPrepareDefinitionMemberVisitor memberPreparer,
61
 			JavaPrepareDefinitionMemberVisitor memberPreparer,
59
 			boolean startsEmpty) {
62
 			boolean startsEmpty) {
60
 		this.context = context;
63
 		this.context = context;
64
+		this.module = module;
61
 		this.cls = cls;
65
 		this.cls = cls;
62
 		this.nativeClass = nativeClass;
66
 		this.nativeClass = nativeClass;
63
 		this.memberPreparer = memberPreparer;
67
 		this.memberPreparer = memberPreparer;
71
 			System.out.println("Class " + cls.fullName + " not empty because of const " + member.name);
75
 			System.out.println("Class " + cls.fullName + " not empty because of const " + member.name);
72
 		
76
 		
73
 		cls.empty = false;
77
 		cls.empty = false;
74
-		member.setTag(JavaField.class, new JavaField(cls, member.name, context.getDescriptor(member.type)));
78
+		module.setFieldInfo(member, new JavaField(cls, member.name, context.getDescriptor(member.type)));
75
 		return null;
79
 		return null;
76
 	}
80
 	}
77
 	
81
 	
78
 	@Override
82
 	@Override
79
 	public Void visitField(FieldMember member) {
83
 	public Void visitField(FieldMember member) {
80
 		JavaField field = new JavaField(cls, member.name, context.getDescriptor(member.type));
84
 		JavaField field = new JavaField(cls, member.name, context.getDescriptor(member.type));
81
-		member.setTag(JavaField.class, field);
85
+		module.setFieldInfo(member, field);
82
 		if (member.hasAutoGetter()) {
86
 		if (member.hasAutoGetter()) {
83
 			visitGetter(member.autoGetter);
87
 			visitGetter(member.autoGetter);
84
-			member.autoGetter.setTag(JavaField.class, field);
88
+			module.setFieldInfo(member.autoGetter, field);
85
 		}
89
 		}
86
 		if (member.hasAutoSetter()) {
90
 		if (member.hasAutoSetter()) {
87
 			visitSetter(member.autoSetter);
91
 			visitSetter(member.autoSetter);
92
+			module.setFieldInfo(member.autoSetter, field);
88
 		}
93
 		}
89
 		
94
 		
90
 		return null;
95
 		return null;
152
 		memberPreparer.prepare(member.type);
157
 		memberPreparer.prepare(member.type);
153
 		
158
 		
154
 		if (canMergeImplementation(member)) {
159
 		if (canMergeImplementation(member)) {
155
-			member.setTag(JavaImplementation.class, new JavaImplementation(true, cls));
160
+			module.setImplementationInfo(member, new JavaImplementation(true, cls));
156
 			for (IDefinitionMember m : member.members)
161
 			for (IDefinitionMember m : member.members)
157
 				m.accept(this);
162
 				m.accept(this);
158
 		} else {
163
 		} else {
162
 			cls.empty = false;
167
 			cls.empty = false;
163
 			
168
 			
164
 			JavaClass implementationClass = new JavaClass(cls, JavaTypeNameVisitor.INSTANCE.process(member.type) + "Implementation", JavaClass.Kind.CLASS);
169
 			JavaClass implementationClass = new JavaClass(cls, JavaTypeNameVisitor.INSTANCE.process(member.type) + "Implementation", JavaClass.Kind.CLASS);
165
-			member.setTag(JavaImplementation.class, new JavaImplementation(false, implementationClass));
170
+			module.setImplementationInfo(member, new JavaImplementation(false, implementationClass));
166
 			
171
 			
167
-			JavaPrepareClassMethodVisitor visitor = new JavaPrepareClassMethodVisitor(context, implementationClass, null, memberPreparer, true);
172
+			JavaPrepareClassMethodVisitor visitor = new JavaPrepareClassMethodVisitor(context, module, implementationClass, null, memberPreparer, true);
168
 			for (IDefinitionMember m : member.members)
173
 			for (IDefinitionMember m : member.members)
169
 				m.accept(visitor);
174
 				m.accept(visitor);
170
 		}
175
 		}
177
 
182
 
178
 	@Override
183
 	@Override
179
 	public Void visitInnerDefinition(InnerDefinitionMember member) {
184
 	public Void visitInnerDefinition(InnerDefinitionMember member) {
180
-		JavaPrepareDefinitionMemberVisitor innerDefinitionPrepare = new JavaPrepareDefinitionMemberVisitor(context);
185
+		JavaPrepareDefinitionMemberVisitor innerDefinitionPrepare = new JavaPrepareDefinitionMemberVisitor(context, module);
181
 		member.innerDefinition.accept(innerDefinitionPrepare);
186
 		member.innerDefinition.accept(innerDefinitionPrepare);
182
 		
187
 		
183
 		if (DEBUG_EMPTY && cls.empty)
188
 		if (DEBUG_EMPTY && cls.empty)
293
 		
298
 		
294
 		if (member.getOverrides() != null) {
299
 		if (member.getOverrides() != null) {
295
 			DefinitionMemberRef base = member.getOverrides();
300
 			DefinitionMemberRef base = member.getOverrides();
296
-			JavaMethod baseMethod = base.getTarget().getTag(JavaMethod.class);
297
-			if (baseMethod == null)
298
-				throw new IllegalStateException("Base method not yet prepared!");
299
-			
301
+			JavaMethod baseMethod = context.getJavaMethod(base.getTarget());
300
 			method = new JavaMethod(
302
 			method = new JavaMethod(
301
 					cls,
303
 					cls,
302
 					baseMethod.kind,
304
 					baseMethod.kind,
323
 			cls.empty = false;
325
 			cls.empty = false;
324
 		}
326
 		}
325
 		
327
 		
326
-		member.setTag(JavaMethod.class, method);
328
+		module.setMethodInfo(member, method);
327
 	}
329
 	}
328
 }
330
 }

+ 22
- 19
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionMemberVisitor.java View File

20
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
20
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
21
 import org.openzen.zenscript.codemodel.type.TypeID;
21
 import org.openzen.zenscript.codemodel.type.TypeID;
22
 import org.openzen.zenscript.javashared.JavaClass;
22
 import org.openzen.zenscript.javashared.JavaClass;
23
+import org.openzen.zenscript.javashared.JavaCompiledModule;
23
 import org.openzen.zenscript.javashared.JavaContext;
24
 import org.openzen.zenscript.javashared.JavaContext;
24
 import org.openzen.zenscript.javashared.JavaMethod;
25
 import org.openzen.zenscript.javashared.JavaMethod;
25
 import org.openzen.zenscript.javashared.JavaModifiers;
26
 import org.openzen.zenscript.javashared.JavaModifiers;
30
  */
31
  */
31
 public class JavaPrepareDefinitionMemberVisitor implements DefinitionVisitor<JavaClass> {
32
 public class JavaPrepareDefinitionMemberVisitor implements DefinitionVisitor<JavaClass> {
32
 	private final JavaContext context;
33
 	private final JavaContext context;
34
+	private final JavaCompiledModule module;
33
 	
35
 	
34
-	public JavaPrepareDefinitionMemberVisitor(JavaContext context) {
36
+	public JavaPrepareDefinitionMemberVisitor(JavaContext context, JavaCompiledModule module) {
35
 		this.context = context;
37
 		this.context = context;
38
+		this.module = module;
36
 	}
39
 	}
37
 	
40
 	
38
 	private boolean isPrepared(HighLevelDefinition definition) {
41
 	private boolean isPrepared(HighLevelDefinition definition) {
39
-		return definition.getTag(JavaClass.class).membersPrepared;
42
+		return context.getJavaClass(definition).membersPrepared;
40
 	}
43
 	}
41
 	
44
 	
42
 	public void prepare(TypeID type) {
45
 	public void prepare(TypeID type) {
51
 	@Override
54
 	@Override
52
 	public JavaClass visitClass(ClassDefinition definition) {
55
 	public JavaClass visitClass(ClassDefinition definition) {
53
 		if (isPrepared(definition))
56
 		if (isPrepared(definition))
54
-			return definition.getTag(JavaClass.class);
57
+			return context.getJavaClass(definition);
55
 		
58
 		
56
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
59
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
57
 	}
60
 	}
59
 	@Override
62
 	@Override
60
 	public JavaClass visitInterface(InterfaceDefinition definition) {
63
 	public JavaClass visitInterface(InterfaceDefinition definition) {
61
 		if (isPrepared(definition))
64
 		if (isPrepared(definition))
62
-			return definition.getTag(JavaClass.class);
65
+			return context.getJavaClass(definition);
63
 		
66
 		
64
 		for (TypeID baseType : definition.baseInterfaces)
67
 		for (TypeID baseType : definition.baseInterfaces)
65
 			prepare(baseType);
68
 			prepare(baseType);
70
 	@Override
73
 	@Override
71
 	public JavaClass visitEnum(EnumDefinition definition) {
74
 	public JavaClass visitEnum(EnumDefinition definition) {
72
 		if (isPrepared(definition))
75
 		if (isPrepared(definition))
73
-			return definition.getTag(JavaClass.class);
76
+			return context.getJavaClass(definition);
74
 		
77
 		
75
 		return visitClassCompiled(definition, false, JavaClass.Kind.ENUM);
78
 		return visitClassCompiled(definition, false, JavaClass.Kind.ENUM);
76
 	}
79
 	}
78
 	@Override
81
 	@Override
79
 	public JavaClass visitStruct(StructDefinition definition) {
82
 	public JavaClass visitStruct(StructDefinition definition) {
80
 		if (isPrepared(definition))
83
 		if (isPrepared(definition))
81
-			return definition.getTag(JavaClass.class);
84
+			return context.getJavaClass(definition);
82
 		
85
 		
83
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
86
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
84
 	}
87
 	}
86
 	@Override
89
 	@Override
87
 	public JavaClass visitFunction(FunctionDefinition definition) {
90
 	public JavaClass visitFunction(FunctionDefinition definition) {
88
 		if (isPrepared(definition))
91
 		if (isPrepared(definition))
89
-			return definition.getTag(JavaClass.class);
92
+			return context.getJavaClass(definition);
90
 		
93
 		
91
-		JavaClass cls = definition.getTag(JavaClass.class);
94
+		JavaClass cls = context.getJavaClass(definition);
92
 		JavaMethod method = JavaMethod.getStatic(cls, definition.name, context.getMethodDescriptor(definition.header), JavaModifiers.getJavaModifiers(definition.modifiers));
95
 		JavaMethod method = JavaMethod.getStatic(cls, definition.name, context.getMethodDescriptor(definition.header), JavaModifiers.getJavaModifiers(definition.modifiers));
93
-		definition.caller.setTag(JavaMethod.class, method);
96
+		module.setMethodInfo(definition.caller, method);
94
 		return cls;
97
 		return cls;
95
 	}
98
 	}
96
 
99
 
97
 	@Override
100
 	@Override
98
 	public JavaClass visitExpansion(ExpansionDefinition definition) {
101
 	public JavaClass visitExpansion(ExpansionDefinition definition) {
99
 		if (isPrepared(definition))
102
 		if (isPrepared(definition))
100
-			return definition.getTag(JavaClass.class);
103
+			return context.getJavaClass(definition);
101
 		
104
 		
102
-		JavaNativeClass nativeClass = definition.getTag(JavaNativeClass.class);
103
-		JavaClass cls = definition.getTag(JavaClass.class);
105
+		JavaNativeClass nativeClass = context.getJavaNativeClass(definition);
106
+		JavaClass cls = context.getJavaClass(definition);
104
 		visitExpansionMembers(definition, cls, nativeClass);
107
 		visitExpansionMembers(definition, cls, nativeClass);
105
 		return cls;
108
 		return cls;
106
 	}
109
 	}
113
 
116
 
114
 	@Override
117
 	@Override
115
 	public JavaClass visitVariant(VariantDefinition variant) {
118
 	public JavaClass visitVariant(VariantDefinition variant) {
116
-		if (isPrepared(variant))
117
-			return variant.getTag(JavaClass.class);
119
+		JavaClass cls = context.getJavaClass(variant);
120
+		if (cls.membersPrepared)
121
+			return cls;
118
 		
122
 		
119
-		JavaClass cls = variant.getTag(JavaClass.class);
120
 		visitClassMembers(variant, cls, null, false);
123
 		visitClassMembers(variant, cls, null, false);
121
 		return cls;
124
 		return cls;
122
 	}
125
 	}
125
 		if (definition.getSuperType() != null)
128
 		if (definition.getSuperType() != null)
126
 			prepare(definition.getSuperType());
129
 			prepare(definition.getSuperType());
127
 		
130
 		
128
-		JavaNativeClass nativeClass = definition.getTag(JavaNativeClass.class);
129
-		JavaClass cls = definition.getTag(JavaClass.class);
131
+		JavaNativeClass nativeClass = context.getJavaNativeClass(definition);
132
+		JavaClass cls = context.getJavaClass(definition);
130
 		if (nativeClass == null) {
133
 		if (nativeClass == null) {
131
 			visitClassMembers(definition, cls, null, startsEmpty);
134
 			visitClassMembers(definition, cls, null, startsEmpty);
132
 		} else {
135
 		} else {
137
 	
140
 	
138
 	private void visitClassMembers(HighLevelDefinition definition, JavaClass cls, JavaNativeClass nativeClass, boolean startsEmpty) {
141
 	private void visitClassMembers(HighLevelDefinition definition, JavaClass cls, JavaNativeClass nativeClass, boolean startsEmpty) {
139
 		System.out.println("Preparing " + cls.internalName);
142
 		System.out.println("Preparing " + cls.internalName);
140
-		JavaPrepareClassMethodVisitor methodVisitor = new JavaPrepareClassMethodVisitor(context, cls, nativeClass, this, startsEmpty);
143
+		JavaPrepareClassMethodVisitor methodVisitor = new JavaPrepareClassMethodVisitor(context, module, cls, nativeClass, this, startsEmpty);
141
 		for (IDefinitionMember member : definition.members) {
144
 		for (IDefinitionMember member : definition.members) {
142
 			member.accept(methodVisitor);
145
 			member.accept(methodVisitor);
143
 		}
146
 		}
146
 	
149
 	
147
 	private void visitExpansionMembers(HighLevelDefinition definition, JavaClass cls, JavaNativeClass nativeClass) {
150
 	private void visitExpansionMembers(HighLevelDefinition definition, JavaClass cls, JavaNativeClass nativeClass) {
148
 		System.out.println("Preparing " + cls.internalName);
151
 		System.out.println("Preparing " + cls.internalName);
149
-		JavaPrepareExpansionMethodVisitor methodVisitor = new JavaPrepareExpansionMethodVisitor(context, cls, nativeClass);
152
+		JavaPrepareExpansionMethodVisitor methodVisitor = new JavaPrepareExpansionMethodVisitor(context, module, cls, nativeClass);
150
 		for (IDefinitionMember member : definition.members) {
153
 		for (IDefinitionMember member : definition.members) {
151
 			member.accept(methodVisitor);
154
 			member.accept(methodVisitor);
152
 		}
155
 		}

+ 25
- 18
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareDefinitionVisitor.java View File

29
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
29
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
30
 import org.openzen.zenscript.codemodel.type.TypeID;
30
 import org.openzen.zenscript.codemodel.type.TypeID;
31
 import org.openzen.zenscript.javashared.JavaClass;
31
 import org.openzen.zenscript.javashared.JavaClass;
32
+import org.openzen.zenscript.javashared.JavaCompiledModule;
33
+import org.openzen.zenscript.javashared.JavaContext;
32
 import org.openzen.zenscript.javashared.JavaMethod;
34
 import org.openzen.zenscript.javashared.JavaMethod;
33
 import org.openzen.zenscript.javashared.JavaVariantOption;
35
 import org.openzen.zenscript.javashared.JavaVariantOption;
34
 
36
 
227
 		}
229
 		}
228
 	}
230
 	}
229
 	
231
 	
232
+	private final JavaContext context;
230
 	private final String filename;
233
 	private final String filename;
231
 	private final JavaClass outerClass;
234
 	private final JavaClass outerClass;
235
+	private final JavaCompiledModule module;
232
 	
236
 	
233
-	public JavaPrepareDefinitionVisitor(String filename, JavaClass outerClass) {
237
+	public JavaPrepareDefinitionVisitor(JavaContext context, JavaCompiledModule module, String filename, JavaClass outerClass) {
238
+		this.context = context;
234
 		this.filename = filename;
239
 		this.filename = filename;
235
 		this.outerClass = outerClass;
240
 		this.outerClass = outerClass;
241
+		this.module = module;
236
 	}
242
 	}
237
 	
243
 	
238
 	private boolean isPrepared(HighLevelDefinition definition) {
244
 	private boolean isPrepared(HighLevelDefinition definition) {
239
-		return definition.hasTag(JavaClass.class);
245
+		return context.hasJavaClass(definition);
240
 	}
246
 	}
241
 	
247
 	
242
 	public void prepare(TypeID type) {
248
 	public void prepare(TypeID type) {
250
 	@Override
256
 	@Override
251
 	public JavaClass visitClass(ClassDefinition definition) {
257
 	public JavaClass visitClass(ClassDefinition definition) {
252
 		if (isPrepared(definition))
258
 		if (isPrepared(definition))
253
-			return definition.getTag(JavaClass.class);
259
+			return context.getJavaClass(definition);
254
 		
260
 		
255
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
261
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
256
 	}
262
 	}
258
 	@Override
264
 	@Override
259
 	public JavaClass visitInterface(InterfaceDefinition definition) {
265
 	public JavaClass visitInterface(InterfaceDefinition definition) {
260
 		if (isPrepared(definition))
266
 		if (isPrepared(definition))
261
-			return definition.getTag(JavaClass.class);
267
+			return context.getJavaClass(definition);
262
 		
268
 		
263
 		for (TypeID baseType : definition.baseInterfaces)
269
 		for (TypeID baseType : definition.baseInterfaces)
264
 			prepare(baseType);
270
 			prepare(baseType);
269
 	@Override
275
 	@Override
270
 	public JavaClass visitEnum(EnumDefinition definition) {
276
 	public JavaClass visitEnum(EnumDefinition definition) {
271
 		if (isPrepared(definition))
277
 		if (isPrepared(definition))
272
-			return definition.getTag(JavaClass.class);
278
+			return context.getJavaClass(definition);
273
 		
279
 		
274
 		return visitClassCompiled(definition, false, JavaClass.Kind.ENUM);
280
 		return visitClassCompiled(definition, false, JavaClass.Kind.ENUM);
275
 	}
281
 	}
277
 	@Override
283
 	@Override
278
 	public JavaClass visitStruct(StructDefinition definition) {
284
 	public JavaClass visitStruct(StructDefinition definition) {
279
 		if (isPrepared(definition))
285
 		if (isPrepared(definition))
280
-			return definition.getTag(JavaClass.class);
286
+			return context.getJavaClass(definition);
281
 		
287
 		
282
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
288
 		return visitClassCompiled(definition, true, JavaClass.Kind.CLASS);
283
 	}
289
 	}
285
 	@Override
291
 	@Override
286
 	public JavaClass visitFunction(FunctionDefinition definition) {
292
 	public JavaClass visitFunction(FunctionDefinition definition) {
287
 		if (isPrepared(definition))
293
 		if (isPrepared(definition))
288
-			return definition.getTag(JavaClass.class);
294
+			return context.getJavaClass(definition);
289
 		
295
 		
290
 		JavaClass cls = new JavaClass(definition.pkg.fullName, JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
296
 		JavaClass cls = new JavaClass(definition.pkg.fullName, JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
291
-		definition.setTag(JavaClass.class, cls);
297
+		context.setJavaClass(definition, cls);
292
 		return cls;
298
 		return cls;
293
 	}
299
 	}
294
 
300
 
295
 	@Override
301
 	@Override
296
 	public JavaClass visitExpansion(ExpansionDefinition definition) {
302
 	public JavaClass visitExpansion(ExpansionDefinition definition) {
297
 		if (isPrepared(definition))
303
 		if (isPrepared(definition))
298
-			return definition.getTag(JavaClass.class);
304
+			return context.getJavaClass(definition);
299
 		
305
 		
300
 		NativeTag nativeTag = definition.getTag(NativeTag.class);
306
 		NativeTag nativeTag = definition.getTag(NativeTag.class);
301
 		if (nativeTag != null) {
307
 		if (nativeTag != null) {
302
-			definition.setTag(JavaNativeClass.class, nativeClasses.get(nativeTag.value));
308
+			context.setJavaNativeClass(definition, nativeClasses.get(nativeTag.value));
303
 		}
309
 		}
304
 		
310
 		
305
 		JavaClass cls = new JavaClass(definition.pkg.fullName, JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
311
 		JavaClass cls = new JavaClass(definition.pkg.fullName, JavaClass.getNameFromFile(filename), JavaClass.Kind.CLASS);
306
-		definition.setTag(JavaClass.class, cls);
312
+		context.setJavaClass(definition, cls);
307
 		return cls;
313
 		return cls;
308
 	}
314
 	}
309
 
315
 
316
 	@Override
322
 	@Override
317
 	public JavaClass visitVariant(VariantDefinition variant) {
323
 	public JavaClass visitVariant(VariantDefinition variant) {
318
 		if (isPrepared(variant))
324
 		if (isPrepared(variant))
319
-			return variant.getTag(JavaClass.class);
325
+			return context.getJavaClass(variant);
320
 		
326
 		
321
 		JavaClass cls = new JavaClass(variant.pkg.fullName, variant.name, JavaClass.Kind.CLASS);
327
 		JavaClass cls = new JavaClass(variant.pkg.fullName, variant.name, JavaClass.Kind.CLASS);
322
-		variant.setTag(JavaClass.class, cls);
328
+		context.setJavaClass(variant, cls);
323
 		
329
 		
324
 		for (VariantDefinition.Option option : variant.options) {
330
 		for (VariantDefinition.Option option : variant.options) {
325
 			JavaClass variantCls = new JavaClass(cls, option.name, JavaClass.Kind.CLASS);
331
 			JavaClass variantCls = new JavaClass(cls, option.name, JavaClass.Kind.CLASS);
326
-			option.setTag(JavaVariantOption.class, new JavaVariantOption(cls, variantCls));
332
+			module.setVariantOption(option, new JavaVariantOption(cls, variantCls));
327
 		}
333
 		}
328
 		
334
 		
329
 		return cls;
335
 		return cls;
339
 		if (nativeClass == null) {
345
 		if (nativeClass == null) {
340
 			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, definition.name, kind) : new JavaClass(outerClass, definition.name, kind);
346
 			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, definition.name, kind) : new JavaClass(outerClass, definition.name, kind);
341
 			cls.destructible = definition.isDestructible();
347
 			cls.destructible = definition.isDestructible();
342
-			definition.setTag(JavaClass.class, cls);
348
+			context.setJavaClass(definition, cls);
343
 		} else {
349
 		} else {
344
 			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, filename, kind) : new JavaClass(outerClass, filename, kind);
350
 			cls = outerClass == null ? new JavaClass(definition.pkg.fullName, filename, kind) : new JavaClass(outerClass, filename, kind);
345
-			definition.setTag(JavaClass.class, nativeClass.cls);
346
-			definition.setTag(JavaNativeClass.class, nativeClass);
351
+			
352
+			context.setJavaClass(definition, nativeClass.cls);
353
+			context.setJavaNativeClass(definition, nativeClass);
347
 			
354
 			
348
 			if (nativeClass.nonDestructible)
355
 			if (nativeClass.nonDestructible)
349
 				cls.destructible = false;
356
 				cls.destructible = false;
351
 		
358
 		
352
 		for (IDefinitionMember member : definition.members) {
359
 		for (IDefinitionMember member : definition.members) {
353
 			if (member instanceof InnerDefinitionMember) {
360
 			if (member instanceof InnerDefinitionMember) {
354
-				((InnerDefinitionMember) member).innerDefinition.accept(new JavaPrepareDefinitionVisitor(filename, cls));
361
+				((InnerDefinitionMember) member).innerDefinition.accept(new JavaPrepareDefinitionVisitor(context, module, filename, cls));
355
 			}
362
 			}
356
 		}
363
 		}
357
 		
364
 		

+ 11
- 5
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareExpansionMethodVisitor.java View File

31
 import org.openzen.zenscript.codemodel.type.GenericTypeID;
31
 import org.openzen.zenscript.codemodel.type.GenericTypeID;
32
 import org.openzen.zenscript.javashared.JavaTypeNameVisitor;
32
 import org.openzen.zenscript.javashared.JavaTypeNameVisitor;
33
 import org.openzen.zenscript.javashared.JavaClass;
33
 import org.openzen.zenscript.javashared.JavaClass;
34
+import org.openzen.zenscript.javashared.JavaCompiledModule;
34
 import org.openzen.zenscript.javashared.JavaField;
35
 import org.openzen.zenscript.javashared.JavaField;
35
 import org.openzen.zenscript.javashared.JavaContext;
36
 import org.openzen.zenscript.javashared.JavaContext;
36
 import org.openzen.zenscript.javashared.JavaImplementation;
37
 import org.openzen.zenscript.javashared.JavaImplementation;
45
 	private static final boolean DEBUG_EMPTY = true;
46
 	private static final boolean DEBUG_EMPTY = true;
46
 	
47
 	
47
 	private final JavaContext context;
48
 	private final JavaContext context;
49
+	private final JavaCompiledModule module;
48
 	private final JavaClass cls;
50
 	private final JavaClass cls;
49
 	private final JavaNativeClass nativeClass;
51
 	private final JavaNativeClass nativeClass;
50
 	
52
 	
51
-	public JavaPrepareExpansionMethodVisitor(JavaContext context, JavaClass cls, JavaNativeClass nativeClass) {
53
+	public JavaPrepareExpansionMethodVisitor(JavaContext context, JavaCompiledModule module, JavaClass cls, JavaNativeClass nativeClass) {
54
+		this.module = module;
52
 		this.cls = cls;
55
 		this.cls = cls;
53
 		this.nativeClass = nativeClass;
56
 		this.nativeClass = nativeClass;
54
 		this.context = context;
57
 		this.context = context;
57
 	
60
 	
58
 	@Override
61
 	@Override
59
 	public Void visitConst(ConstMember member) {
62
 	public Void visitConst(ConstMember member) {
60
-		member.setTag(JavaField.class, new JavaField(cls, member.name, context.getDescriptor(member.type)));
63
+		JavaField field = new JavaField(cls, member.name, context.getDescriptor(member.type));
64
+		module.setFieldInfo(member, field);
61
 		
65
 		
62
 		if (DEBUG_EMPTY && cls.empty)
66
 		if (DEBUG_EMPTY && cls.empty)
63
 			System.out.println("Class " + cls.fullName + " not empty because of const");
67
 			System.out.println("Class " + cls.fullName + " not empty because of const");
69
 	@Override
73
 	@Override
70
 	public Void visitField(FieldMember member) {
74
 	public Void visitField(FieldMember member) {
71
 		// TODO: expansion fields
75
 		// TODO: expansion fields
72
-		member.setTag(JavaField.class, new JavaField(cls, member.name, context.getDescriptor(member.type)));
76
+		JavaField field = new JavaField(cls, member.name, context.getDescriptor(member.type));
77
+		module.setFieldInfo(member, field);
78
+		
73
 		if (member.hasAutoGetter() || member.hasAutoSetter())
79
 		if (member.hasAutoGetter() || member.hasAutoSetter())
74
 			cls.empty = false;
80
 			cls.empty = false;
75
 		return null;
81
 		return null;
135
 	@Override
141
 	@Override
136
 	public Void visitImplementation(ImplementationMember member) {
142
 	public Void visitImplementation(ImplementationMember member) {
137
 		JavaClass implementationClass = new JavaClass(cls, JavaTypeNameVisitor.INSTANCE.process(member.type) + "Implementation", JavaClass.Kind.CLASS);
143
 		JavaClass implementationClass = new JavaClass(cls, JavaTypeNameVisitor.INSTANCE.process(member.type) + "Implementation", JavaClass.Kind.CLASS);
138
-		member.setTag(JavaImplementation.class, new JavaImplementation(false, implementationClass));
144
+		module.setImplementationInfo(member, new JavaImplementation(false, implementationClass));
139
 		for (IDefinitionMember implementedMember : member.members)
145
 		for (IDefinitionMember implementedMember : member.members)
140
 			implementedMember.accept(this);
146
 			implementedMember.accept(this);
141
 		
147
 		
170
 			cls.empty = false;
176
 			cls.empty = false;
171
 		}
177
 		}
172
 		
178
 		
173
-		member.setTag(JavaMethod.class, method);
179
+		module.setMethodInfo(member, method);
174
 	}
180
 	}
175
 	
181
 	
176
 	private JavaMethod.Kind getKind(DefinitionMember member) {
182
 	private JavaMethod.Kind getKind(DefinitionMember member) {

+ 35
- 12
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java View File

37
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
37
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
38
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
38
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
39
 import org.openzen.zenscript.javashared.JavaClass;
39
 import org.openzen.zenscript.javashared.JavaClass;
40
+import org.openzen.zenscript.javashared.JavaCompiledModule;
41
+import org.openzen.zenscript.javashared.JavaContext;
40
 import org.openzen.zenscript.javashared.JavaImplementation;
42
 import org.openzen.zenscript.javashared.JavaImplementation;
41
 
43
 
42
 /**
44
 /**
52
 	private final List<ExpansionDefinition> expansions;
54
 	private final List<ExpansionDefinition> expansions;
53
 	private final StringBuilder output;
55
 	private final StringBuilder output;
54
 	private final Map<HighLevelDefinition, SemanticModule> modules;
56
 	private final Map<HighLevelDefinition, SemanticModule> modules;
57
+	private final JavaContext context;
58
+	private final JavaCompiledModule module;
55
 	
59
 	
56
 	public JavaDefinitionVisitor(
60
 	public JavaDefinitionVisitor(
57
 			String indent,
61
 			String indent,
58
 			JavaSourceCompiler compiler,
62
 			JavaSourceCompiler compiler,
63
+			JavaCompiledModule module,
59
 			JavaClass cls,
64
 			JavaClass cls,
60
 			JavaSourceFile file,
65
 			JavaSourceFile file,
61
 			StringBuilder output,
66
 			StringBuilder output,
70
 		this.output = output;
75
 		this.output = output;
71
 		this.expansions = expansions;
76
 		this.expansions = expansions;
72
 		this.modules = modules;
77
 		this.modules = modules;
78
+		this.context = compiler.context;
79
+		this.module = module;
73
 	}
80
 	}
74
 	
81
 	
75
 	private JavaSourceFileScope createScope(HighLevelDefinition definition) {
82
 	private JavaSourceFileScope createScope(HighLevelDefinition definition) {
80
 		CompileScope scope = new CompileScope(module.compilationUnit.globalTypeRegistry, module.expansions, module.annotations, module.storageTypes);
87
 		CompileScope scope = new CompileScope(module.compilationUnit.globalTypeRegistry, module.expansions, module.annotations, module.storageTypes);
81
 		return new JavaSourceFileScope(file.importer, compiler.helperGenerator, cls, scope, definition instanceof InterfaceDefinition, thisType, compiler.context);
88
 		return new JavaSourceFileScope(file.importer, compiler.helperGenerator, cls, scope, definition instanceof InterfaceDefinition, thisType, compiler.context);
82
 	}
89
 	}
90
+	
91
+	private List<ImplementationMember> getMergedImplementations(HighLevelDefinition definition) {
92
+		List<ImplementationMember> result = new ArrayList<>();
93
+		for (IDefinitionMember member : definition.members) {
94
+			if (member instanceof ImplementationMember) {
95
+				JavaImplementation implementation = module.getImplementationInfo((ImplementationMember) member);
96
+				if (implementation.inline)
97
+					result.add((ImplementationMember)member);
98
+			}
99
+		}
100
+		return result;
101
+	}
83
 
102
 
84
 	@Override
103
 	@Override
85
 	public Void visitClass(ClassDefinition definition) {
104
 	public Void visitClass(ClassDefinition definition) {
86
 		JavaSourceFileScope scope = createScope(definition);
105
 		JavaSourceFileScope scope = createScope(definition);
87
-		JavaClass cls = definition.getTag(JavaClass.class);
106
+		JavaClass cls = context.getJavaClass(definition);
88
 		
107
 		
89
 		output.append(indent);
108
 		output.append(indent);
90
 		convertModifiers(definition.modifiers);
109
 		convertModifiers(definition.modifiers);
94
 			output.append(" extends ");
113
 			output.append(" extends ");
95
 			output.append(scope.type(definition.getSuperType()));
114
 			output.append(scope.type(definition.getSuperType()));
96
 		}
115
 		}
97
-		List<ImplementationMember> mergedImplementations = new ArrayList<>();
98
-		for (IDefinitionMember member : definition.members) {
99
-			if (member instanceof ImplementationMember) {
100
-				JavaImplementation implementation = ((ImplementationMember) member).getTag(JavaImplementation.class);
101
-				if (implementation.inline)
102
-					mergedImplementations.add((ImplementationMember)member);
103
-			}
104
-		}
105
 		
116
 		
117
+		List<ImplementationMember> mergedImplementations = getMergedImplementations(definition);
106
 		if (mergedImplementations.size() > 0 || cls.destructible) {
118
 		if (mergedImplementations.size() > 0 || cls.destructible) {
107
 			output.append(" implements ");
119
 			output.append(" implements ");
108
 			boolean first = true;
120
 			boolean first = true;
136
 	@Override
148
 	@Override
137
 	public Void visitInterface(InterfaceDefinition definition) {
149
 	public Void visitInterface(InterfaceDefinition definition) {
138
 		JavaSourceFileScope scope = createScope(definition);
150
 		JavaSourceFileScope scope = createScope(definition);
139
-		JavaClass cls = definition.getTag(JavaClass.class);
151
+		JavaClass cls = context.getJavaClass(definition);
140
 		
152
 		
141
 		output.append(indent);
153
 		output.append(indent);
142
 		convertModifiers(definition.modifiers | Modifiers.VIRTUAL); // to prevent 'final'
154
 		convertModifiers(definition.modifiers | Modifiers.VIRTUAL); // to prevent 'final'
160
 			output.append(scope.type(base));
172
 			output.append(scope.type(base));
161
 		}
173
 		}
162
 		
174
 		
175
+		List<ImplementationMember> mergedImplementations = getMergedImplementations(definition);
176
+		for (ImplementationMember member : mergedImplementations) {
177
+			if (firstExtends) {
178
+				firstExtends = false;
179
+				output.append(" extends ");
180
+			} else {
181
+				output.append(", ");
182
+			}
183
+			output.append(scope.type(member.type));
184
+		}
185
+		
163
 		output.append(" {\n");
186
 		output.append(" {\n");
164
 		compileMembers(scope, definition);
187
 		compileMembers(scope, definition);
165
 		compileExpansions();
188
 		compileExpansions();
367
 	}
390
 	}
368
 	
391
 	
369
 	private void compileMembers(JavaSourceFileScope scope, HighLevelDefinition definition) {
392
 	private void compileMembers(JavaSourceFileScope scope, HighLevelDefinition definition) {
370
-		if (definition.hasTag(JavaNativeClass.class)) {
393
+		if (context.getJavaNativeClass(definition) != null) {
371
 			StoredType[] typeParameters = new StoredType[definition.getNumberOfGenericParameters()];
394
 			StoredType[] typeParameters = new StoredType[definition.getNumberOfGenericParameters()];
372
 			for (int i = 0; i < typeParameters.length; i++)
395
 			for (int i = 0; i < typeParameters.length; i++)
373
 				typeParameters[i] = scope.semanticScope.getTypeRegistry().getGeneric(definition.typeParameters[i]).stored(definition.typeParameters[i].storage);
396
 				typeParameters[i] = scope.semanticScope.getTypeRegistry().getGeneric(definition.typeParameters[i]).stored(definition.typeParameters[i].storage);
378
 				member.accept(memberCompiler);
401
 				member.accept(memberCompiler);
379
 			memberCompiler.finish();
402
 			memberCompiler.finish();
380
 		} else {
403
 		} else {
381
-			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, file, settings, indent + settings.indent, output, scope, scope.isInterface, definition, modules);
404
+			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, scope.isInterface, definition, modules);
382
 			for (IDefinitionMember member : definition.members)
405
 			for (IDefinitionMember member : definition.members)
383
 				member.accept(memberCompiler);
406
 				member.accept(memberCompiler);
384
 			
407
 			

+ 1
- 3
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaExpansionMemberCompiler.java View File

57
 	}
57
 	}
58
 	
58
 	
59
 	private void compileMethod(DefinitionMember member, FunctionHeader header, Statement body) {
59
 	private void compileMethod(DefinitionMember member, FunctionHeader header, Statement body) {
60
-		JavaMethod method = member.getTag(JavaMethod.class);
61
-		if (method == null)
62
-			throw new AssertionError();
60
+		JavaMethod method = scope.context.getJavaMethod(member);
63
 		if (!method.compile)
61
 		if (!method.compile)
64
 			return;
62
 			return;
65
 		
63
 		

+ 16
- 7
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java View File

39
 import org.openzen.zenscript.compiler.SemanticModule;
39
 import org.openzen.zenscript.compiler.SemanticModule;
40
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
40
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
41
 import org.openzen.zenscript.javashared.JavaClass;
41
 import org.openzen.zenscript.javashared.JavaClass;
42
+import org.openzen.zenscript.javashared.JavaCompiledModule;
43
+import org.openzen.zenscript.javashared.JavaContext;
42
 import org.openzen.zenscript.javashared.JavaImplementation;
44
 import org.openzen.zenscript.javashared.JavaImplementation;
43
 import org.openzen.zenscript.javashared.JavaMethod;
45
 import org.openzen.zenscript.javashared.JavaMethod;
44
 
46
 
52
 	private final List<FieldMember> fields = new ArrayList<>();
54
 	private final List<FieldMember> fields = new ArrayList<>();
53
 	private final boolean isInterface;
55
 	private final boolean isInterface;
54
 	private final Map<HighLevelDefinition, SemanticModule> modules;
56
 	private final Map<HighLevelDefinition, SemanticModule> modules;
57
+	private final JavaContext context;
55
 	public boolean hasDestructor = false;
58
 	public boolean hasDestructor = false;
59
+	private final JavaCompiledModule module;
56
 	
60
 	
57
 	public JavaMemberCompiler(
61
 	public JavaMemberCompiler(
58
 			JavaSourceCompiler compiler,
62
 			JavaSourceCompiler compiler,
63
+			JavaCompiledModule module,
59
 			JavaSourceFile file,
64
 			JavaSourceFile file,
60
 			JavaSourceFormattingSettings settings,
65
 			JavaSourceFormattingSettings settings,
61
 			String indent,
66
 			String indent,
71
 		this.isInterface = isInterface;
76
 		this.isInterface = isInterface;
72
 		this.compiler = compiler;
77
 		this.compiler = compiler;
73
 		this.modules = modules;
78
 		this.modules = modules;
79
+		this.context = compiler.context;
80
+		this.module = module;
74
 	}
81
 	}
75
 	
82
 	
76
 	private void compileMethod(DefinitionMember member, FunctionHeader header, Statement body) {
83
 	private void compileMethod(DefinitionMember member, FunctionHeader header, Statement body) {
77
-		JavaMethod method = member.getTag(JavaMethod.class);
78
-		if (method == null)
79
-			throw new AssertionError();
84
+		JavaMethod method = module.getMethodInfo(member);
80
 		if (!method.compile)
85
 		if (!method.compile)
81
 			return;
86
 			return;
82
 		
87
 		
222
 
227
 
223
 	@Override
228
 	@Override
224
 	public Void visitImplementation(ImplementationMember member) {
229
 	public Void visitImplementation(ImplementationMember member) {
225
-		JavaImplementation implementation = member.getTag(JavaImplementation.class);
230
+		JavaImplementation implementation = module.getImplementationInfo(member);
226
 		if (implementation.inline) {
231
 		if (implementation.inline) {
227
 			for (IDefinitionMember m : member.members) {
232
 			for (IDefinitionMember m : member.members) {
228
 				m.accept(this);
233
 				m.accept(this);
238
 			
243
 			
239
 			begin(ElementType.INNERCLASS);
244
 			begin(ElementType.INNERCLASS);
240
 			output.append("private class ").append(implementationName).append(" implements ").append(scope.type(member.type)).append(" {\n");
245
 			output.append("private class ").append(implementationName).append(" implements ").append(scope.type(member.type)).append(" {\n");
241
-			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, file, settings, indent + settings.indent, output, scope, isInterface, definition, modules);
246
+			JavaMemberCompiler memberCompiler = new JavaMemberCompiler(compiler, module, file, settings, indent + settings.indent, output, scope, isInterface, definition, modules);
242
 			for (IDefinitionMember m : member.members) {
247
 			for (IDefinitionMember m : member.members) {
243
 				m.accept(memberCompiler);
248
 				m.accept(memberCompiler);
244
 			}
249
 			}
249
 
254
 
250
 	@Override
255
 	@Override
251
 	public Void visitInnerDefinition(InnerDefinitionMember member) {
256
 	public Void visitInnerDefinition(InnerDefinitionMember member) {
252
-		JavaClass cls = member.innerDefinition.getTag(JavaClass.class);
257
+		JavaClass cls = context.optJavaClass(member.innerDefinition);
258
+		if (cls == null)
259
+			return null;
260
+		
253
 		JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
261
 		JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
254
-				indent,
262
+				indent, 
255
 				compiler,
263
 				compiler,
264
+				module,
256
 				cls,
265
 				cls,
257
 				file,
266
 				file,
258
 				output,
267
 				output,

+ 9
- 3
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceCompiler.java View File

15
 import org.openzen.zenscript.compiler.CompilationUnit;
15
 import org.openzen.zenscript.compiler.CompilationUnit;
16
 import org.openzen.zenscript.compiler.SemanticModule;
16
 import org.openzen.zenscript.compiler.SemanticModule;
17
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
17
 import org.openzen.zenscript.compiler.ZenCodeCompiler;
18
+import org.openzen.zenscript.javashared.JavaBaseCompiler;
18
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
19
 import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
19
 import org.openzen.zenscript.javashared.JavaClass;
20
 import org.openzen.zenscript.javashared.JavaClass;
20
 
21
 
22
  *
23
  *
23
  * @author Hoofdgebruiker
24
  * @author Hoofdgebruiker
24
  */
25
  */
25
-public class JavaSourceCompiler implements ZenCodeCompiler {
26
+public class JavaSourceCompiler extends JavaBaseCompiler implements ZenCodeCompiler {
26
 	public final JavaSourceFormattingSettings settings;
27
 	public final JavaSourceFormattingSettings settings;
27
 	public final JavaSourceSyntheticHelperGenerator helperGenerator;
28
 	public final JavaSourceSyntheticHelperGenerator helperGenerator;
28
 	
29
 	
41
 		this.directory = directory;
42
 		this.directory = directory;
42
 	}
43
 	}
43
 	
44
 	
45
+	@Override
46
+	public void addModule(SemanticModule module) {
47
+		context.addModule(module.module);
48
+	}
49
+	
44
 	@Override
50
 	@Override
45
 	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
51
 	public void addDefinition(HighLevelDefinition definition, SemanticModule module) {
46
 		String filename = getFilename(definition);
52
 		String filename = getFilename(definition);
47
-		JavaPrepareDefinitionVisitor prepare = new JavaPrepareDefinitionVisitor(filename, null);
53
+		JavaPrepareDefinitionVisitor prepare = new JavaPrepareDefinitionVisitor(context, context.getJavaModule(module.module), filename, null);
48
 		JavaClass cls = definition.accept(prepare);
54
 		JavaClass cls = definition.accept(prepare);
49
 		
55
 		
50
 		File file = new File(getDirectory(definition.pkg), cls.getName() + ".java");
56
 		File file = new File(getDirectory(definition.pkg), cls.getName() + ".java");
51
 		System.out.println("Compiling " + definition.name + " as " + cls.fullName);
57
 		System.out.println("Compiling " + definition.name + " as " + cls.fullName);
52
 		JavaSourceFile sourceFile = sourceFiles.get(file);
58
 		JavaSourceFile sourceFile = sourceFiles.get(file);
53
 		if (sourceFile == null)
59
 		if (sourceFile == null)
54
-			sourceFiles.put(file, sourceFile = new JavaSourceFile(this, file, cls, definition.pkg));
60
+			sourceFiles.put(file, sourceFile = new JavaSourceFile(this, file, cls, module.module, definition.pkg));
55
 		
61
 		
56
 		sourceFile.add(definition, module);
62
 		sourceFile.add(definition, module);
57
 	}
63
 	}

+ 16
- 15
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java View File

99
 import org.openzen.zenscript.formattershared.StatementFormattingTarget;
99
 import org.openzen.zenscript.formattershared.StatementFormattingTarget;
100
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
100
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
101
 import org.openzen.zenscript.javashared.JavaClass;
101
 import org.openzen.zenscript.javashared.JavaClass;
102
+import org.openzen.zenscript.javashared.JavaCompiledModule;
103
+import org.openzen.zenscript.javashared.JavaContext;
102
 import org.openzen.zenscript.javashared.JavaField;
104
 import org.openzen.zenscript.javashared.JavaField;
103
 import org.openzen.zenscript.javashared.JavaNativeTranslator;
105
 import org.openzen.zenscript.javashared.JavaNativeTranslator;
104
 import org.openzen.zenscript.javashared.JavaMethod;
106
 import org.openzen.zenscript.javashared.JavaMethod;
118
 	
120
 	
119
 	public final JavaSourceStatementScope scope;
121
 	public final JavaSourceStatementScope scope;
120
 	public final StatementFormattingTarget target;
122
 	public final StatementFormattingTarget target;
123
+	private final JavaContext context;
121
 	
124
 	
122
 	public JavaSourceExpressionFormatter(StatementFormattingTarget target, JavaSourceStatementScope scope) {
125
 	public JavaSourceExpressionFormatter(StatementFormattingTarget target, JavaSourceStatementScope scope) {
123
 		this.target = target;
126
 		this.target = target;
124
 		this.scope = scope;
127
 		this.scope = scope;
128
+		context = scope.context;
125
 	}
129
 	}
126
 
130
 
127
 	@Override
131
 	@Override
168
 		if (expression.member.getBuiltin() != null) {
172
 		if (expression.member.getBuiltin() != null) {
169
 			return visitBuiltinCall(expression, expression.member.getBuiltin());
173
 			return visitBuiltinCall(expression, expression.member.getBuiltin());
170
 		} else {
174
 		} else {
171
-			JavaMethod method = expression.member.getTag(JavaMethod.class);
172
-			if (method == null)
173
-				throw new IllegalStateException("No source method tag for " + expression.member.getCanonicalName() + "!");
174
-			
175
+			JavaMethod method = context.getJavaMethod(expression.member);
175
 			if (method.kind == JavaMethod.Kind.COMPILED) {
176
 			if (method.kind == JavaMethod.Kind.COMPILED) {
176
 				return (ExpressionString) method.translation.translate(expression, this);
177
 				return (ExpressionString) method.translation.translate(expression, this);
177
 			} else {
178
 			} else {
208
 		if (expression.member.getBuiltin() != null)
209
 		if (expression.member.getBuiltin() != null)
209
 			return visitBuiltinCallStatic(expression, expression.member.getBuiltin());
210
 			return visitBuiltinCallStatic(expression, expression.member.getBuiltin());
210
 		
211
 		
211
-		JavaMethod method = expression.member.getTag(JavaMethod.class);
212
+		JavaMethod method = context.getJavaMethod(expression.member);
212
 		if (method == null)
213
 		if (method == null)
213
 			throw new IllegalStateException("No source method tag for " + expression.member.getCanonicalName() + "!");
214
 			throw new IllegalStateException("No source method tag for " + expression.member.getCanonicalName() + "!");
214
 		
215
 		
258
 			}
259
 			}
259
 		}
260
 		}
260
 		
261
 		
261
-		JavaMethod method = expression.member.getTag(JavaMethod.class);
262
+		JavaMethod method = context.getJavaMethod(expression.member);
262
 		if (method == null)
263
 		if (method == null)
263
 			throw new RuntimeException(expression.position + ": No tag for caster");
264
 			throw new RuntimeException(expression.position + ": No tag for caster");
264
 		
265
 		
304
 			return visitBuiltinConstant(expression, expression.constant.member.builtin);
305
 			return visitBuiltinConstant(expression, expression.constant.member.builtin);
305
 		
306
 		
306
 		return new ExpressionString(
307
 		return new ExpressionString(
307
-				scope.type(expression.constant.member.definition.getTag(JavaClass.class)) + "." + expression.constant.member.name, 
308
+				scope.type(expression.constant.member.definition) + "." + expression.constant.member.name, 
308
 				JavaOperator.MEMBER);
309
 				JavaOperator.MEMBER);
309
 	}
310
 	}
310
 
311
 
451
 
452
 
452
 	@Override
453
 	@Override
453
 	public ExpressionString visitGetStaticField(GetStaticFieldExpression expression) {
454
 	public ExpressionString visitGetStaticField(GetStaticFieldExpression expression) {
454
-		JavaField field = expression.field.getTag(JavaField.class);
455
+		JavaField field = context.getJavaField(expression.field);
455
 		if (field == null)
456
 		if (field == null)
456
 			throw new RuntimeException(expression.position + ": Missing field tag");
457
 			throw new RuntimeException(expression.position + ": Missing field tag");
457
 		
458
 		
464
 			return visitBuiltinGetter(expression, expression.getter.member.builtin);
465
 			return visitBuiltinGetter(expression, expression.getter.member.builtin);
465
 		
466
 		
466
 		ExpressionString target = expression.target.accept(this);
467
 		ExpressionString target = expression.target.accept(this);
467
-		if (expression.getter.member.hasTag(JavaField.class)) {
468
-			JavaField field = expression.getter.member.getTag(JavaField.class);
468
+		if (context.hasJavaField(expression.getter)) {
469
+			JavaField field = context.getJavaField(expression.getter);
469
 			if (target.value.equals("this") && !scope.hasLocalVariable(field.name))
470
 			if (target.value.equals("this") && !scope.hasLocalVariable(field.name))
470
 				return new ExpressionString(field.name, JavaOperator.PRIMARY);
471
 				return new ExpressionString(field.name, JavaOperator.PRIMARY);
471
 			
472
 			
472
 			return target.unaryPostfix(JavaOperator.MEMBER, "." + field.name);
473
 			return target.unaryPostfix(JavaOperator.MEMBER, "." + field.name);
473
 		}
474
 		}
474
 		
475
 		
475
-		JavaMethod method = expression.getter.getTag(JavaMethod.class);
476
+		JavaMethod method = context.getJavaMethod(expression.getter);
476
 		StringBuilder result = new StringBuilder();
477
 		StringBuilder result = new StringBuilder();
477
 		if (!target.value.equals("this") || scope.hasLocalVariable(method.name)) {
478
 		if (!target.value.equals("this") || scope.hasLocalVariable(method.name)) {
478
 			result.append(target.value);
479
 			result.append(target.value);
551
 			}
552
 			}
552
 		}
553
 		}
553
 		
554
 		
554
-		JavaMethod method = expression.constructor.getTag(JavaMethod.class);
555
+		JavaMethod method = context.getJavaMethod(expression.constructor);
555
 		switch (method.kind) {
556
 		switch (method.kind) {
556
 			case EXPANSION: {
557
 			case EXPANSION: {
557
 				StringBuilder output = new StringBuilder();
558
 				StringBuilder output = new StringBuilder();
637
 
638
 
638
 	@Override
639
 	@Override
639
 	public ExpressionString visitSetStaticField(SetStaticFieldExpression expression) {
640
 	public ExpressionString visitSetStaticField(SetStaticFieldExpression expression) {
640
-		JavaField field = expression.field.getTag(JavaField.class);
641
+		JavaField field = context.getJavaField(expression.field);
641
 		if (field == null)
642
 		if (field == null)
642
 			throw new RuntimeException(expression.position + ": Missing field tag");
643
 			throw new RuntimeException(expression.position + ": Missing field tag");
643
 		
644
 		
708
 	}
709
 	}
709
 	
710
 	
710
 	private String constructThisName(DefinitionTypeID type) {
711
 	private String constructThisName(DefinitionTypeID type) {
711
-		JavaClass cls = type.definition.getTag(JavaClass.class);
712
+		JavaClass cls = context.getJavaClass(type.definition);
712
 		return cls.getClassName();
713
 		return cls.getClassName();
713
 	}
714
 	}
714
 
715
 
758
 	
759
 	
759
 	@Override
760
 	@Override
760
 	public ExpressionString visitVariantValue(VariantValueExpression expression) {
761
 	public ExpressionString visitVariantValue(VariantValueExpression expression) {
761
-		JavaVariantOption option = expression.option.getTag(JavaVariantOption.class);
762
+		JavaVariantOption option = context.getJavaVariantOption(expression.option);
762
 		
763
 		
763
 		StringBuilder result = new StringBuilder();
764
 		StringBuilder result = new StringBuilder();
764
 		result.append("new ").append(scope.type(option.variantOptionClass));
765
 		result.append("new ").append(scope.type(option.variantOptionClass));

+ 21
- 4
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java View File

1
+
1
 /*
2
 /*
2
  * To change this license header, choose License Headers in Project Properties.
3
  * To change this license header, choose License Headers in Project Properties.
3
  * To change this template file, choose Tools | Templates
4
  * To change this template file, choose Tools | Templates
20
 import java.util.Map;
21
 import java.util.Map;
21
 import java.util.Set;
22
 import java.util.Set;
22
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
23
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
24
+import org.openzen.zenscript.codemodel.Module;
25
+import org.openzen.zenscript.codemodel.annotations.NativeTag;
23
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
26
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
24
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
27
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
25
 import org.openzen.zenscript.compiler.SemanticModule;
28
 import org.openzen.zenscript.compiler.SemanticModule;
37
 	private final JavaClass cls;
40
 	private final JavaClass cls;
38
 	private final StringBuilder contents = new StringBuilder();
41
 	private final StringBuilder contents = new StringBuilder();
39
 	private final ZSPackage pkg;
42
 	private final ZSPackage pkg;
43
+	private final Module module;
40
 	
44
 	
41
 	private HighLevelDefinition mainDefinition;
45
 	private HighLevelDefinition mainDefinition;
42
 	private final List<ExpansionDefinition> expansions = new ArrayList<>();
46
 	private final List<ExpansionDefinition> expansions = new ArrayList<>();
44
 	private final Map<HighLevelDefinition, SemanticModule> modules = new HashMap<>();
48
 	private final Map<HighLevelDefinition, SemanticModule> modules = new HashMap<>();
45
 	private final Set<String> existing = new HashSet<>();
49
 	private final Set<String> existing = new HashSet<>();
46
 	
50
 	
47
-	public JavaSourceFile(JavaSourceCompiler compiler, File file, JavaClass cls, ZSPackage pkg) {
51
+	public JavaSourceFile(JavaSourceCompiler compiler, File file, JavaClass cls, Module module, ZSPackage pkg) {
48
 		this.compiler = compiler;
52
 		this.compiler = compiler;
49
 		this.pkg = pkg;
53
 		this.pkg = pkg;
50
 		this.cls = cls;
54
 		this.cls = cls;
55
+		this.module = module;
51
 		this.file = file;
56
 		this.file = file;
52
 		
57
 		
53
-		importer = new JavaSourceImporter(cls);
58
+		importer = new JavaSourceImporter(compiler.context, cls);
54
 	}
59
 	}
55
 	
60
 	
56
 	public String getName() {
61
 	public String getName() {
70
 	}
75
 	}
71
 	
76
 	
72
 	public void prepare(JavaContext context) {
77
 	public void prepare(JavaContext context) {
73
-		JavaPrepareDefinitionMemberVisitor visitor = new JavaPrepareDefinitionMemberVisitor(context);
78
+		JavaPrepareDefinitionMemberVisitor visitor = new JavaPrepareDefinitionMemberVisitor(context, context.getJavaModule(module));
74
 		
79
 		
75
 		if (mainDefinition != null)
80
 		if (mainDefinition != null)
76
 			mainDefinition.accept(visitor);
81
 			mainDefinition.accept(visitor);
79
 			expansion.accept(visitor);
84
 			expansion.accept(visitor);
80
 	}
85
 	}
81
 	
86
 	
87
+	private boolean isEmpty(HighLevelDefinition definition) {
88
+		JavaClass cls = compiler.context.getJavaClass(definition);
89
+		if (!cls.empty)
90
+			return false;
91
+		
92
+		if (cls.isInterface() && definition.getTag(NativeTag.class) == null)
93
+			return false;
94
+		
95
+		return true;
96
+	}
97
+	
82
 	public void write() {
98
 	public void write() {
83
 		System.out.println("Calling write on " + file.getName());
99
 		System.out.println("Calling write on " + file.getName());
84
 		
100
 		
85
-		if (mainDefinition == null || mainDefinition.getTag(JavaClass.class).empty) {
101
+		if (mainDefinition == null || isEmpty(mainDefinition)) {
86
 			if (expansions.isEmpty())
102
 			if (expansions.isEmpty())
87
 				return;
103
 				return;
88
 			
104
 			
93
 		JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
109
 		JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
94
 				"",
110
 				"",
95
 				compiler,
111
 				compiler,
112
+				compiler.context.getJavaModule(module),
96
 				cls,
113
 				cls,
97
 				this,
114
 				this,
98
 				contents,
115
 				contents,

+ 5
- 6
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceImporter.java View File

12
 import java.util.Set;
12
 import java.util.Set;
13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
 import org.openzen.zenscript.javashared.JavaClass;
14
 import org.openzen.zenscript.javashared.JavaClass;
15
+import org.openzen.zenscript.javashared.JavaContext;
15
 
16
 
16
 /**
17
 /**
17
  *
18
  *
21
 	private final JavaClass cls;
22
 	private final JavaClass cls;
22
 	private final Map<String, JavaClass> imports = new HashMap<>();
23
 	private final Map<String, JavaClass> imports = new HashMap<>();
23
 	private final Set<JavaClass> usedImports = new HashSet<>();
24
 	private final Set<JavaClass> usedImports = new HashSet<>();
25
+	private final JavaContext context;
24
 	
26
 	
25
-	public JavaSourceImporter(JavaClass cls) {
27
+	public JavaSourceImporter(JavaContext context, JavaClass cls) {
26
 		this.cls = cls;
28
 		this.cls = cls;
29
+		this.context = context;
27
 	}
30
 	}
28
 	
31
 	
29
 	public String importType(HighLevelDefinition definition) {
32
 	public String importType(HighLevelDefinition definition) {
30
-		JavaClass cls = definition.getTag(JavaClass.class);
31
-		if (cls == null)
32
-			throw new IllegalStateException("Missing source class tag on " + definition.name);
33
-		
34
-		return importType(cls);
33
+		return importType(context.getJavaClass(definition));
35
 	}
34
 	}
36
 	
35
 	
37
 	public String importType(JavaClass cls) {
36
 	public String importType(JavaClass cls) {

+ 1
- 1
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceStatementFormatter.java View File

159
 			
159
 			
160
 			DefinitionTypeID variantType = statement.value.type.asDefinition();
160
 			DefinitionTypeID variantType = statement.value.type.asDefinition();
161
 			HighLevelDefinition variant = variantType.definition;
161
 			HighLevelDefinition variant = variantType.definition;
162
-			String variantTypeName = scope.type(variant.getTag(JavaClass.class));
162
+			String variantTypeName = scope.type(variant);
163
 			for (SwitchCase switchCase : statement.cases) {
163
 			for (SwitchCase switchCase : statement.cases) {
164
 				VariantOptionSwitchValue switchValue = (VariantOptionSwitchValue)switchCase.value;
164
 				VariantOptionSwitchValue switchValue = (VariantOptionSwitchValue)switchCase.value;
165
 				String header = switchValue == null ? "default:" : "case " + switchValue.option.getName() + ":";
165
 				String header = switchValue == null ? "default:" : "case " + switchValue.option.getName() + ":";

+ 3
- 3
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceSyntheticTypeGenerator.java View File

35
 	
35
 	
36
 	@Override
36
 	@Override
37
 	public void synthesizeFunction(JavaSynthesizedFunction function) {
37
 	public void synthesizeFunction(JavaSynthesizedFunction function) {
38
-		JavaSourceImporter importer = new JavaSourceImporter(function.cls);
38
+		JavaSourceImporter importer = new JavaSourceImporter(context, function.cls);
39
 		JavaSourceTypeVisitor typeVisitor = new JavaSourceTypeVisitor(importer, context);
39
 		JavaSourceTypeVisitor typeVisitor = new JavaSourceTypeVisitor(importer, context);
40
 		
40
 		
41
 		StringBuilder contents = new StringBuilder();
41
 		StringBuilder contents = new StringBuilder();
69
 	
69
 	
70
 	@Override
70
 	@Override
71
 	public void synthesizeRange(JavaSynthesizedRange range) {
71
 	public void synthesizeRange(JavaSynthesizedRange range) {
72
-		JavaSourceImporter importer = new JavaSourceImporter(range.cls);
72
+		JavaSourceImporter importer = new JavaSourceImporter(context, range.cls);
73
 		JavaSourceTypeVisitor typeVisitor = new JavaSourceTypeVisitor(importer, context);
73
 		JavaSourceTypeVisitor typeVisitor = new JavaSourceTypeVisitor(importer, context);
74
 		
74
 		
75
 		StringBuilder contents = new StringBuilder();
75
 		StringBuilder contents = new StringBuilder();
95
 	
95
 	
96
 	@Override
96
 	@Override
97
 	public void synthesizeShared() {
97
 	public void synthesizeShared() {
98
-		JavaSourceImporter importer = new JavaSourceImporter(JavaClass.SHARED);
98
+		JavaSourceImporter importer = new JavaSourceImporter(context, JavaClass.SHARED);
99
 		
99
 		
100
 		StringBuilder contents = new StringBuilder();
100
 		StringBuilder contents = new StringBuilder();
101
 		contents.append("public final class Shared<T extends AutoCloseable> {\n");
101
 		contents.append("public final class Shared<T extends AutoCloseable> {\n");

+ 5
- 0
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/scope/JavaSourceStatementScope.java View File

9
 import java.util.Set;
9
 import java.util.Set;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.FunctionParameter;
11
 import org.openzen.zenscript.codemodel.FunctionParameter;
12
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
 import org.openzen.zenscript.codemodel.expression.Expression;
13
 import org.openzen.zenscript.codemodel.expression.Expression;
13
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
14
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
14
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
15
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
97
 		return fileScope.importer.importType(cls);
98
 		return fileScope.importer.importType(cls);
98
 	}
99
 	}
99
 	
100
 	
101
+	public String type(HighLevelDefinition definition) {
102
+		return type(context.getJavaClass(definition));
103
+	}
104
+	
100
 	public void addLocalVariable(String localVariable) {
105
 	public void addLocalVariable(String localVariable) {
101
 		localVariables.add(localVariable);
106
 		localVariables.add(localVariable);
102
 	}
107
 	}

+ 17
- 24
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java View File

39
 import org.openzen.zenscript.codemodel.type.TypeID;
39
 import org.openzen.zenscript.codemodel.type.TypeID;
40
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
40
 import org.openzen.zenscript.codemodel.type.storage.AutoStorageTag;
41
 import org.openzen.zenscript.codemodel.type.storage.StaticExpressionStorageTag;
41
 import org.openzen.zenscript.codemodel.type.storage.StaticExpressionStorageTag;
42
+import org.openzen.zenscript.javashared.JavaCompiledModule;
43
+import org.openzen.zenscript.javashared.JavaContext;
42
 
44
 
43
 /**
45
 /**
44
  *
46
  *
50
 	private final ZSPackage javaIo = rootPackage.getOrCreatePackage("java").getOrCreatePackage("io");
52
 	private final ZSPackage javaIo = rootPackage.getOrCreatePackage("java").getOrCreatePackage("io");
51
 	private final ZSPackage javaLang = rootPackage.getOrCreatePackage("java").getOrCreatePackage("lang");
53
 	private final ZSPackage javaLang = rootPackage.getOrCreatePackage("java").getOrCreatePackage("lang");
52
 	
54
 	
55
+	private final Module MODULE = new Module("scriptingExample");
56
+	private final ClassDefinition PRINTSTREAM = new ClassDefinition(CodePosition.NATIVE, MODULE, javaIo, "PrintStream", Modifiers.PUBLIC);
57
+	private final ClassDefinition SYSTEM = new ClassDefinition(CodePosition.NATIVE, MODULE, javaLang, "System", Modifiers.PUBLIC);
58
+	private final MethodMember PRINTSTREAM_PRINTLN;
59
+	private final GetterMember SYSTEM_OUT;
60
+	
53
 	public GlobalRegistry(GlobalTypeRegistry registry, ZSPackage globals) {
61
 	public GlobalRegistry(GlobalTypeRegistry registry, ZSPackage globals) {
54
 		PRINTSTREAM_PRINTLN = new MethodMember(
62
 		PRINTSTREAM_PRINTLN = new MethodMember(
55
 			CodePosition.NATIVE,
63
 			CodePosition.NATIVE,
66
 			"out",
74
 			"out",
67
 			DefinitionTypeID.forType(registry, SYSTEM).stored(AutoStorageTag.INSTANCE),
75
 			DefinitionTypeID.forType(registry, SYSTEM).stored(AutoStorageTag.INSTANCE),
68
 			null);
76
 			null);
77
+	}
78
+	
79
+	public void register(JavaContext context) {
80
+		context.addModule(MODULE);
81
+		JavaCompiledModule javaModule = context.getJavaModule(MODULE);
82
+		
83
+		JavaClass jSystem = new JavaClass("java.lang", "System", JavaClass.Kind.CLASS);
84
+		javaModule.setFieldInfo(SYSTEM_OUT, new JavaField(jSystem, "out", "Ljava/io/PrintStream;"));
69
 		
85
 		
70
 		JavaClass jPrintStream = new JavaClass("java.io", "PrintStream", JavaClass.Kind.CLASS);
86
 		JavaClass jPrintStream = new JavaClass("java.io", "PrintStream", JavaClass.Kind.CLASS);
71
 		JavaMethod printstreamPrintln = JavaMethod.getNativeVirtual(jPrintStream, "println", "(Ljava/lang/String;)V");
87
 		JavaMethod printstreamPrintln = JavaMethod.getNativeVirtual(jPrintStream, "println", "(Ljava/lang/String;)V");
72
-		PRINTSTREAM_PRINTLN.setTag(JavaMethod.class, printstreamPrintln);
73
-		
74
-		JavaClass jSystem = new JavaClass("java.lang", "System", JavaClass.Kind.CLASS);
75
-		SYSTEM_OUT.setTag(JavaField.class, new JavaField(jSystem, "out", "Ljava/io/PrintStream;"));
88
+		javaModule.setMethodInfo(PRINTSTREAM_PRINTLN, printstreamPrintln);
76
 	}
89
 	}
77
 	
90
 	
78
 	public ZSPackage collectPackages() {
91
 	public ZSPackage collectPackages() {
79
 		// register packages here
92
 		// register packages here
80
 		
93
 		
81
-		{
82
-			// eg. package my.package with a class MyClass with a single native method test() returning a string
83
-			// the visitors can then during compilation check if a method is an instance of NativeMethodMember and treat it accordingly
84
-			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("test");
85
-			ClassDefinition myClassDefinition = new ClassDefinition(CodePosition.NATIVE, MODULE, packageMyPackage, "MyClass", Modifiers.PUBLIC, null);
86
-			JavaClass myClassInfo = new JavaClass("my.test", "MyClass", JavaClass.Kind.CLASS);
87
-			
88
-			MethodMember member = new MethodMember(CodePosition.NATIVE, myClassDefinition, Modifiers.PUBLIC, "test", new FunctionHeader(StringTypeID.UNIQUE), null);
89
-			member.setTag(JavaMethod.class, JavaMethod.getNativeVirtual(myClassInfo, "test", "()Ljava/lang/String;"));
90
-			myClassDefinition.addMember(member);
91
-			
92
-			packageMyPackage.register(myClassDefinition);
93
-		}
94
-		
95
 		return rootPackage;
94
 		return rootPackage;
96
 	}
95
 	}
97
 	
96
 	
110
 		return globals;
109
 		return globals;
111
 	}
110
 	}
112
 	
111
 	
113
-	private final Module MODULE = new Module("scriptingExample");
114
-	private final ClassDefinition PRINTSTREAM = new ClassDefinition(CodePosition.NATIVE, MODULE, javaIo, "PrintStream", Modifiers.PUBLIC);
115
-	private final ClassDefinition SYSTEM = new ClassDefinition(CodePosition.NATIVE, MODULE, javaLang, "System", Modifiers.PUBLIC);
116
-	private final MethodMember PRINTSTREAM_PRINTLN;
117
-	private final GetterMember SYSTEM_OUT;
118
-	
119
 	private class PrintlnSymbol implements ISymbol {
112
 	private class PrintlnSymbol implements ISymbol {
120
 
113
 
121
 		@Override
114
 		@Override

+ 6
- 2
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

57
 		//formatFiles(pkg, module);
57
 		//formatFiles(pkg, module);
58
 		
58
 		
59
 		if (semantic.isValid()) {
59
 		if (semantic.isValid()) {
60
-			JavaModule javaModule = compileSemanticToJava(semantic);
60
+			JavaModule javaModule = compileSemanticToJava(registry, semantic);
61
 			javaModule.execute();
61
 			javaModule.execute();
62
 		} else {
62
 		} else {
63
 			System.out.println("There were compilation errors");
63
 			System.out.println("There were compilation errors");
127
 		return result;
127
 		return result;
128
 	}
128
 	}
129
 	
129
 	
130
-	private static JavaModule compileSemanticToJava(SemanticModule module) {
130
+	private static JavaModule compileSemanticToJava(GlobalRegistry registry, SemanticModule module) {
131
 		JavaCompiler compiler = new JavaCompiler(module.compilationUnit.globalTypeRegistry, false, null);
131
 		JavaCompiler compiler = new JavaCompiler(module.compilationUnit.globalTypeRegistry, false, null);
132
+		registry.register(compiler.getContext());
133
+		
134
+		compiler.getContext().addModule(module.module);
135
+		
132
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
136
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
133
 			compiler.addDefinition(definition, module);
137
 			compiler.addDefinition(definition, module);
134
 		}
138
 		}

+ 1
- 1
Shared/src/main/java/org/openzen/zencode/shared/SourceFile.java View File

3
 import java.io.IOException;
3
 import java.io.IOException;
4
 import java.io.Reader;
4
 import java.io.Reader;
5
 
5
 
6
-public interface SourceFile {
6
+public interface SourceFile extends Tag {
7
     String getFilename();
7
     String getFilename();
8
     
8
     
9
     Reader open() throws IOException;
9
     Reader open() throws IOException;

+ 4
- 0
Shared/src/main/java/org/openzen/zencode/shared/Tag.java View File

1
+package org.openzen.zencode.shared;
2
+
3
+public interface Tag {
4
+}

+ 3
- 3
Shared/src/main/java/org/openzen/zencode/shared/Taggable.java View File

6
 public class Taggable {
6
 public class Taggable {
7
     private final Map<Class<?>, Object> tags = new HashMap<Class<?>, Object>();
7
     private final Map<Class<?>, Object> tags = new HashMap<Class<?>, Object>();
8
     
8
     
9
-    public <T> void setTag(Class<T> typeOfT, T tag) {
9
+    public <T extends Tag> void setTag(Class<T> typeOfT, T tag) {
10
         tags.put(typeOfT, tag);
10
         tags.put(typeOfT, tag);
11
     }
11
     }
12
     
12
     
13
-    public <T> T getTag(Class<T> typeOfT) {
13
+    public <T extends Tag> T getTag(Class<T> typeOfT) {
14
         return (T)(tags.get(typeOfT));
14
         return (T)(tags.get(typeOfT));
15
     }
15
     }
16
     
16
     
17
-    public <T> boolean hasTag(Class<T> typeOfT) {
17
+    public <T extends Tag> boolean hasTag(Class<T> typeOfT) {
18
         return tags.containsKey(typeOfT);
18
         return tags.containsKey(typeOfT);
19
     }
19
     }
20
     
20
     

Loading…
Cancel
Save