001: package gnu.kawa.lispexpr;
002:
003: import gnu.bytecode.*;
004: import gnu.mapping.*;
005: import gnu.expr.*;
006: import gnu.text.*;
007:
008: public class LangObjType extends ObjectType implements TypeValue {
009: final int typeCode;
010: private static final int PATH_TYPE_CODE = 1;
011: private static final int FILEPATH_TYPE_CODE = 2;
012: private static final int URI_TYPE_CODE = 3;
013:
014: public static final LangObjType pathType = new LangObjType("path",
015: "gnu.text.Path", PATH_TYPE_CODE);
016: public static final LangObjType filepathType = new LangObjType(
017: "filepath", "gnu.text.FilePath", FILEPATH_TYPE_CODE);
018: public static final LangObjType URIType = new LangObjType("URI",
019: "gnu.text.URIPath", URI_TYPE_CODE);
020:
021: LangObjType(String name, String implClass, int typeCode) {
022: super (name);
023: this .implementationType = ClassType.make(implClass);
024: this .typeCode = typeCode;
025: }
026:
027: ClassType implementationType;
028:
029: public int compare(Type other) {
030: return getImplementationType().compare(other);
031: }
032:
033: public int getMethods(Filter filter, int searchSupers,
034: java.util.Vector result, String context) {
035: return implementationType.getMethods(filter, searchSupers,
036: result, context);
037: }
038:
039: public java.lang.Class getReflectClass() {
040: return implementationType.getReflectClass();
041: }
042:
043: public Type getImplementationType() {
044: return implementationType;
045: }
046:
047: static PrimProcedure makePathProc = new PrimProcedure(
048: "gnu.text.Path", "valueOf", 1);
049: static PrimProcedure makeFilepathProc = new PrimProcedure(
050: "gnu.text.FilePath", "makeFilePath", 1);
051: static PrimProcedure makeURIProc = new PrimProcedure(
052: "gnu.text.URIPath", "makeURI", 1);
053:
054: public void emitIsInstance(Variable incoming, Compilation comp,
055: Target target) {
056: gnu.kawa.reflect.InstanceOf.emitIsInstance(this , incoming,
057: comp, target);
058: }
059:
060: public void emitTestIf(Variable incoming, Declaration decl,
061: Compilation comp) {
062: CodeAttr code = comp.getCode();
063: if (incoming != null)
064: code.emitLoad(incoming);
065: String mname;
066: switch (typeCode) {
067: case PATH_TYPE_CODE:
068: mname = "coerceToPathOrNull";
069: break;
070: case FILEPATH_TYPE_CODE:
071: mname = "coerceToFilePathOrNull";
072: break;
073: case URI_TYPE_CODE:
074: mname = "coerceToURIPathOrNull";
075: break;
076: default:
077: mname = null;
078: }
079: code.emitInvokeStatic(implementationType.getDeclaredMethod(
080: mname, 1));
081: if (decl != null) {
082: code.emitDup();
083: decl.compileStore(comp);
084: }
085: code.emitIfNotNull();
086: }
087:
088: public Object coerceFromObject(Object obj) {
089: switch (typeCode) {
090: case PATH_TYPE_CODE:
091: return Path.valueOf(obj);
092: case FILEPATH_TYPE_CODE:
093: return FilePath.makeFilePath(obj);
094: case URI_TYPE_CODE:
095: return URIPath.makeURI(obj);
096: default:
097: return null;
098: }
099: }
100:
101: public void emitCoerceFromObject(CodeAttr code) {
102: code.emitInvoke(((PrimProcedure) getConstructor()).getMethod());
103: }
104:
105: public Procedure getConstructor() {
106: switch (typeCode) {
107: case PATH_TYPE_CODE:
108: return makePathProc;
109: case FILEPATH_TYPE_CODE:
110: return makeFilepathProc;
111: case URI_TYPE_CODE:
112: return makeURIProc;
113: default:
114: return null;
115: }
116: }
117: }
|