001: /*
002: * $Id: TypeNode.java,v 1.9 2002/09/16 08:05:05 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.expression;
011:
012: import anvil.core.Any;
013: import anvil.core.runtime.AnyNamespace;
014: import anvil.ErrorListener;
015: import anvil.ErrorListener;
016: import anvil.codec.Code;
017: import anvil.codec.ConstantPool;
018: import anvil.script.compiler.ByteCompiler;
019: import anvil.script.statements.FunctionStatement;
020: import anvil.script.statements.ClassStatement;
021: import anvil.script.ClassType;
022: import anvil.script.Context;
023: import anvil.script.Type;
024: import anvil.script.Import;
025: import anvil.script.Grammar;
026: import anvil.script.NativeJava;
027: import java.io.IOException;
028:
029: /**
030: * class TypeNode
031: *
032: * @author: Jani Lehtimäki
033: */
034: public class TypeNode extends Node {
035:
036: protected Type _type;
037:
038: public TypeNode(Type type) {
039: super ();
040: _type = type;
041: }
042:
043: public int typeOf() {
044: return Node.EXPR_TYPE;
045: }
046:
047: public boolean isConstant() {
048: return false;
049: }
050:
051: public Node optimize() {
052: return this ;
053: }
054:
055: public Type getType() {
056: return _type;
057: }
058:
059: public void compile(ByteCompiler context, int operation) {
060: boolean get_type = (operation == GET_TYPE);
061: Code code = context.getCode();
062: ConstantPool pool = code.getPool();
063: if (_type instanceof NativeJava) {
064: switch (_type.getType()) {
065: case Type.FUNCTION: {
066: code.getstatic(pool.addFieldRef(_type.getParent()
067: .getTypeRef(pool), "__module__",
068: "Lanvil/script/compiler/NativeNamespace;"));
069: code.aload_first();
070: code.astring(_type.getName());
071: int method;
072: if (get_type) {
073: method = pool
074: .addMethodRef(
075: "anvil/script/compiler/NativeNamespace",
076: "getDeclaration",
077: "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/script/Type;");
078: } else {
079: method = pool
080: .addMethodRef(
081: "anvil/script/compiler/NativeNamespace",
082: "getFunction",
083: "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/core/Any;");
084: }
085: code.invokevirtual(method);
086: }
087: break;
088:
089: case Type.CLASS: {
090: code.getstatic(pool.addFieldRef(_type.getTypeRef(pool),
091: "__class__",
092: "Lanvil/script/compiler/NativeClass;"));
093: if (!get_type) {
094: code.invokevirtual(pool.addMethodRef(
095: "anvil/script/compiler/NativeClass",
096: "getWrapper", "()Lanvil/core/Any;"));
097: }
098: }
099: break;
100:
101: case Type.NAMESPACE: {
102: code.getstatic(pool.addFieldRef(_type.getTypeRef(pool),
103: "__module__",
104: "Lanvil/script/compiler/NativeNamespace;"));
105: if (!get_type) {
106: code.invokevirtual(pool.addMethodRef(
107: "anvil/script/compiler/NativeNamespace",
108: "getWrapper", "()Lanvil/core/Any;"));
109: }
110: }
111: break;
112:
113: case Type.METHOD: {
114: code.getstatic(pool.addFieldRef(_type.getParent()
115: .getTypeRef(pool), "__class__",
116: "Lanvil/script/compiler/NativeClass;"));
117: int method;
118: code.aload_first();
119: code.astring(_type.getName());
120: if (get_type) {
121: method = pool
122: .addMethodRef(
123: "anvil/script/compiler/NativeClass",
124: "getDeclaration",
125: "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/script/Type;");
126: } else {
127: method = pool
128: .addMethodRef(
129: "anvil/script/compiler/NativeClass",
130: "getMethod",
131: "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/core/Any;");
132: }
133: code.invokevirtual(method);
134: }
135: break;
136: }
137:
138: } else {
139: switch (_type.getType()) {
140: case Type.FUNCTION: {
141: int field = pool.addFieldRef(_type.getParent()
142: .getTypeRef(pool), (get_type ? "f_" : "F_")
143: + _type.getName(),
144: get_type ? "Lanvil/script/Function;"
145: : "Lanvil/core/Any;");
146: code.getstatic(field);
147: }
148: break;
149:
150: case Type.MEMBER_VARIABLE: {
151: Type parent = _type.getParent();
152: int field = pool.addFieldRef(parent.getTypeRef(pool),
153: "_class",
154: "Lanvil/script/compiler/CompiledClassType;");
155: code.getstatic(field);
156: code.aload_first();
157: code.astring(_type.getName());
158: code
159: .invokevirtual(pool
160: .addMethodRef(
161: "anvil/script/compiler/CompiledClassType",
162: get_type ? "getMember"
163: : "getMemberType",
164: get_type ? "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/script/Type;"
165: : "(Lanvil/script/Context;Ljava/lang/String;)Lanvil/core/Any;"));
166: }
167: break;
168:
169: case Type.INTERFACE_METHOD:
170: case Type.METHOD:
171: case Type.CONSTRUCTOR: {
172: int field = pool.addFieldRef(_type.getParent()
173: .getTypeRef(pool), (get_type ? "m_" : "M_")
174: + _type.getName(),
175: get_type ? "Lanvil/script/Function;"
176: : "Lanvil/core/Any;");
177: code.getstatic(field);
178: }
179: break;
180:
181: case Type.CLASS: {
182: if (get_type) {
183: int field = pool
184: .addFieldRef(_type.getTypeRef(pool),
185: "_class",
186: "Lanvil/script/compiler/CompiledClassType;");
187: code.getstatic(field);
188: } else {
189: int field = pool.addFieldRef(
190: _type.getTypeRef(pool), "_type",
191: "Lanvil/core/Any;");
192: code.getstatic(field);
193: }
194: }
195: break;
196:
197: case Type.INTERFACE: {
198: int field = pool
199: .addFieldRef(
200: _type.getTypeRef(pool),
201: get_type ? "_class" : "_type",
202: get_type ? "Lanvil/script/compiler/CompiledInterfaceType;"
203: : "Lanvil/core/Any;");
204: code.getstatic(field);
205: }
206: break;
207:
208: case Type.NAMESPACE: {
209: int field = pool
210: .addFieldRef(
211: _type.getTypeRef(pool),
212: get_type ? "_class" : "_type",
213: get_type ? "Lanvil/script/compiler/CompiledNamespace;"
214: : "Lanvil/core/Any;");
215: code.getstatic(field);
216: }
217: break;
218:
219: case Type.MODULE: {
220: int field = code
221: .getPool()
222: .addFieldRef(
223: _type.getTypeRef(pool),
224: get_type ? "_module" : "_type",
225: get_type ? "Lanvil/script/compiler/CompiledModule;"
226: : "Lanvil/core/Any;");
227: code.getstatic(field);
228: }
229: break;
230:
231: }
232: }
233: if (operation == GET_BOOLEAN) {
234: context.any2boolean();
235: }
236:
237: }
238:
239: }
|