Example #1
0
        protected static bool RecursiveMerge(ref ScopeMember res, ScopeMember level)
        {
            if(level != null && res != null)
            {
                if(res.IsTypeGroup() && level.IsTypeGroup())
                {
                    // Merge type groups.
                    TypeGroup lower = (TypeGroup)res;
                    TypeGroup next = (TypeGroup)level;
                    if(!lower.IsMergedGroup())
                        res = lower.CreateMerged(next, false);
                    else
                        lower.AppendLevel(next, false);
                }
                else if(res.IsFunctionGroup() && level.IsFunctionGroup())
                {
                    // Merge function groups.
                    FunctionGroup lower = (FunctionGroup)res;
                    FunctionGroup next = (FunctionGroup)level;
                    if(!lower.IsMergedGroup())
                        res = lower.CreateMerged(next, false);
                    else
                        lower.AppendLevel(next, false);
                }
            }
            else if(res == null)
            {
                // Set the result to the next level.
                res = level;
            }

            return IsRecursiveContinue(res);
        }
Example #2
0
        public void AddType(Structure type)
        {
            // Find the previous group.
            ScopeMember oldMember = FindMember(type.GetName());

            if (oldMember != null)
            {
                if (!oldMember.IsTypeGroup())
                {
                    throw new ModuleException("expected type group.");
                }

                // Find the previous definition.
                TypeGroup oldGroup = (TypeGroup)oldMember;
                if (oldGroup.Find(type.GetGenericPrototype()) != null)
                {
                    throw new ModuleException("matching type already exists.");
                }

                // Add the type into the group.
                oldGroup.Insert(type);
            }
            else
            {
                // Create a new type group.
                TypeGroup newGroup = new TypeGroup(type.GetName(), this);
                newGroup.Insert(type);
                AddMember(newGroup);
            }
        }
Example #3
0
 public StructureInstance(Structure template, GenericInstance instance,
                          ScopeMember factory, ChelaModule module)
     : base(module)
 {
     this.factory = factory;
     Initialize(template, instance);
 }
Example #4
0
        private void ReadData()
        {
            // Only read once the data.
            if (readedData)
            {
                return;
            }
            readedData = true;

            ModuleReader reader = new ModuleReader(new MemoryStream(rawData));

            // Get the module.
            ChelaModule module = GetModule();

            // Read the template.
            template = (Structure)module.GetMember(reader.ReadUInt());

            // Read the factory.
            factory = (Scope)module.GetMember(reader.ReadUInt());

            // Read the generic instance.
            genericInstance = GenericInstance.Read(template.GetGenericPrototype(), reader, module);

            // Initialize.
            Initialize(template, genericInstance);
            rawData = null;
        }
Example #5
0
 public StructureInstance(Structure template, GenericInstance instance,
     ScopeMember factory, ChelaModule module)
     : base(module)
 {
     this.factory = factory;
     Initialize(template, instance);
 }
Example #6
0
        public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
        {
            // Instance the instance.
            ChelaModule     module      = factory.GetModule();
            GenericInstance newInstance = genericInstance.InstanceFrom(instance, module);

            // Instance the structure.
            return(template.InstanceMember(factory, newInstance));
        }
Example #7
0
 public override bool IsInstanceOf(ScopeMember template)
 {
     if (template == this.template)
     {
         return(true);
     }
     if (factory != null)
     {
         return(factory.IsInstanceOf(this.template));
     }
     return(false);
 }
Example #8
0
        public void AddMember(ScopeMember member)
        {
            if(member == null)
                return;
            this.members.Add(member.GetName(), member);

            // Store the static constructor.
            if(member.IsFunction())
            {
                Function function = (Function)member;
                if(function.IsStaticConstructor())
                    staticConstructor = function;
            }
        }
Example #9
0
        public void AddMember(ScopeMember member)
        {
            if (member == null)
            {
                return;
            }
            this.members.Add(member.GetName(), member);

            // Store the static constructor.
            if (member.IsFunction())
            {
                Function function = (Function)member;
                if (function.IsStaticConstructor())
                {
                    staticConstructor = function;
                }
            }
        }
Example #10
0
        protected static bool RecursiveMerge(ref ScopeMember res, ScopeMember level)
        {
            if (level != null && res != null)
            {
                if (res.IsTypeGroup() && level.IsTypeGroup())
                {
                    // Merge type groups.
                    TypeGroup lower = (TypeGroup)res;
                    TypeGroup next  = (TypeGroup)level;
                    if (!lower.IsMergedGroup())
                    {
                        res = lower.CreateMerged(next, false);
                    }
                    else
                    {
                        lower.AppendLevel(next, false);
                    }
                }
                else if (res.IsFunctionGroup() && level.IsFunctionGroup())
                {
                    // Merge function groups.
                    FunctionGroup lower = (FunctionGroup)res;
                    FunctionGroup next  = (FunctionGroup)level;
                    if (!lower.IsMergedGroup())
                    {
                        res = lower.CreateMerged(next, false);
                    }
                    else
                    {
                        lower.AppendLevel(next, false);
                    }
                }
            }
            else if (res == null)
            {
                // Set the result to the next level.
                res = level;
            }

            return(IsRecursiveContinue(res));
        }
Example #11
0
        public virtual Structure FindType(string name, GenericPrototype prototype)
        {
            // Find the member.
            ScopeMember member = FindMember(name);

            if (member == null)
            {
                return(null);
            }

            // Use the matching type in the type group.
            if (member.IsTypeGroup())
            {
                TypeGroup group = (TypeGroup)member;
                return(group.Find(prototype));
            }
            else if (!member.IsClass() && !member.IsInterface() && !member.IsStructure())
            {
                throw new ModuleException("found no structural type " + member.GetFullName());
            }

            // Cast the member.
            return((Structure)member);
        }
Example #12
0
        public override ScopeMember FindMember(string member)
        {
            // Find the instanced member.
            ScopeMember instanced;

            if (this.members.TryGetValue(member, out instanced))
            {
                return(instanced);
            }

            // Find the templated member.
            ScopeMember templated = template.FindMember(member);

            if (templated != null)
            {
                // Instance the templated member.
                instanced = templated.InstanceMember(this, GetFullGenericInstance());
                this.members.Add(member, instanced);
                return(instanced);
            }

            // Couldn't find.
            return(null);
        }
Example #13
0
        private void InstanceMembers()
        {
            if (instancedMembers)
            {
                return;
            }
            instancedMembers = true;

            // Instance all of the members.
            foreach (ScopeMember member in template.memberList)
            {
                string name = member.GetName();
                if (!members.ContainsKey(name))
                {
                    ScopeMember instanced = member.InstanceMember(this, GetFullGenericInstance());
                    this.members.Add(member.GetName(), instanced);
                    this.memberList.Add(instanced);
                }
                else
                {
                    this.memberList.Add(members[name]);
                }
            }
        }
Example #14
0
        public override ScopeMember FindMemberRecursive(string member)
        {
            // Find the member here.
            ScopeMember ret = FindMember(member);

            if (ret != null)
            {
                return(ret);
            }

            // Find the member in the sister namespaces.
            ChelaModule myModule = GetModule();
            string      myname   = GetFullName();

            foreach (ChelaModule refMod in ChelaModule.GetLoadedModules())
            {
                if (refMod == myModule)
                {
                    continue;
                }

                ScopeMember sister = refMod.GetMember(myname);
                if (sister != null && sister.IsNamespace())
                {
                    Namespace test = (Namespace)sister;
                    ret = test.FindMember(member);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            }

            // Couldn't find the member.
            return(null);
        }
Example #15
0
 public virtual bool IsInstanceOf(ScopeMember template)
 {
     return(false);
 }
Example #16
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return null;//new FunctionGroupInstance (this, instance, factory);
 }
Example #17
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return(new PropertyInstance(factory, instance, this));
 }
Example #18
0
 public virtual ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return(this);
 }
Example #19
0
        internal void PrepareSerialization(ChelaModule module)
        {
            // Register types and external members used.
            InstructionDescription desc = InstructionDescription.GetInstructionTable()[(int)opcode];

            InstructionArgumentType[] args = desc.GetArguments();
            for (int i = 0, k = 0; i < arguments.Length; i++, k++)
            {
                object arg = arguments[i];
                switch (args[k])
                {
                case InstructionArgumentType.UInt8V:
                case InstructionArgumentType.Int8V:
                case InstructionArgumentType.UInt16V:
                case InstructionArgumentType.Int16V:
                case InstructionArgumentType.UInt32V:
                case InstructionArgumentType.Int32V:
                case InstructionArgumentType.UInt64V:
                case InstructionArgumentType.Int64V:
                case InstructionArgumentType.Fp32V:
                case InstructionArgumentType.Fp64V:
                {
                    // Ignore the vector arguments.
                    byte n = (byte)arg;
                    i += n;
                }
                break;

                case InstructionArgumentType.TypeID:
                {
                    // Prepare serialization of generic instances.
                    IChelaType argType = (IChelaType)arg;
                    if (argType.IsGenericInstance() &&
                        (argType.IsStructure() || argType.IsClass() || argType.IsInterface()))
                    {
                        ScopeMember member = (ScopeMember)argType;
                        member.PrepareSerialization();
                    }

                    // Register types.
                    module.RegisterType(argType);
                }
                break;

                case InstructionArgumentType.GlobalID:
                case InstructionArgumentType.FieldID:
                case InstructionArgumentType.FunctionID:
                {
                    // Register external members.
                    ScopeMember member = (ScopeMember)arg;
                    if (member != null)
                    {
                        if (member.IsGenericInstance())
                        {
                            member.PrepareSerialization();
                        }
                        module.RegisterMember(member);
                    }
                }
                break;

                case InstructionArgumentType.JumpTable:
                {
                    // Ignore the jump table
                    ushort tableLen = (ushort)arg;
                    i += tableLen * 2;
                }
                break;

                default:
                    // Do nothing.
                    break;
                }
            }
        }
Example #20
0
        public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
        {
            // Instance the instance.
            ChelaModule module = factory.GetModule();
            GenericInstance newInstance = genericInstance.InstanceFrom(instance, module);

            // Instance the structure.
            return template.InstanceMember(factory, newInstance);
        }
Example #21
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return new PropertyInstance(factory, instance, this);
 }
Example #22
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance genericInstance)
 {
     return new FunctionInstance(this, genericInstance, factory);
 }
Example #23
0
 public virtual bool IsInstanceOf(ScopeMember template)
 {
     return false;
 }
Example #24
0
 protected static bool IsRecursiveContinue(ScopeMember member)
 {
     return member == null || member.IsTypeGroup() || member.IsFunctionGroup();
 }
Example #25
0
        public void CheckMemberVisibility(AstNode node, ScopeMember member)
        {
            // Ignore placeholder types.
            if(member.IsType())
            {
                IChelaType type = (IChelaType)member;
                if(type.IsPlaceHolderType())
                    return;
            }

            // Special treatment for scope member.
            if(member.IsScope())
            {
                CheckScopeVisibility(node, (Scope)member);
                return;
            }

            // Check the parent scope access.
            Scope parentScope = member.GetParentScope();
            if(parentScope != null)
                CheckScopeVisibility(node, parentScope);

            // Check the actual member visibility.
            if(member.IsPrivate())
            {
                // Walk the scope hierarchy.
                Scope scope = currentScope;
                while(scope != null)
                {
                    // Found the defining scope, stop checking.
                    if(scope == parentScope ||
                       parentScope.IsInstanceOf(scope))
                        return;

                    // Found the scope.
                    scope = scope.GetParentScope();
                }

                // Couldn't find scope, raise error.
                Error(node, "cannot access private member " + member.GetFullName());
            }
            else if(member.IsProtected())
            {
                // The parent scope must be a Structure derivative.
                Structure parentBuilding = (Structure)parentScope;

                // Walk the scope hierarchy.
                Scope scope = currentScope;
                while(scope != null)
                {
                    // Found the defining scope, stop checking.
                    if(scope is Structure)
                    {
                        Structure derived = (Structure)scope;
                        if(derived == parentBuilding ||
                           derived.IsDerivedFrom(parentBuilding))
                            return;
                    }

                    // Found the scope.
                    scope = scope.GetParentScope();
                }

                // Couldn't find scope, raise error.
                Error(node, "cannot access protected member " + member.GetFullName());
            }
            else if(member.IsInternal())
            {
                if(member.GetModule() != currentModule)
                    Error(node, "cannot access internal member " + member.GetFullName() + " of another module.");
            }
        }
Example #26
0
 protected static bool IsRecursiveContinue(ScopeMember member)
 {
     return(member == null || member.IsTypeGroup() || member.IsFunctionGroup());
 }
Example #27
0
        private void ReadData()
        {
            // Only read once the data.
            if(readedData)
                return;
            readedData = true;

            ModuleReader reader = new ModuleReader(new MemoryStream(rawData));

            // Get the module.
            ChelaModule module = GetModule();

            // Read the template.
            template = (Structure)module.GetMember(reader.ReadUInt());

            // Read the factory.
            factory = (Scope)module.GetMember(reader.ReadUInt());

            // Read the generic instance.
            genericInstance = GenericInstance.Read(template.GetGenericPrototype(), reader, module);

            // Initialize.
            Initialize(template, genericInstance);
            rawData = null;
        }
Example #28
0
        /// <summary>
        /// Registers a member in the module.
        /// </summary>
        public uint RegisterMember(ScopeMember member)
        {
            // Use the null member.
            if(member == null)
                return 0;

            // Find the already registered member.
            uint res;
            if(registeredMembers.TryGetValue(member, out res))
                return res;

            // Don't allow registering new members.
            if(writingModule)
                throw new BugException("Writing unregistered member " + member.GetFullName());

            // Add into the member table.
            res = (uint)(memberTable.Count + 1);
            memberTable.Add(member);

            // Prevent multiple definitions.
            registeredMembers.Add(member, res);

            // Return the member id.
            return res;
        }
Example #29
0
 private bool CheckTypeHint(ScopeMember value, bool typeHint)
 {
     return value != null && (!typeHint || value.IsType() || value.IsNamespace() || value.IsTypeName());
 }
Example #30
0
 public void RegisterGenericInstance(ScopeMember instance)
 {
     genericInstances.Add(instance);
 }
Example #31
0
 public override bool IsInstanceOf(ScopeMember template)
 {
     if(template == this.template)
         return true;
     if(factory != null)
         return factory.IsInstanceOf(this.template);
     return false;
 }
Example #32
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return(null);//new FunctionGroupInstance (this, instance, factory);
 }
Example #33
0
 public override ScopeMember InstanceMember(ScopeMember factory, GenericInstance genericInstance)
 {
     return(new FunctionInstance(this, genericInstance, factory));
 }
Example #34
0
        public Structure ExtractClass(AstNode where, ScopeMember member)
        {
            // Make sure its a structure or type group.
            if(!member.IsClass() && !member.IsStructure() &&!member.IsInternal() &&
                !member.IsTypeGroup())
                Error(where, "coudn't load runtime class.");

            // Read the type group.
            if(member.IsTypeGroup())
            {
                TypeGroup group = (TypeGroup)member;
                Structure building = group.GetDefaultType();
                if(building == null)
                    Error(where, "unexpected type group {0}", @group.GetDisplayName());

                // Prevent ambiguity of merged type group.
                building.CheckAmbiguity(where.GetPosition());
                return building;
            }

            return (Structure)member;
        }
Example #35
0
        public void Write(ChelaModule module, ModuleWriter writer)
        {
            // Write the opcode.
            writer.Write((byte)opcode);

            // Write the instruction arguments.
            InstructionDescription desc = InstructionDescription.GetInstructionTable()[(int)opcode];

            InstructionArgumentType[] args = desc.GetArguments();
            byte n;

            for (int i = 0, k = 0; i < arguments.Length; i++, k++)
            {
                object arg = arguments[i];
                switch (args[k])
                {
                case InstructionArgumentType.UInt8:
                    writer.Write((byte)arg);
                    break;

                case InstructionArgumentType.UInt8V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((byte)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int8:
                    writer.Write((sbyte)arg);
                    break;

                case InstructionArgumentType.Int8V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((sbyte)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt16:
                    writer.Write((ushort)arg);
                    break;

                case InstructionArgumentType.UInt16V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((ushort)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int16:
                    writer.Write((short)arg);
                    break;

                case InstructionArgumentType.Int16V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((short)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt32:
                    writer.Write((uint)arg);
                    break;

                case InstructionArgumentType.UInt32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((uint)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int32:
                    writer.Write((int)arg);
                    break;

                case InstructionArgumentType.Int32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((int)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt64:
                    writer.Write((ulong)arg);
                    break;

                case InstructionArgumentType.UInt64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((ulong)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int64:
                    writer.Write((long)arg);
                    break;

                case InstructionArgumentType.Int64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((long)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Fp32:
                    writer.Write((float)arg);
                    break;

                case InstructionArgumentType.Fp32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((float)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Fp64:
                    writer.Write((double)arg);
                    break;

                case InstructionArgumentType.Fp64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((double)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.TypeID:
                {
                    uint tyid = module.RegisterType((IChelaType)arg);
                    writer.Write(tyid);
                }
                break;

                case InstructionArgumentType.GlobalID:
                case InstructionArgumentType.FieldID:
                case InstructionArgumentType.FunctionID:
                {
                    ScopeMember member = (ScopeMember)arg;
                    uint        id     = member != null?module.RegisterMember(member) : 0u;

                    writer.Write(id);
                }
                break;

                case InstructionArgumentType.StringID:
                {
                    uint sid = module.RegisterString((string)arg);
                    writer.Write(sid);
                }
                break;

                case InstructionArgumentType.BasicBlockID:
                {
                    BasicBlock bb = (BasicBlock)arg;
                    writer.Write((ushort)bb.GetIndex());
                }
                break;

                case InstructionArgumentType.JumpTable:
                {
                    ushort tableLen = (ushort)arg;
                    writer.Write(tableLen); ++i;
                    for (int j = 0; j < tableLen; j++)
                    {
                        int constant = (int)arguments[i++];
                        writer.Write((int)constant);
                        BasicBlock bb = (BasicBlock)arguments[i++];
                        writer.Write((ushort)bb.GetIndex());
                    }
                }
                break;
                }
            }
        }
Example #36
0
 public void AddAlias(string alias, ScopeMember member)
 {
     this.members.Add(alias, member);
 }
Example #37
0
 public virtual ScopeMember InstanceMember(ScopeMember factory, GenericInstance instance)
 {
     return this;
 }