Ejemplo n.º 1
0
 public void Visit(StructDecl structDecl)
 {
     var type = structDecl.Type;
     Indent();
     _sb.Append("struct ");
     _sb.Append(type.Name);
     _sb.Append(" {");
     int i = 0;
     for (; i < type.Fields.Count - 1; ++i) {
         _sb.Append(type.Fields[i].Type);
         _sb.Append(" ");
         _sb.Append(type.Fields[i].Name);
         _sb.Append("; ");
     }
     _sb.Append(type.Fields[i].Type);
     _sb.Append(" ");
     _sb.Append(type.Fields[i].Name);
     _sb.Append(";}\r\n");
 }
Ejemplo n.º 2
0
        StructType AddStructType(StructDecl decl)
        {
            var name = decl.Name.ToLower();
            Raise<TypeCheckException>.If(_types.ContainsKey(name));

            const string nmsp = "DanglingLang.Runner";
            const TypeAttributes typeAttr =
                TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout;
            var typeDef = new TypeDefinition(nmsp, name, typeAttr, Module.Import(typeof(object)));

            var type = new StructType(name, typeDef);
            foreach (var f in decl.Fields) {
                var fieldType = GetType(f.Item2);
                Raise<TypeCheckException>.IfAreEqual("void", fieldType.Name, "Field cannot be void");
                type.AddField(f.Item1, fieldType);
            }

            _types.Add(name, type);
            _structTypes.Add(name, type);
            return type;
        }
Ejemplo n.º 3
0
 public void Visit(StructDecl structDecl)
 {
     // Nothing to do here...
 }
Ejemplo n.º 4
0
 public void Visit(StructDecl structDecl)
 {
     structDecl.Type = AddStructType(structDecl);
 }
Ejemplo n.º 5
0
        public void Init(BinaryReader reader, bool swap)
        {
            Stream stream = reader.BaseStream;

            // SDNA
            byte[] code = reader.ReadBytes(8);
            string codes = ASCIIEncoding.ASCII.GetString(code);

            // NAME
            if (!codes.Equals("SDNANAME"))
            {
                throw new InvalidDataException();
            }
            int dataLen = reader.ReadInt32();
            _names = new NameInfo[dataLen];
            for (int i = 0; i < dataLen; i++)
            {
                List<byte> name = new List<byte>();
                byte ch = reader.ReadByte();
                while (ch != 0)
                {
                    name.Add(ch);
                    ch = reader.ReadByte();
                }

                _names[i] = new NameInfo(ASCIIEncoding.ASCII.GetString(name.ToArray()));
            }
            stream.Position = (stream.Position + 3) & ~3;

            // TYPE
            code = reader.ReadBytes(4);
            codes = ASCIIEncoding.ASCII.GetString(code);
            if (!codes.Equals("TYPE"))
            {
                throw new InvalidDataException();
            }
            dataLen = reader.ReadInt32();
            _types = new TypeDecl[dataLen];
            for (int i = 0; i < dataLen; i++)
            {
                List<byte> name = new List<byte>();
                byte ch = reader.ReadByte();
                while (ch != 0)
                {
                    name.Add(ch);
                    ch = reader.ReadByte();
                }
                string type = ASCIIEncoding.ASCII.GetString(name.ToArray());
                _types[i] = new TypeDecl(type);
            }
            stream.Position = (stream.Position + 3) & ~3;

            // TLEN
            code = reader.ReadBytes(4);
            codes = ASCIIEncoding.ASCII.GetString(code);
            if (!codes.Equals("TLEN"))
            {
                throw new InvalidDataException();
            }
            for (int i = 0; i < _types.Length; i++)
            {
                _types[i].Length = reader.ReadInt16();
            }
            stream.Position = (stream.Position + 3) & ~3;

            // STRC
            code = reader.ReadBytes(4);
            codes = ASCIIEncoding.ASCII.GetString(code);
            if (!codes.Equals("STRC"))
            {
                throw new InvalidDataException();
            }
            dataLen = reader.ReadInt32();
            _structs = new StructDecl[dataLen];
            long shtPtr = stream.Position;
            for (int i = 0; i < dataLen; i++)
            {
                StructDecl structDecl = new StructDecl();
                _structs[i] = structDecl;
                if (swap)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    short typeNr = reader.ReadInt16();
                    structDecl.Type = _types[typeNr];
                    structDecl.Type.Struct = structDecl;
                    int numElements = reader.ReadInt16();
                    structDecl.Elements = new ElementDecl[numElements];
                    for (int j = 0; j < numElements; j++)
                    {
                        typeNr = reader.ReadInt16();
                        short nameNr = reader.ReadInt16();
                        structDecl.Elements[j] = new ElementDecl(_types[typeNr], _names[nameNr]);
                    }
                }
            }

            // build reverse lookups
            _structReverse = new Dictionary<string, int>(_structs.Length);
            for (int i = 0; i < _structs.Length; i++)
            {
                StructDecl s = _structs[i];
                if (_ptrLen == 0 && s.Type.Name.Equals("ListBase"))
                {
                    _ptrLen = s.Type.Length / 2;
                }
                _structReverse.Add(s.Type.Name, i);
            }
        }
Ejemplo n.º 6
0
        public void Visit(StructDecl structDecl)
        {
            // Each field is added to the struct type...
            const FieldAttributes fieldAttr = FieldAttributes.Public;
            var typeDef = structDecl.Type.Reference as TypeDefinition;
            Debug.Assert(typeDef != null);
            foreach (var f in structDecl.Type.Fields) {
                var fieldDef = new FieldDefinition(f.Name, fieldAttr, f.Type.Reference);
                typeDef.Fields.Add(fieldDef);
                f.Reference = fieldDef;
            }

            // We add a proper constructor to the new type. We first create
            // a parameter for each field in the struct.
            var structFields = structDecl.Type.Fields;
            var parameters = new ParameterDefinition[structFields.Count];
            for (var i = 0; i < structFields.Count; ++i) {
                var pName = "_" + structFields[i].Name;
                const ParameterAttributes pAttr = ParameterAttributes.None;
                var pType = structFields[i].Type.Reference;
                parameters[i] = new ParameterDefinition(pName, pAttr, pType);
            }
            // Then, we create the constructor itself...
            const MethodAttributes ctorAttr =
                MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                MethodAttributes.RTSpecialName;
            var ctor = new MethodDefinition(".ctor", ctorAttr, _module.TypeSystem.Void);
            // And we add the parameters we created before.
            foreach (var p in parameters) {
                ctor.Parameters.Add(p);
            }
            // Then, we build a constructor so that each field receives
            // its new value from the corresponding parameter.
            ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This
            ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, _objCtor));
            for (var i = 0; i < parameters.Length; ++i) {
                ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This
                ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, parameters[i]));
                ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, structFields[i].Reference));
            }
            ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            // Constructor is added to the new type.
            typeDef.Methods.Add(ctor);
            structDecl.Type.Ctor = ctor;

            // After that, we have to follow a similar procedure
            // to declare a proper "Equals" method for the new type.
            // As before, we first create the parameter.
            var eqParam = new ParameterDefinition("other", ParameterAttributes.None, typeDef);
            // Then, we create the method itself...
            const MethodAttributes eqAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
            var equals = new MethodDefinition("MyEquals", eqAttr, _module.TypeSystem.Boolean);
            // And we add the parameters we created before.
            equals.Parameters.Add(eqParam);
            // Then, we build a method so that each field is compared
            // against value from the corresponding other field.
            var nop = Instruction.Create(OpCodes.Nop);
            var ret = Instruction.Create(OpCodes.Ret);
            foreach (var f in structFields) {
                equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This
                equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, f.Reference));
                equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); // Other
                equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, f.Reference));
                if (f.Type is StructType) {
                    var fieldEq = (f.Type as StructType).TypeEquals;
                    equals.Body.Instructions.Add(Instruction.Create(OpCodes.Call, fieldEq));
                } else {
                    equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ceq));
                }
                equals.Body.Instructions.Add(Instruction.Create(OpCodes.Brfalse, nop));
            }
            equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_1));
            equals.Body.Instructions.Add(Instruction.Create(OpCodes.Br, ret));
            equals.Body.Instructions.Add(nop);
            equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0));
            equals.Body.Instructions.Add(ret);
            // Equals is added to the new type.
            typeDef.Methods.Add(equals);
            structDecl.Type.TypeEquals = equals;

            // New type is then added to the assembly.
            _module.Types.Add(typeDef);
        }
Ejemplo n.º 7
0
        private void Init(BulletReader reader, bool swap)
        {
            if (swap)
            {
                throw new NotImplementedException();
            }

            Stream stream = reader.BaseStream;
            reader.ReadTag("SDNA");

            // Element names
            reader.ReadTag("NAME");
            string[] names = reader.ReadStringList();
            var nameInfos = names
                .Select(n => new NameInfo(n))
                .ToArray();
            _hasIntType = names.Contains("int");

            // Types
            reader.ReadTag("TYPE");
            string[] typeNames = reader.ReadStringList();
            stream.Position = (stream.Position + 3) & ~3;

            reader.ReadTag("TLEN");
            TypeDecl[] types = typeNames.Select(name =>
            {
                short length = reader.ReadInt16();
                if (_ptrLen == 0 && name == "ListBase")
                {
                    _ptrLen = length / 2;
                }
                return new TypeDecl(name, length);
            }).ToArray();
            stream.Position = (stream.Position + 3) & ~3;

            // Structs
            reader.ReadTag("STRC");
            int numStructs = reader.ReadInt32();
            _structs = new StructDecl[numStructs];
            _structByTypeName = new Dictionary<string, StructDecl>(numStructs);
            for (int i = 0; i < numStructs; i++)
            {
                short typeIndex = reader.ReadInt16();
                TypeDecl structType = types[typeIndex];

                int numElements = reader.ReadInt16();
                var elements = new ElementDecl[numElements];
                for (int j = 0; j < numElements; j++)
                {
                    typeIndex = reader.ReadInt16();
                    short nameIndex = reader.ReadInt16();
                    elements[j] = new ElementDecl(types[typeIndex], nameInfos[nameIndex]);
                }

                var structDecl = new StructDecl(structType, elements);
                structType.Struct = structDecl;
                _structs[i] = structDecl;
                _structByTypeName.Add(structType.Name, structDecl);
            }
        }
Ejemplo n.º 8
0
 // Structs containing non-equal structs are also non-equal
 private void CompareStruct(StructDecl iter, bool[] _structChanged)
 {
     for (int i = 0; i < _structs.Length; i++)
     {
         StructDecl curStruct = _structs[i];
         if (curStruct != iter && !_structChanged[i])
         {
             foreach (ElementDecl element in curStruct.Elements)
             {
                 if (curStruct.Type == iter.Type && element.NameInfo.IsPointer)
                 {
                     _structChanged[i] = true;
                     CompareStruct(curStruct, _structChanged);
                 }
             }
         }
     }
 }