Package anvil.script.statements

Source Code of anvil.script.statements.InterfaceStatement

/*
* $Id: InterfaceStatement.java,v 1.7 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.statements;

import anvil.Location;
import anvil.codec.ConstantPool;
import anvil.codec.Code;
import anvil.codec.ClassRoom;
import anvil.codec.Field;
import anvil.codec.Method;
import anvil.doc.Doc;
import anvil.doc.DocParser;
import anvil.ErrorListener;
import anvil.script.Type;
import anvil.script.InterfaceType;
import anvil.script.InterfaceRef;
import anvil.script.Module;
import anvil.script.compiler.ByteCompiler;
import anvil.script.CompilableFunction;
import anvil.script.expression.Expression;
import anvil.java.util.Hashlist;
import java.io.IOException;
import java.util.Enumeration;

/**
* class InterfaceStatement
*
* @author: Jani Lehtim�ki
*/
public class InterfaceStatement extends DefinitionStatement implements InterfaceType
{

  private InterfaceRef[]      _bases;
 

  public InterfaceStatement(
      Location location,
      DefinitionStatement parent,
      String name,
      String document,
      InterfaceRef[] bases)
  {
    super(parent, location, name, DocParser.parseInterface(name, document));
    _bases = bases;
    _descriptor = _parent.getDescriptor() + "$i_" + _name;
  }
 

  public int typeOf()
  {
    return Statement.ST_INTERFACE;
  }


  public String name()
  {
    return "interface";
  }
 

  public int getType()
  {
    return INTERFACE;
  }
 
 
  public InterfaceRef[] getBases()
  {
    return _bases;
  }


  public Type lookupDeclaration(String name)
  {
    Type type = (Type)_types.get(name);
    if (type != null){
      return type;
    }
    type = lookupInheritedDeclaration(name);
    if (type != null){
      return type;
    }
    return super.lookupDeclaration(name);
  }


  public Type lookupInheritedDeclaration(String name)
  {
    InterfaceType base;
    Type type;
    int n = _bases.length;
    for(int i=0; i<n; i++) {
      if ((base = _bases[i].getInterfaceType()) != null) {
        if ((type = base.lookupDeclaration(name)) != null) {
          return type;
        }
      }
    }
    return null;
  }


  public void check(ErrorListener context)
  {
    super.check(context);
  }
 

  public void compile(ByteCompiler context)
  {
    ClassRoom clazz = context.getClassRoom().createClass(getDescriptor(), "i_"+_name);
    clazz.setAccessFlags(context.ACC_PUBLIC);
    ConstantPool pool = clazz.getPool();
    context.pushClass(clazz);
    Field typefield1 = clazz.createField("_class", "Lanvil/script/compiler/CompiledInterfaceType;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Field typefield2 = clazz.createField("_type", "Lanvil/core/Any;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    clazz.setSuperClassname(context.TYPE_OBJECT);
    clazz.setAccessFlags(Code.ACC_SUPER|Code.ACC_PUBLIC);

    compileMembers(context, _types.size(), _types.elements());

    Field bases = clazz.createField("_bases", "[Ljava/lang/String;", Code.ACC_PUBLIC|Code.ACC_STATIC);
    Code code = clazz.getStatic().getCode();
    context.pushCode(code);

    //code.println("INTERFACE-START:"+getDescriptor());
    code.getstatic(pool.addFieldRef(_parent.getDescriptor(),
      "_members", "[Ljava/lang/Object;"));
    code.pop();

    int n = _bases.length;
    code.iconst(n);
    code.anewarray("java/lang/String");
    for(int i=0; i<n; i++) {
      InterfaceType interfacetype = _bases[i].getInterfaceType();
      code.dup();
      code.iconst(i);
      code.astring(interfacetype.getDescriptor().replace('/', '.'));
      code.aastore();
    }
    code.putstatic(bases);
    //code.println("INTERFACE-END:"+getDescriptor());
    code.vreturn();
    context.popCode();

    super.compile(context);
   
    context.popClass();
  }


}
TOP

Related Classes of anvil.script.statements.InterfaceStatement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.