예제 #1
0
            /// <summary>
            /// Seals this builder and returns a type that corresponds to the type
            /// represented by this builder.
            /// </summary>
            /// <returns>The create type node.</returns>
            public TypeNode Seal()
            {
                // Check for a special case in which we require custom padding elements
                // NB: Prevent empty structures by ensuring we have at least one field
                // that is not padding.
                if (Count == 0 && AlignedSize < Size)
                {
                    Add(TypeContext.Padding8Type.PrimitiveType);
                }

                // If the structure has a single field, apply padding using the size
                // of the initial field. This allows us to fill a fixed buffer with
                // the correct/aligned type.
                if (Count == 1 && AlignedSize < Size)
                {
                    var basicValueType = allFieldsBuilder[0].BasicValueType;
                    var paddingType    = TypeContext.GetPaddingType(basicValueType);
                    for (int i = AlignedSize, e = Size; i < e; i += paddingType.Size)
                    {
                        Add(paddingType);
                    }
                }

                // Ensure that the structure is populated to its required size before
                // we finalize. CAUTION: we have to ignore cases in which this type
                // contains types that will be lowered in future stages.
                if ((typeFlags & TypeFlags.ViewDependent) == TypeFlags.None)
                {
                    for (int i = AlignedSize, e = Size; i < e; ++i)
                    {
                        Add(TypeContext.Padding8Type);
                    }
                }
                return(TypeContext.FinishStructureType(this));
            }