Exemplo n.º 1
0
        public static RecordTypeDescriptor GenerateRecordTypeDescriptor(AssemblyGen ag, object name, object parent, object uid, object issealed, object isopaque, object fields, object fieldtypes)
        {
            string n  = SymbolTable.IdToString(RequiresNotNull <SymbolId>(name));
            string id = uid is SymbolId?SymbolTable.IdToString(RequiresNotNull <SymbolId>(uid)) : uid as string;

            if (id != null)
            {
                RecordTypeDescriptor ngrtd;
                if (nongenerative.TryGetValue(n + id, out ngrtd))
                {
                    return(ngrtd);
                }

                var type = ClrGenerator.GetTypeFast("record." + id + "." + n.Replace("&", "$"));

                if (type != null)
                {
                    return(RecordTypeDescriptor.Create(type, n, id, parent as RecordTypeDescriptor));
                }
            }

            bool @sealed = RequiresNotNull <bool>(issealed);
            bool opaque  = RequiresNotNull <bool>(isopaque);

            RecordTypeDescriptor prtd = parent as RecordTypeDescriptor; // can be #f

            Type parenttype = typeof(object);

            if (prtd != null)
            {
                parenttype = prtd.type;
            }
            else if (n == "&condition")
            {
                parenttype = typeof(Condition);
            }

            TypeAttributes attrs = TypeAttributes.Public | TypeAttributes.Serializable;

            var rtd = new RecordTypeDescriptor
            {
                Name       = n,
                @sealed    = @sealed,
                opaque     = opaque,
                ag         = ag,
                Parent     = prtd,
                uid        = uid,
                generative = id == null || uid is string,
            };

            if (@sealed)
            {
                attrs |= TypeAttributes.Sealed;
            }

            object gid      = (object)id ?? Guid.NewGuid();
            var    ns       = "record." + gid;
            var    typename = ns + "." + n.Replace("&", "$");

            TypeGen tg = ag.DefinePublicType(typename, parenttype, attrs);

            rtd.tg   = tg;
            rtd.type = tg.TypeBuilder;

            if (id != null)
            {
                nongenerative[n + id] = rtd;
            }

            if (parenttype.IsSubclassOf(typeof(Condition)))
            {
                SetSymbolValueFast(SymbolTable.StringToObject(n + "-rtd"), rtd);
            }

            GeneratePredicate(n, rtd, tg);

            GenerateFields(fields, n, rtd, tg, fieldtypes);

            GenerateConstructor(rtd, tg, parenttype);

            return(rtd);
        }