Example #1
0
        public NonstaticFunctionType(IdContainer Container, Identifier Child, bool DoUpdate = true)
            : base(Container, new CodeString(), true)
        {
            Children = new Identifier[1] {
                Child
            };
            if (Child != null && !(Child is TypeOfFunction))
            {
                throw new ArgumentException(null, "Child");
            }

            this.UndeclaredIdType = UndeclaredIdType.NonstaticFunctionType;
            var Scope = new StructuredScope(Container, new CodeString(), this);

            StructuredScope = Scope;

            var SelfType = Container.GlobalContainer.CommonIds.Object;
            var Self     = new MemberVariable(Scope, new CodeString("Self"), SelfType);

            Self.Access = IdentifierAccess.Public;
            Scope.IdentifierList.Add(Self);

            var PointerType = Container.GlobalContainer.CommonIds.VoidPtr;
            var Pointer     = new MemberVariable(Scope, new CodeString("Pointer"), PointerType);

            Pointer.Access = IdentifierAccess.Public;
            Scope.IdentifierList.Add(Pointer);

            if (DoUpdate)
            {
                Update();
            }
        }
Example #2
0
        public PointerAndLength(IdContainer Container, Identifier Child, bool DoUpdate = true)
            : base(Container, new CodeString(), true)
        {
            this.UndeclaredIdType = UndeclaredIdType.PointerAndLength;
            var Scope = new StructuredScope(Container, new CodeString(), this);

            StructuredScope = Scope;

            var PointerType = new PointerType(Container, Child, DoUpdate);
            var Pointer     = new MemberVariable(Scope, new CodeString("Pointer"), PointerType);

            Pointer.Access = IdentifierAccess.Public;
            Scope.IdentifierList.Add(Pointer);

            var LengthType = Container.GlobalContainer.CommonIds.UIntPtr;
            var Length     = new MemberVariable(Scope, new CodeString("Length"), LengthType);

            Length.Access = IdentifierAccess.Public;
            Scope.IdentifierList.Add(Length);

            if (DoUpdate)
            {
                Update();
            }
        }
Example #3
0
        public Identifier GetRetType(Identifier Type0, Identifier Type1)
        {
            var RType0 = Type0.RealId;
            var RType1 = Type1.RealId;

            if (RType0 is AutomaticType || RType0 is CharType)
            {
                return(Type1);
            }
            if (RType1 is AutomaticType || RType1 is CharType)
            {
                return(Type0);
            }
            if (RType0 is StringType || RType0 is PointerType)
            {
                return(Type0);
            }
            if (RType1 is StringType || RType1 is PointerType)
            {
                return(Type1);
            }

            if (RType0 is NumberType && RType1 is NumberType)
            {
                return(GetNumberRetType(RType0 as NumberType, RType1 as NumberType));
            }

            var Tuple0 = RType0 as TupleType;
            var Tuple1 = RType1 as TupleType;

            if (Tuple0 != null && Tuple1 != null)
            {
                var Members0 = Tuple0.StructuredScope.IdentifierList;
                var Members1 = Tuple1.StructuredScope.IdentifierList;
                if (Members0.Count == Members1.Count)
                {
                    var Members = new List <Identifier>();
                    for (var i = 0; i < Members0.Count; i++)
                    {
                        var Var0 = Members0[i] as MemberVariable;
                        var Var1 = Members1[i] as MemberVariable;

                        var T = GetRetType(Var0.TypeOfSelf, Var1.TypeOfSelf);
                        if (T == null)
                        {
                            return(null);
                        }

                        var MemVar = new MemberVariable(this, new CodeString(), T);
                        MemVar.Access = T.Access;
                        Members.Add(MemVar);
                    }

                    return(new TupleType(this, Members));
                }
            }

            return(Type0);
        }
Example #4
0
        public override Variable OnCreateVariable(CodeString Name, Identifier Type, List <Modifier> Mods = null)
        {
            var Ret = CreateVariableHelper(Name, Type, Mods);

            if (Ret == null)
            {
                Ret = new MemberVariable(this, Name, Type);
            }
            return(Ret);
        }
Example #5
0
        public bool AutoImplement()
        {
            if (Property.Children.Length != 1)
            {
                throw new ApplicationException();
            }

            var Type = Property.Children[0];
            var Name = new CodeString(Property.Name.ToString() + "_%Value");

            Identifier Id;

            if ((Property.Flags & IdentifierFlags.Static) != 0)
            {
                Id        = new GlobalVariable(StructuredScope, Name, Type);
                Id.Flags |= IdentifierFlags.Static;
            }
            else
            {
                Id = new MemberVariable(StructuredScope, Name, Type);
            }

            if (!StructuredScope.DeclareIdentifier(Id))
            {
                return(false);
            }

            if (!AutoImplementGetter(Id))
            {
                return(false);
            }
            if (!AutoImplementSetter(Id))
            {
                return(false);
            }
            return(true);
        }
Example #6
0
        public TupleType ToTupleType(IdContainer Container, bool EnableMessages = true)
        {
            var State   = Container.State;
            var Members = new List <Identifier>();

            for (var i = 0; i < Count; i++)
            {
                var Decl = this[i];
                if (Decl.InitString.IsValid)
                {
                    if (EnableMessages)
                    {
                        State.Messages.Add(MessageId.CannotHaveInitVal, Decl.InitString);
                    }

                    return(null);
                }

                var RType = Decl.Type.RealId as Type;
                if ((RType.TypeFlags & TypeFlags.CanBeVariable) == 0)
                {
                    if (EnableMessages)
                    {
                        State.Messages.Add(MessageId.CannotBeThisType, Decl.TypeName);
                    }

                    return(null);
                }

                var Var = new MemberVariable(Container, Decl.Name, Decl.Type);
                Var.Access = Decl.Type.Access;
                Members.Add(Var);
            }

            return(new TupleType(Container, Members));
        }
Example #7
0
        Identifier ReadIdentifer(IdContainer Container, Identifier Parent)
        {
            var IdScope = Container.GetParent <IdentifierScope>();

            if (IdScope == null || IdScope.Identifier != Parent)
            {
                throw new ApplicationException();
            }

            var IdPos = Reader.BaseStream.Position - BeginningPos;

            if (IdPos != ReadLEB128_Long())
            {
                throw new InvalidAssemblyException();
            }

#warning CHECK
            var ParentIdRef = ReadLEB128_Int();

            var Byte           = Reader.ReadByte();
            var DeclaredIdType = (DeclaredIdType)(Byte & 15);
            var Access         = (IdentifierAccess)(Byte >> 4);

            var FlagData       = Reader.ReadUInt16();
            var Flags          = (IdentifierFlags)FlagData & IdentifierFlags.All;
            var HasName        = (FlagData & 16384) != 0;
            var HasOverloads   = (FlagData & 32768) != 0;
            var HasSpecialName = (Flags & IdentifierFlags.SpecialName) != 0;

            var Name         = HasName ? new CodeString(ReadLEB128_String()) : new CodeString();
            var AssemblyName = HasSpecialName ? ReadLEB128_String() : null;

            var StructuredScope = Container as StructuredScope;
            var IsGlobal        = (Flags & IdentifierFlags.Static) != 0 || !(Container.RealContainer is StructuredScope);

            var Ret = (Identifier)null;
            if (DeclaredIdType == DeclaredIdType.Alias)
            {
                Ret = new IdentifierAlias(Container, Name, null);
                ReadReference(Ret, ReferenceDestination.RealId, -1);
                UpdateList.Add(Ret);
            }
            else if (DeclaredIdType == DeclaredIdType.Class || DeclaredIdType == DeclaredIdType.Struct)
            {
                StructuredType Structured;
                if (DeclaredIdType == DeclaredIdType.Class)
                {
                    Ret = Structured = new ClassType(Container, Name);
                }
                else
                {
                    Ret = Structured = new StructType(Container, Name);
                }

                Structured.InstanceSize = ReadLEB128_Int();
                if (Structured.InstanceSize <= 0)
                {
                    throw new InvalidAssemblyException("Invalid size");
                }

                Structured.Align            = ReadLEB128_Int();
                Structured.LayoutCalculated = true;
                if (!DataStoring.VerifyAlign(Structured.Align))
                {
                    throw new InvalidAssemblyException("Invalid alingment");
                }

                if (DeclaredIdType == DeclaredIdType.Struct)
                {
                    Structured.Size = Structured.InstanceSize;
                }

                Structured.Guid = ReadGuid();
                var BaseCount = ReadLEB128_Int();
                Structured.BaseStructures = new StructureBase[BaseCount];
                for (var i = 0; i < BaseCount; i++)
                {
                    ReadReferenceDeclared(Structured, ReferenceDestination.Base, i);
                }

                Structured.FunctionTableIndex = ReadLEB128_Int();
                var Scope = new StructuredScope(Container, new CodeString(), Structured);
                Container.Children.Add(Scope);
                Structured.StructuredScope = Scope;
                ReadIdList(Scope, Structured);
                UpdateList.Add(Ret);
            }
            else if (DeclaredIdType == DeclaredIdType.Enum || DeclaredIdType == DeclaredIdType.Flag)
            {
                EnumType Enum;
                if (DeclaredIdType == Zinnia.DeclaredIdType.Enum)
                {
                    Ret = Enum = new EnumType(Container, Name, new CodeString());
                }
                else
                {
                    Ret = Enum = new FlagType(Container, Name, new CodeString());
                }
                ReadReference(Enum, ReferenceDestination.Children, 0);

                var Scope = new EnumScope(Container, new CodeString(), Enum);
                Container.Children.Add(Scope);
                Enum.EnumScope = Scope;

                var MemberCount = ReadLEB128_Int();
                if (MemberCount != 0)
                {
                    if (MemberCount < 0)
                    {
                        throw new InvalidAssemblyException();
                    }

                    for (var i = 0; i < MemberCount; i++)
                    {
                        var ConstName = new CodeString(ReadLEB128_String());
                        if (!ConstName.IsValidIdentifierName)
                        {
                            throw new InvalidAssemblyException("Invalid identifier name");
                        }

                        if (Identifiers.IsDefined(Scope.IdentifierList, ConstName.ToString()))
                        {
                            throw new InvalidAssemblyException("Identifier already defined");
                        }

                        var Const = new ConstVariable(Container, ConstName, Enum, ReadConst());
                        Scope.IdentifierList.Add(Const);
                    }
                }

                UpdateList.Add(Enum);
            }
            else if (DeclaredIdType == DeclaredIdType.Function || DeclaredIdType == DeclaredIdType.Constructor)
            {
                Function          Function;
                FunctionOverloads Overloads;
                if (DeclaredIdType == Zinnia.DeclaredIdType.Function)
                {
                    Overloads = Container.GetOverload(Name.ToString());
                    if (IsGlobal)
                    {
                        Ret = Function = new Function(Container, Name, null, Overloads);
                    }
                    else
                    {
                        Ret = Function = new MemberFunction(Container, Name, null, Overloads);
                    }
                }
                else
                {
                    if (HasName)
                    {
                        throw new InvalidAssemblyException("Constructors cannot have name");
                    }
                    if (StructuredScope == null)
                    {
                        throw new InvalidAssemblyException("Invalid container");
                    }

                    if (StructuredScope.ConstructorOverloads == null)
                    {
                        StructuredScope.ConstructorOverloads = new FunctionOverloads(null);
                    }

                    Overloads = StructuredScope.ConstructorOverloads;
                    Ret       = Function = new Constructor(Container, null, Overloads, new CodeString());
                }

                ReadReference(Function, ReferenceDestination.Children, 0);

                var OverloadIndex = HasOverloads ? ReadLEB128_Int() : 0;
                for (var i = 0; i < Overloads.Functions.Count; i++)
                {
                    var OverloadFunc = Overloads.Functions[i];
                    if (OverloadFunc.OverloadIndex == OverloadIndex)
                    {
                        throw new InvalidAssemblyException("Function with the same overload index");
                    }

                    /*
                     * if (Function.AreParametersSame(OverloadFunc))
                     *  throw new InvalidAssemblyException("Function with the same name and parameters");*/
                }

                Function.OverloadIndex = OverloadIndex;
                Overloads.Functions.Add(Function);

                if ((Flags & IdentifierFlags.Virtual) != 0)
                {
                    var MemberFunc = Function as MemberFunction;
                    MemberFunc.VirtualIndex = ReadLEB128_Int();
                    if ((Flags & IdentifierFlags.Override) != 0)
                    {
                        ReadReferenceDeclared(MemberFunc, ReferenceDestination.OverriddenId, -1);
                    }
                }

                Function.GlobalPointerIndex = ReadLEB128_Int();
            }
            else if (DeclaredIdType == DeclaredIdType.Destructor)
            {
                if (HasName)
                {
                    throw new InvalidAssemblyException("Destructors cannot have name");
                }
                if (StructuredScope == null)
                {
                    throw new InvalidAssemblyException("Invalid container");
                }

                var Function = new Destructor(Container, null, new CodeString());
                ReadReference(Function, ReferenceDestination.Children, 0);
                Function.GlobalPointerIndex = ReadLEB128_Int();
                Ret = Function;
            }
            else if (DeclaredIdType == DeclaredIdType.Variable)
            {
                if (IsGlobal)
                {
                    Ret = new GlobalVariable(Container, Name, null);
                }
                else
                {
                    Ret = new MemberVariable(Container, Name, null);
                }

                ReadReference(Ret, ReferenceDestination.Children, 0);

                if (!IsGlobal)
                {
                    var MemVar = Ret as MemberVariable;
                    MemVar.Offset = ReadLEB128_Int();
                }
                else
                {
                    var Global = Ret as GlobalVariable;
                    Global.GlobalPointerIndex = ReadLEB128_Int();
                }
            }
            else if (DeclaredIdType == DeclaredIdType.Constant)
            {
                var Const = new ConstVariable(Container, Name, null, null);
                ReadReference(Const, ReferenceDestination.Children, 0);
                Const.ConstInitValue = ReadConst();
                Ret = Const;
            }
            else if (DeclaredIdType == DeclaredIdType.Property)
            {
                var Property = new Property(Container, Name, (Type)null);
                var PScope   = new PropertyScope(Container, new CodeString(), Property);
                Container.Children.Add(PScope);
                Property.PropertyScope = PScope;
                ReadReference(Property, ReferenceDestination.Children, 0);
                ReadParameterReferences(Property);

                var Data = Reader.ReadByte();
                if ((Data & 1) != 0)
                {
                    PScope.Getter = ReadIdentifer(PScope, Property) as Function;
                }
                if ((Data & 2) != 0)
                {
                    PScope.Setter = ReadIdentifer(PScope, Property) as Function;
                }
                Ret = Property;
            }
            else if (DeclaredIdType == DeclaredIdType.Namespace)
            {
                var Scope = Container as NamespaceScope;
                if (Scope == null)
                {
                    throw new InvalidAssemblyException("Invalid container");
                }
                if (Access != IdentifierAccess.Public)
                {
                    throw new InvalidAssemblyException("Invalid access");
                }

                var Options = new GetIdOptions()
                {
                    Func = x => x is Namespace
                };
                var Namespace = Identifiers.GetMember(State, Parent, Name, Options) as Namespace;
                if (Namespace == null)
                {
                    Ret = Namespace = new Namespace(Container, Name);
                }

                var NewScope = new NamespaceScope(Container, new CodeString(), Namespace);
                Container.Children.Add(NewScope);
                Namespace.AddScope(NewScope);

                ReadIdList(NewScope, Namespace);
            }
            else
            {
                throw new InvalidAssemblyException("Invalid identifier type");
            }

            if (Ret != null)
            {
                if (HasSpecialName)
                {
                    Ret.AssemblyName = AssemblyName;
                }

                Ret.Access       = Access;
                Ret.Flags        = Flags;
                Ret.DescPosition = IdPos;
                CurrentAssembly.Ids.Add(IdPos, Ret);
            }

            return(Ret);
        }
Example #8
0
        void ReadReference(LoaderReference Ref)
        {
            var NonDeclared = (UndeclaredIdType)Reader.ReadByte();

            if (NonDeclared == UndeclaredIdType.Unknown)
            {
                ReadReferenceDeclared(Ref);
                return;
            }

            Identifier NewId;
            var        Container = Ref.DstId.Container;

            if (NonDeclared == UndeclaredIdType.RefArrayType)
            {
                var Arr = new RefArrayType(Container, null, 0, false);
                ReadReference(Arr, ReferenceDestination.Children, 0);
                UpdateList.Add(Arr);

                Arr.Dimensions = ReadLEB128_Int();
                NewId          = Arr;
            }
            else if (NonDeclared == UndeclaredIdType.NonrefArrayType)
            {
                var Arr = new NonrefArrayType(Container, null, null, false);
                ReadReference(Arr, ReferenceDestination.Children, 0);
                UpdateList.Add(Arr);

                var Dimensions = ReadLEB128_Int();
                if (Dimensions != 0)
                {
                    var Lengths = new int[Dimensions];
                    for (var i = 0; i < Dimensions; i++)
                    {
                        Lengths[i] = ReadLEB128_Int();
                    }

                    Arr.Lengths    = Lengths;
                    Arr.Dimensions = Dimensions;
                }

                NewId = Arr;
            }
            else if (NonDeclared == UndeclaredIdType.Pointer)
            {
                NewId = new PointerType(Container, null, false);
                ReadReference(NewId, ReferenceDestination.Children, 0);
                UpdateList.Add(NewId);
            }
            else if (NonDeclared == UndeclaredIdType.Reference)
            {
                var Mode = (ReferenceMode)Reader.ReadByte();
                NewId = new ReferenceType(Container, null, Mode, false);
                ReadReference(NewId, ReferenceDestination.Children, 0);
                UpdateList.Add(NewId);
            }
            else if (NonDeclared == UndeclaredIdType.Tuple)
            {
                var Tuple = new TupleType(Container, (List <Identifier>)null);
                Tuple.InstanceSize = ReadLEB128_Int();
                if (Tuple.InstanceSize <= 0)
                {
                    throw new InvalidAssemblyException("Invalid size");
                }

                Tuple.Size             = Tuple.InstanceSize;
                Tuple.Align            = ReadLEB128_Int();
                Tuple.LayoutCalculated = true;
                if (!DataStoring.VerifyAlign(Tuple.Align))
                {
                    throw new InvalidAssemblyException("Invalid alignment");
                }

                var Named       = Reader.ReadBoolean();
                var MemberCount = ReadLEB128_Int();
                for (var i = 0; i < MemberCount; i++)
                {
                    var Name      = Named ? new CodeString(ReadLEB128_String()) : new CodeString();
                    var MemberVar = new MemberVariable(Tuple.StructuredScope, Name, null);
                    MemberVar.Access = IdentifierAccess.Public;
                    ReadReference(MemberVar, ReferenceDestination.Children, 0);
                    MemberVar.Offset = ReadLEB128_Int();
                    Tuple.StructuredScope.IdentifierList.Add(MemberVar);
                }

                UpdateList.Add(Tuple);
                NewId = Tuple;
            }
            else if (NonDeclared == UndeclaredIdType.PointerAndLength)
            {
                var PAndL = new PointerAndLength(Container, null, false);
                ReadReference(PAndL.PointerType, ReferenceDestination.Children, 0);
                UpdateList.Add(PAndL);
                NewId = PAndL;
            }
            else if (NonDeclared == UndeclaredIdType.NonstaticFunctionType)
            {
                var FType = new NonstaticFunctionType(Container, null, false);
                ReadReference(FType, ReferenceDestination.Children, 0);
                UpdateList.Add(FType);
                NewId = FType;
            }
            else if (NonDeclared == UndeclaredIdType.Function)
            {
                var Conv = (CallingConvention)Reader.ReadByte();
                NewId = new TypeOfFunction(Container, Conv, new Identifier[1], false);
                ReadReference(NewId, ReferenceDestination.Children, 0);

                ReadParameterReferences(NewId);
                UpdateList.Add(NewId);
            }
            else
            {
                NewId = Global.CommonIds.GetIdentifier(NonDeclared);
                if (NewId == null)
                {
                    throw new InvalidAssemblyException();
                }
            }

            SetReferencedId(Ref, NewId);
        }