fan-1.0 /src/sys/dotnet/fan/sys/Type.cs

Language C# Lines 357
MD5 Hash 1de9a66a162f29fb1e87be879c598ebd Estimated Cost $4,776 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   14 Sep 06  Andy Frank  Creation
//

using System.Collections;
using System.Runtime.CompilerServices;
using Fanx.Emit;
using Fanx.Fcode;
using Fanx.Serial;
using Fanx.Util;

namespace Fan.Sys
{
  /// <summary>
  /// Type models a static type definition for an Obj class.  A Type lifecycle:
  ///
  ///  1) Hollow: in this state we know basic identity of the type, and
  ///     it's inheritance hierarchy.  A type is setup to be hollow during
  ///     Pod.load().
  ///  2) Reflected: in this state we read all the slot definitions from the
  ///     fcode to populate the slot tables used to for reflection.  At this
  ///     point clients can discover the signatures of the Type.
  ///  3) Emitted: the final state of loading a Type is to emit to a Java
  ///     class called "fan.{pod}.{type}".  Once emitted we can instantiate
  ///     the type or call it's methods.
  ///  4) Finished: once we have reflected the slots into memory and emitted
  ///     the Java class, the last stage is to bind the all the java.lang.reflect
  ///     representations to the Slots for dynamic dispatch.  We delay this
  ///     until needed by Method or Field for a reflection invocation
  ///
  ///  Type models sys::Type.  Implementation classes are:
  ///   - ClassType
  ///   - GenericType (ListType, MapType, FuncType)
  ///   - NullableType
  /// </summary>
  public abstract class Type : FanObj, Literal
  {

  //////////////////////////////////////////////////////////////////////////
  // Management
  //////////////////////////////////////////////////////////////////////////

    public static Type of(object obj)
    {
      if (obj is FanObj)
        return ((FanObj)obj).@typeof();
      else
        return FanUtil.toFanType(obj.GetType(), true);
    }

    public static Type find(string sig) { return TypeParser.load(sig, true, null); }
    public static Type find(string sig, bool check) { return TypeParser.load(sig, check, null); }

  //////////////////////////////////////////////////////////////////////////
  // Naming
  //////////////////////////////////////////////////////////////////////////

    public override Type @typeof() { return Sys.TypeType; }

    public abstract Pod pod();
    public abstract string name();
    public abstract string qname();
    public abstract string signature();

  //////////////////////////////////////////////////////////////////////////
  // Flags
  //////////////////////////////////////////////////////////////////////////

    public bool isAbstract()  { return (flags() & FConst.Abstract)  != 0; }
    public bool isClass()     { return (flags() & (FConst.Enum|FConst.Mixin)) == 0; }
    public bool isConst()     { return (flags() & FConst.Const)     != 0; }
    public bool isEnum()      { return (flags() & FConst.Enum)      != 0; }
    public bool isFacet()     { return (flags() & FConst.Facet)     != 0; }
    public bool isFinal()     { return (flags() & FConst.Final)     != 0; }
    public bool isInternal()  { return (flags() & FConst.Internal)  != 0; }
    public bool isMixin()     { return (flags() & FConst.Mixin)     != 0; }
    public bool isPublic()    { return (flags() & FConst.Public)    != 0; }
    public bool isSynthetic() { return (flags() & FConst.Synthetic) != 0; }
    internal abstract int flags();

    public override object trap(string name, List args)
    {
      // private undocumented access
      if (name == "flags") return Long.valueOf(flags());
      return base.trap(name, args);
    }

  //////////////////////////////////////////////////////////////////////////
  // Value Types
  //////////////////////////////////////////////////////////////////////////

    public virtual bool isVal()
    {
      return this == Sys.BoolType || this == Sys.IntType || this == Sys.FloatType;
    }

  //////////////////////////////////////////////////////////////////////////
  // Nullable
  //////////////////////////////////////////////////////////////////////////

    public virtual bool isNullable() { return false; }

    public virtual Type toNonNullable() { return this; }

    public abstract Type toNullable();

  //////////////////////////////////////////////////////////////////////////
  // Generics
  //////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// A generic type means that one or more of my slots contain signatures
    /// using a generic parameter (such as V or K).  Fantom supports three built-in
    /// generic types: List, Map, and Method.  A generic instance (such as string[])
    /// is NOT a generic type (all of its generic parameters have been filled in).
    /// User defined generic types are not supported in Fan.
    /// </summary>
    public virtual bool isGenericType()
    {
      return this == Sys.ListType || this == Sys.MapType || this == Sys.FuncType;
    }

    /// <summary>
    /// A generic instance is a type which has "instantiated" a generic type
    /// and replaced all the generic parameter types with generic argument
    /// types.  The type string[] is a generic instance of the generic type
    /// List (V is replaced with string).  A generic instance always has a signature
    /// which different from the qname.
    /// </summary>
    public virtual bool isGenericInstance()
    {
      return false;
    }

    /// <summary>
    /// Return if this type is a generic parameter (such as V or K) in a
    /// generic type (List, Map, or Method).  Generic parameters serve
    /// as place holders for the parameterization of the generic type.
    /// Fantom has a predefined set of generic parameters which are always
    /// defined in the sys pod with a one character name.
    /// </summary>
    public virtual bool isGenericParameter()
    {
      return pod() == Sys.m_sysPod && name().Length == 1;
    }

    /// <summary>
    /// If this type is a generic parameter (V, L, etc), then return
    /// the actual type used in the Java method.  For example V is Obj,
    /// and L is List.  This is the type we actually use when constructing
    /// a signature for the invoke opcode.
    /// </summary>
    public virtual Type getRawType()
    {
      if (!isGenericParameter()) return this;
      if (this == Sys.LType)  return Sys.ListType;
      if (this == Sys.MType)  return Sys.MapType;
      if (this is ListType)   return Sys.ListType;
      if (this is MapType)    return Sys.MapType;
      if (this is FuncType) return Sys.FuncType;
      return Sys.ObjType;
    }

    public bool isGeneric()
    {
      return isGenericType();
    }

    public virtual Map @params()
    {
      if (noParams == null)
        noParams = new Map(Sys.StrType, Sys.TypeType).ro();
      return (Map)noParams;
    }

    public virtual Type parameterize(Map pars)
    {
      if (this == Sys.ListType)
      {
        Type v = (Type)pars.get(FanStr.m_ascii['V']);
        if (v == null) throw ArgErr.make("List.parameterize - V undefined").val;
        return v.toListOf();
      }

      if (this == Sys.MapType)
      {
        Type v = (Type)pars.get(FanStr.m_ascii['V']);
        Type k = (Type)pars.get(FanStr.m_ascii['K']);
        if (v == null) throw ArgErr.make("Map.parameterize - V undefined").val;
        if (k == null) throw ArgErr.make("Map.parameterize - K undefined").val;
        return new MapType(k, v);
      }

      if (this == Sys.FuncType)
      {
        Type r = (Type)pars.get(FanStr.m_ascii['R']);
        if (r == null) throw ArgErr.make("Map.parameterize - R undefined").val;
        ArrayList p = new ArrayList();
        for (int i='A'; i<='H'; ++i)
        {
          Type x = (Type)pars.get(FanStr.m_ascii[i]);
          if (x == null) break;
          p.Add(x);
        }
        return new FuncType((Type[])p.ToArray(System.Type.GetType("Fan.Sys.Type")), r);
      }

      throw UnsupportedErr.make("not generic: " + this).val;
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    public Type toListOf()
    {
      if (m_listOf == null) m_listOf = new ListType(this);
      return m_listOf;
    }

    public List emptyList()
    {
      if (m_emptyList == null) m_emptyList = (List)new List(this, 0).toImmutable();
      return m_emptyList;
    }

  //////////////////////////////////////////////////////////////////////////
  // Slots
  //////////////////////////////////////////////////////////////////////////

    public abstract List fields();
    public abstract List methods();
    public abstract List slots();

    public Field field(string name) { return (Field)slot(name, true); }
    public Field field(string name, bool check) { return (Field)slot(name, check); }

    public Method method(string name) { return (Method)slot(name, true); }
    public Method method(string name, bool check) { return (Method)slot(name, check); }

    public Slot slot(string name) { return slot(name, true); }
    public abstract Slot slot(string name, bool check);

    public object make() { return make(null); }
    public virtual object make(List args)
    {
      Method make = method("make", false);
      if (make != null && make.isPublic())
      {
        int numArgs = args == null ? 0 : args.sz();
        List p = make.@params();
        if ((numArgs == p.sz()) ||
            (numArgs < p.sz() && ((Param)p.get(numArgs)).hasDefault()))
          return make.m_func.callList(args);
      }

      Slot defVal = slot("defVal", false);
      if (defVal is Field) return ((Field)defVal).get(null);
      if (defVal is Method) return ((Method)defVal).m_func.callList(null);

      throw Err.make("Type missing 'make' or 'defVal' slots: " + this).val;
   }

  //////////////////////////////////////////////////////////////////////////
  // Inheritance
  //////////////////////////////////////////////////////////////////////////

    public abstract Type @base();

    public abstract List mixins();

    public abstract List inheritance();

    public bool fits(Type type) { return @is(type); }
    public abstract bool @is(Type type);

    /// <summary>
    /// Given a list of objects, compute the most specific type which they all
    /// share,or at worst return sys::Obj.  This method does not take into
    /// account interfaces, only extends class inheritance.
    /// </summary>
    public static Type common(object[] objs, int n)
    {
      if (objs.Length == 0) return Sys.ObjType.toNullable();
      bool nullable = false;
      Type best = null;
      for (int i=0; i<n; ++i)
      {
        object obj = objs[i];
        if (obj == null) { nullable = true; continue; }
        Type t = @typeof(obj);
        if (best == null) { best = t; continue; }
        while (!t.@is(best))
        {
          best = best.@base();
          if (best == null) return nullable ? Sys.ObjType.toNullable() : Sys.ObjType;
        }
      }
      if (best == null) best = Sys.ObjType;
      return nullable ? best.toNullable() : best;
    }

  //////////////////////////////////////////////////////////////////////////
  // Facets
  //////////////////////////////////////////////////////////////////////////

    public abstract List facets();

    public Facet facet(Type t) { return facet(t, true); }
    public abstract Facet facet(Type t, bool c);

    public bool hasFacet(Type t) { return facet(t, false) != null; }

  //////////////////////////////////////////////////////////////////////////
  // Documentation
  //////////////////////////////////////////////////////////////////////////

    public abstract string doc();

  //////////////////////////////////////////////////////////////////////////
  // Conversion
  //////////////////////////////////////////////////////////////////////////

    public override string toStr() { return signature(); }

    public string toLocale() { return signature(); }

    public void encode(ObjEncoder @out)
    {
      @out.w(signature()).w("#");
    }

  //////////////////////////////////////////////////////////////////////////
  // Reflection
  //////////////////////////////////////////////////////////////////////////

    // TODO - needs to be public, since we need to
    // call this from FTypeEmit for now
    public /*protected*/ virtual Type reflect() { return this; }

    public virtual void finish() {}

    public abstract bool dotnetRepr();

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    internal static readonly bool Debug = false;
    internal static object noParams;

    Type m_listOf;     // cached value of toListOf()
    List m_emptyList;  // cached value of emptyList()

  }
}
Back to Top