Beispiel #1
0
        public override ClassLayoutMetadata GetClassLayout()
        {
            ClassLayoutMetadata layout = ManagedStructType.GetClassLayout();

            ClassLayoutMetadata result;

            result.PackingSize = layout.PackingSize;
            result.Size        = layout.Size;

            if (IsExplicitLayout)
            {
                result.Offsets = new FieldAndOffset[layout.Offsets.Length];

                Debug.Assert(layout.Offsets.Length <= _fields.Length);

                int layoutIndex = 0;
                for (int index = 0; index < _fields.Length; index++)
                {
                    if (_fields[index].Name == layout.Offsets[layoutIndex].Field.Name)
                    {
                        result.Offsets[layoutIndex] = new FieldAndOffset(_fields[index], layout.Offsets[layoutIndex].Offset);
                        layoutIndex++;
                    }
                }

                Debug.Assert(layoutIndex == layout.Offsets.Length);
            }
            else
            {
                result.Offsets = null;
            }

            return(result);
        }
Beispiel #2
0
        public override ClassLayoutMetadata GetClassLayout()
        {
            ClassLayoutMetadata result = new ClassLayoutMetadata();

            result.PackingSize = 0;
            result.Size        = checked ((int)Length * ElementType.GetElementSize().AsInt);
            return(result);
        }
Beispiel #3
0
        private void OutputTypeFields(CppGenerationBuffer sb, TypeDesc t)
        {
            bool explicitLayout = false;
            ClassLayoutMetadata classLayoutMetadata = default(ClassLayoutMetadata);

            if (t.IsValueType)
            {
                MetadataType metadataType = (MetadataType)t;
                if (metadataType.IsExplicitLayout)
                {
                    explicitLayout      = true;
                    classLayoutMetadata = metadataType.GetClassLayout();
                }
            }

            int instanceFieldIndex = 0;

            if (explicitLayout)
            {
                sb.AppendLine();
                sb.Append("union {");
                sb.Indent();
            }

            foreach (var field in t.GetFields())
            {
                if (field.IsStatic)
                {
                    if (field.IsLiteral)
                    {
                        continue;
                    }

                    TypeDesc            fieldType = GetFieldTypeOrPlaceholder(field);
                    CppGenerationBuffer builder;
                    if (!fieldType.IsValueType)
                    {
                        builder = _gcStatics;
                    }
                    else
                    {
                        // TODO: Valuetype statics with GC references
                        builder = _statics;
                    }
                    builder.AppendLine();
                    builder.Append(GetCppSignatureTypeName(fieldType));
                    builder.Append(" ");
                    builder.Append(GetCppStaticFieldName(field) + ";");
                }
                else
                {
                    if (explicitLayout)
                    {
                        sb.AppendLine();
                        sb.Append("struct {");
                        sb.Indent();
                        int offset = classLayoutMetadata.Offsets[instanceFieldIndex].Offset;
                        if (offset > 0)
                        {
                            sb.AppendLine();
                            sb.Append("char __pad" + instanceFieldIndex + "[" + offset + "];");
                        }
                    }
                    sb.AppendLine();
                    sb.Append(GetCppSignatureTypeName(GetFieldTypeOrPlaceholder(field)) + " " + GetCppFieldName(field) + ";");
                    if (explicitLayout)
                    {
                        sb.Exdent();
                        sb.AppendLine();
                        sb.Append("};");
                    }
                    instanceFieldIndex++;
                }
            }

            if (explicitLayout)
            {
                sb.Exdent();
                sb.AppendLine();
                sb.Append("};");
            }
        }