001: /*
002: * $Id: InterfaceStatement.java,v 1.7 2002/09/16 08:05:06 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.statements;
011:
012: import anvil.Location;
013: import anvil.codec.ConstantPool;
014: import anvil.codec.Code;
015: import anvil.codec.ClassRoom;
016: import anvil.codec.Field;
017: import anvil.codec.Method;
018: import anvil.doc.Doc;
019: import anvil.doc.DocParser;
020: import anvil.ErrorListener;
021: import anvil.script.Type;
022: import anvil.script.InterfaceType;
023: import anvil.script.InterfaceRef;
024: import anvil.script.Module;
025: import anvil.script.compiler.ByteCompiler;
026: import anvil.script.CompilableFunction;
027: import anvil.script.expression.Expression;
028: import anvil.java.util.Hashlist;
029: import java.io.IOException;
030: import java.util.Enumeration;
031:
032: /**
033: * class InterfaceStatement
034: *
035: * @author: Jani Lehtimäki
036: */
037: public class InterfaceStatement extends DefinitionStatement implements
038: InterfaceType {
039:
040: private InterfaceRef[] _bases;
041:
042: public InterfaceStatement(Location location,
043: DefinitionStatement parent, String name, String document,
044: InterfaceRef[] bases) {
045: super (parent, location, name, DocParser.parseInterface(name,
046: document));
047: _bases = bases;
048: _descriptor = _parent.getDescriptor() + "$i_" + _name;
049: }
050:
051: public int typeOf() {
052: return Statement.ST_INTERFACE;
053: }
054:
055: public String name() {
056: return "interface";
057: }
058:
059: public int getType() {
060: return INTERFACE;
061: }
062:
063: public InterfaceRef[] getBases() {
064: return _bases;
065: }
066:
067: public Type lookupDeclaration(String name) {
068: Type type = (Type) _types.get(name);
069: if (type != null) {
070: return type;
071: }
072: type = lookupInheritedDeclaration(name);
073: if (type != null) {
074: return type;
075: }
076: return super .lookupDeclaration(name);
077: }
078:
079: public Type lookupInheritedDeclaration(String name) {
080: InterfaceType base;
081: Type type;
082: int n = _bases.length;
083: for (int i = 0; i < n; i++) {
084: if ((base = _bases[i].getInterfaceType()) != null) {
085: if ((type = base.lookupDeclaration(name)) != null) {
086: return type;
087: }
088: }
089: }
090: return null;
091: }
092:
093: public void check(ErrorListener context) {
094: super .check(context);
095: }
096:
097: public void compile(ByteCompiler context) {
098: ClassRoom clazz = context.getClassRoom().createClass(
099: getDescriptor(), "i_" + _name);
100: clazz.setAccessFlags(context.ACC_PUBLIC);
101: ConstantPool pool = clazz.getPool();
102: context.pushClass(clazz);
103: Field typefield1 = clazz.createField("_class",
104: "Lanvil/script/compiler/CompiledInterfaceType;",
105: Code.ACC_PUBLIC | Code.ACC_STATIC);
106: Field typefield2 = clazz.createField("_type",
107: "Lanvil/core/Any;", Code.ACC_PUBLIC | Code.ACC_STATIC);
108: clazz.setSuperClassname(context.TYPE_OBJECT);
109: clazz.setAccessFlags(Code.ACC_SUPER | Code.ACC_PUBLIC);
110:
111: compileMembers(context, _types.size(), _types.elements());
112:
113: Field bases = clazz.createField("_bases",
114: "[Ljava/lang/String;", Code.ACC_PUBLIC
115: | Code.ACC_STATIC);
116: Code code = clazz.getStatic().getCode();
117: context.pushCode(code);
118:
119: //code.println("INTERFACE-START:"+getDescriptor());
120: code.getstatic(pool.addFieldRef(_parent.getDescriptor(),
121: "_members", "[Ljava/lang/Object;"));
122: code.pop();
123:
124: int n = _bases.length;
125: code.iconst(n);
126: code.anewarray("java/lang/String");
127: for (int i = 0; i < n; i++) {
128: InterfaceType interfacetype = _bases[i].getInterfaceType();
129: code.dup();
130: code.iconst(i);
131: code.astring(interfacetype.getDescriptor()
132: .replace('/', '.'));
133: code.aastore();
134: }
135: code.putstatic(bases);
136: //code.println("INTERFACE-END:"+getDescriptor());
137: code.vreturn();
138: context.popCode();
139:
140: super.compile(context);
141:
142: context.popClass();
143: }
144:
145: }
|