Пример #1
0
        /// <summary>Initializes a new instance of the <see cref="DebugUnionType"/> class.</summary>
        /// <param name="llvmType">Underlying native type this debug type describes</param>
        /// <param name="module">Module to contain the debug metadata for this type</param>
        /// <param name="scope">Scope containing this type</param>
        /// <param name="name">Debug/source name of the type</param>
        /// <param name="file">Source file containing this type</param>
        /// <param name="line">Line number for this type</param>
        /// <param name="debugFlags">Debug flags for this type</param>
        /// <param name="elements">Descriptors for the members of the type</param>
        public DebugUnionType(IStructType llvmType
                              , BitcodeModule module
                              , DIScope scope
                              , string name
                              , DIFile file
                              , uint line
                              , DebugInfoFlags debugFlags
                              , IEnumerable <DebugMemberInfo> elements
                              )
            : base(llvmType)
        {
            llvmType.ValidateNotNull(nameof(llvmType));
            module.ValidateNotNull(nameof(module));
            scope.ValidateNotNull(nameof(scope));
            file.ValidateNotNull(nameof(file));

            if (!llvmType.IsOpaque)
            {
                throw new ArgumentException(Resources.Struct_type_used_as_basis_for_a_union_must_not_have_a_body, nameof(llvmType));
            }

            DIType = module.DIBuilder
                     .CreateReplaceableCompositeType(Tag.UnionType
                                                     , name
                                                     , scope
                                                     , file
                                                     , line
                                                     );
            SetBody(module, scope, file, line, debugFlags, elements);
        }
Пример #2
0
 /// <summary>
 /// Creates a new tuple value representing a Q# value of user defined type.
 /// The casts to get the opaque and typed pointer respectively are executed lazily. When needed,
 /// the instructions are emitted using the current builder.
 /// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
 /// </summary>
 /// <param name="elementTypes">The Q# types of the tuple items</param>
 /// <param name="context">Generation context where constants are defined and generated if needed</param>
 internal TupleValue(UserDefinedType?type, ImmutableArray <ResolvedType> elementTypes, GenerationContext context, bool registerWithScopeManager = true)
 {
     this.sharedState          = context;
     this.customType           = type;
     this.ElementTypes         = elementTypes;
     this.StructType           = this.sharedState.Types.TypedTuple(elementTypes.Select(context.LlvmTypeFromQsharpType));
     this.opaquePointer        = this.CreateOpaquePointerCache(this.AllocateTuple(registerWithScopeManager));
     this.typedPointer         = this.CreateTypedPointerCache();
     this.tupleElementPointers = this.CreateTupleElementPointersCaches();
 }
Пример #3
0
 public void ReferenceDeclaration(IStructType declaration)
 {
     if (declaration is IIntermediateStructType)
     {
         this.ReferenceDeclaration((IIntermediateStructType)declaration);
     }
     else
     {
         this.Formatter.ReferenceDeclaration(declaration);
     }
 }
Пример #4
0
        private static void CreateDoCopyFunctionBody(BitcodeModule module
                                                     , DataLayout layout
                                                     , Function doCopyFunc
                                                     , IStructType foo
                                                     , GlobalVariable bar
                                                     , GlobalVariable baz
                                                     , Function copyFunc
                                                     )
        {
            var bytePtrType = module.Context.Int8Type.CreatePointerType( );

            // create block for the function body, only need one for this simple sample
            var blk = doCopyFunc.AppendBasicBlock("entry");

            // create instruction builder to build the body
            var instBuilder = new InstructionBuilder(blk);

            bool param0ByVal = copyFunc.Attributes[FunctionAttributeIndex.Parameter0].Contains(AttributeKind.ByVal);

            if (!param0ByVal)
            {
                // create a temp local copy of the global structure
                var dstAddr = instBuilder.Alloca(foo)
                              .RegisterName("agg.tmp")
                              .Alignment(layout.CallFrameAlignmentOf(foo));

                var bitCastDst = instBuilder.BitCast(dstAddr, bytePtrType)
                                 .SetDebugLocation(25, 11, doCopyFunc.DISubProgram);

                var bitCastSrc = instBuilder.BitCast(bar, bytePtrType)
                                 .SetDebugLocation(25, 11, doCopyFunc.DISubProgram);

                instBuilder.MemCpy(module
                                   , bitCastDst
                                   , bitCastSrc
                                   , module.Context.CreateConstant(layout.ByteSizeOf(foo))
                                   , ( int )layout.CallFrameAlignmentOf(foo)
                                   , false
                                   ).SetDebugLocation(25, 11, doCopyFunc.DISubProgram);

                instBuilder.Call(copyFunc, dstAddr, baz)
                .SetDebugLocation(25, 5, doCopyFunc.DISubProgram);
            }
            else
            {
                instBuilder.Call(copyFunc, bar, baz)
                .SetDebugLocation(25, 5, doCopyFunc.DISubProgram)
                .AddAttributes(FunctionAttributeIndex.Parameter0, copyFunc.Parameters[0].Attributes);
            }

            instBuilder.Return( )
            .SetDebugLocation(26, 1, doCopyFunc.DISubProgram);
        }
Пример #5
0
        /// <summary>Creates a constant instance of a specified structure type from a set of values</summary>
        /// <param name="type">Type of the structure to create</param>
        /// <param name="values">Set of values to use in forming the structure</param>
        /// <returns>Newly created <see cref="Constant"/></returns>
        /// <remarks>
        /// <note type="note">The actual concrete return type depends on the parameters provided and will be one of the following:
        /// <list type="table">
        /// <listheader>
        /// <term><see cref="Constant"/> derived type</term><description>Description</description>
        /// </listheader>
        /// <item><term>ConstantAggregateZero</term><description>If all the member values are zero constants</description></item>
        /// <item><term>UndefValue</term><description>If all the member values are UndefValue</description></item>
        /// <item><term>ConstantStruct</term><description>All other cases</description></item>
        /// </list>
        /// </note>
        /// </remarks>
        public Constant CreateNamedConstantStruct(IStructType type, IEnumerable <Constant> values)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.Context != this)
            {
                throw new ArgumentException("Cannot create named constant struct with type from another context", nameof(type));
            }

            var valueList    = values as IList <Constant> ?? values.ToList( );
            var valueHandles = valueList.Select(v => v.ValueHandle).ToArray( );

            if (type.Members.Count != valueHandles.Length)
            {
                throw new ArgumentException("Number of values provided must match the number of elements in the specified type");
            }

            var mismatchedTypes = from indexedVal in valueList.Select((v, i) => new { Value = v, Index = i })
                                  where indexedVal.Value.NativeType != type.Members[indexedVal.Index]
                                  select indexedVal;

            if (mismatchedTypes.Any( ))
            {
                var msg = new StringBuilder("One or more values provided do not match the corresponding member type:");
                msg.AppendLine( );
                foreach (var mismatch in mismatchedTypes)
                {
                    msg.AppendFormat("\t[{0}]: member type={1}; value type={2}"
                                     , mismatch.Index
                                     , type.Members[mismatch.Index]
                                     , valueList[mismatch.Index].NativeType
                                     );
                }

                throw new ArgumentException(msg.ToString( ));
            }

            // To interop correctly, we need to have an array of at least size one.
            uint valuesLength = (uint)valueHandles.Length;

            if (valuesLength == 0)
            {
                valueHandles = new LLVMValueRef[1];
            }

            var handle = NativeMethods.ConstNamedStruct(type.GetTypeRef( ), out valueHandles[0], valuesLength);

            return(Value.FromHandle <Constant>(handle));
        }
Пример #6
0
        public DebugUnionType(IStructType llvmType
                              , NativeModule module
                              , DIScope scope
                              , string name
                              , DIFile file
                              , uint line
                              , DebugInfoFlags debugFlags
                              , IEnumerable <DebugMemberInfo> elements
                              )
            : base(llvmType)
        {
            if (llvmType == null)
            {
                throw new ArgumentNullException(nameof(llvmType));
            }

            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (!llvmType.IsOpaque)
            {
                throw new ArgumentException("Struct type used as basis for a union must not have a body", nameof(llvmType));
            }

            DIType = module.DIBuilder
                     .CreateReplaceableCompositeType(Tag.UnionType
                                                     , name
                                                     , scope
                                                     , file
                                                     , line
                                                     );
            SetBody(module, scope, file, line, debugFlags, elements);
        }
Пример #7
0
        /// <summary>
        /// Creates a new tuple value representing a Q# value of user defined type from the given tuple pointer.
        /// The casts to get the opaque and typed pointer respectively are executed lazily. When needed,
        /// instructions are emitted using the current builder.
        /// </summary>
        /// <param name="type">Optionally the user defined type tha that the tuple represents</param>
        /// <param name="tuple">Either an opaque or a typed pointer to the tuple data structure</param>
        /// <param name="elementTypes">The Q# types of the tuple items</param>
        /// <param name="context">Generation context where constants are defined and generated if needed</param>
        internal TupleValue(UserDefinedType?type, Value tuple, ImmutableArray <ResolvedType> elementTypes, GenerationContext context)
        {
            var isTypedTuple  = Types.IsTypedTuple(tuple.NativeType);
            var isOpaqueTuple = !tuple.IsNull && Types.IsTupleOrUnit(tuple.NativeType);

            if (!isTypedTuple && !isOpaqueTuple)
            {
                throw new ArgumentException("expecting either an opaque or a typed tuple");
            }

            this.sharedState          = context;
            this.customType           = type;
            this.ElementTypes         = elementTypes;
            this.StructType           = this.sharedState.Types.TypedTuple(elementTypes.Select(context.LlvmTypeFromQsharpType));
            this.opaquePointer        = this.CreateOpaquePointerCache(isOpaqueTuple ? tuple : null);
            this.typedPointer         = this.CreateTypedPointerCache(isTypedTuple ? tuple : null);
            this.tupleElementPointers = this.CreateTupleElementPointersCaches();
        }
Пример #8
0
        /// <summary>Creates a constant instance of a specified structure type from a set of values</summary>
        /// <param name="type">Type of the structure to create</param>
        /// <param name="values">Set of values to use in forming the structure</param>
        /// <returns>Newly created <see cref="Constant"/></returns>
        /// <remarks>
        /// <note type="note">The actual concrete return type depends on the parameters provided and will be one of the following:
        /// <list type="table">
        /// <listheader>
        /// <term><see cref="Constant"/> derived type</term><description>Description</description>
        /// </listheader>
        /// <item><term>ConstantAggregateZero</term><description>If all the member values are zero constants</description></item>
        /// <item><term>UndefValue</term><description>If all the member values are UndefValue</description></item>
        /// <item><term>ConstantStruct</term><description>All other cases</description></item>
        /// </list>
        /// </note>
        /// </remarks>
        public Constant CreateNamedConstantStruct(IStructType type, IEnumerable <Constant> values)
        {
            type.ValidateNotNull(nameof(type));
            values.ValidateNotNull(nameof(values));

            if (type.Context != this)
            {
                throw new ArgumentException(Resources.Cannot_create_named_constant_struct_with_type_from_another_context, nameof(type));
            }

            var valueList    = values as IList <Constant> ?? values.ToList( );
            var valueHandles = valueList.Select(v => v.ValueHandle).ToArray( );

            if (type.Members.Count != valueHandles.Length)
            {
                throw new ArgumentException(Resources.Number_of_values_provided_must_match_the_number_of_elements_in_the_specified_type);
            }

            var mismatchedTypes = from indexedVal in valueList.Select((v, i) => new { Value = v, Index = i })
                                  where indexedVal.Value.NativeType != type.Members[indexedVal.Index]
                                  select indexedVal;

            if (mismatchedTypes.Any( ))
            {
                var msg = new StringBuilder(Resources.One_or_more_values_provided_do_not_match_the_corresponding_member_type_);
                msg.AppendLine( );
                foreach (var mismatch in mismatchedTypes)
                {
                    msg.AppendFormat(CultureInfo.CurrentCulture
                                     , Resources.MismatchedType_0_member_type_equals_1_value_type_equals_2
                                     , mismatch.Index
                                     , type.Members[mismatch.Index]
                                     , valueList[mismatch.Index].NativeType
                                     );
                }

                throw new ArgumentException(msg.ToString( ));
            }

            var handle = LLVMConstNamedStruct(type.GetTypeRef( ), valueHandles, (uint)valueHandles.Length);

            return(Value.FromHandle <Constant>(handle));
        }
Пример #9
0
        // constructor

        internal Types(Context context)
        {
            this.context = context;

            this.Int    = context.Int64Type;
            this.Double = context.DoubleType;
            this.Bool   = context.BoolType;
            this.Pauli  = context.GetIntType(2);
            this.Range  = context.CreateStructType(TypeNames.Range, false, context.Int64Type, context.Int64Type, context.Int64Type);

            this.Result   = context.CreateStructType(TypeNames.Result).CreatePointerType();
            this.Qubit    = context.CreateStructType(TypeNames.Qubit).CreatePointerType();
            this.String   = context.CreateStructType(TypeNames.String).CreatePointerType();
            this.BigInt   = context.CreateStructType(TypeNames.BigInt).CreatePointerType();
            this.Tuple    = context.CreateStructType(TypeNames.Tuple).CreatePointerType();
            this.Array    = context.CreateStructType(TypeNames.Array).CreatePointerType();
            this.Callable = context.CreateStructType(TypeNames.Callable).CreatePointerType();

            this.FunctionSignature             = context.GetFunctionType(context.VoidType, this.Tuple, this.Tuple, this.Tuple);
            this.CallableTable                 = this.FunctionSignature.CreatePointerType().CreateArrayType(4);
            this.CaptureCountFunction          = context.GetFunctionType(context.VoidType, this.Tuple, context.Int32Type);
            this.CallableMemoryManagementTable = this.CaptureCountFunction.CreatePointerType().CreateArrayType(2);
        }
Пример #10
0
 public void ReferenceDeclaration(IStructType declaration)
 {
 }
Пример #11
0
 protected abstract IStructInstance OnCreateStruct(ISymbolInfo entry, IStructType structType, ISymbol parent);
Пример #12
0
 internal StructInstance(ISymbol parent, IStructType type, string instanceName, int fieldOffset) : base(parent, type, instanceName, fieldOffset, ((ISymbolFactoryServicesProvider)parent).FactoryServices)
 {
     base.category = DataTypeCategory.Struct;
 }
Пример #13
0
 internal StructInstance(AdsSymbolEntry entry, IStructType type, ISymbol parent, ISymbolFactoryServices factoryServices) : base(entry, type, parent, factoryServices)
 {
     base.category = DataTypeCategory.Struct;
 }
Пример #14
0
 public StructTypeWrapper(IStructType <TAttributeGroup, TGenericParameter, TInterfaceReference, TEventCollection, TPropertyCollection, TIndexerCollection, TMethodCollection, TFieldCollection, TConstructor, TOperatorOverload, TConversionOperator, TNestedClassCollection, TNestedDelegate, TNestedEnum, TNestedInterface, TNestedStructCollection, TStaticConstructor> type)
 {
     WrappedObject = type;
 }
Пример #15
0
 public override void TranslateType(IStructType structureType)
 {
     this.Provider.GenerateCodeFromType(structureType.GenerateCodeDom(this.Options), base.Target, this.Options.Options);
 }
Пример #16
0
 public ulong OffsetOfElement(IStructType structType, uint element)
 {
     VerifySized(structType, nameof(structType));
     return(LLVMOffsetOfElement(DataLayoutHandle, structType.GetTypeRef( ), element));
 }
Пример #17
0
 public void ReferenceDeclaration(IStructType declaration)
 {
     referenceStack.Push(declaration);
 }
Пример #18
0
 internal _Method(IStructType parent, IStructPropertyMethodMember original)
     : base(parent, original)
 {
 }
Пример #19
0
 /// <summary>Creates a constant instance of a specified structure type from a set of values</summary>
 /// <param name="type">Type of the structure to create</param>
 /// <param name="values">Set of values to use in forming the structure</param>
 /// <returns>Newly created <see cref="Constant"/></returns>
 /// <remarks>
 /// <note type="note">The actual concrete return type depends on the parameters provided and will be one of the following:
 /// <list type="table">
 /// <listheader>
 /// <term><see cref="Constant"/> derived type</term><description>Description</description>
 /// </listheader>
 /// <item><term>ConstantAggregateZero</term><description>If all the member values are zero constants</description></item>
 /// <item><term>UndefValue</term><description>If all the member values are UndefValue</description></item>
 /// <item><term>ConstantStruct</term><description>All other cases</description></item>
 /// </list>
 /// </note>
 /// </remarks>
 public Constant CreateNamedConstantStruct(IStructType type, params Constant[] values)
 {
     return(CreateNamedConstantStruct(type, (IEnumerable <Constant>)values));
 }
Пример #20
0
 public MethodMember(ICliMetadataMethodDefinitionTableRow metadataEntry, _ICliAssembly assembly, IStructType parent, IGeneralGenericSignatureMemberUniqueIdentifier uniqueIdentifier, PropertyMethodType methodType)
     : base(metadataEntry, assembly, parent, uniqueIdentifier)
 {
     this.methodType = methodType;
 }
Пример #21
0
 internal _MethodMember(IStructType parent, IStructMethodMember method)
     : base(parent, method)
 {
 }
Пример #22
0
 public uint ElementAtOffset(IStructType structType, ulong offset)
 {
     VerifySized(structType, nameof(structType));
     return(LLVMElementAtOffset(DataLayoutHandle, structType.GetTypeRef( ), offset));
 }
Пример #23
0
 /// <summary>
 /// Translates a structure-based declared type.
 /// </summary>
 /// <param name="structureType">The <see cref="IStructType"/> to translate.</param>
 public abstract void TranslateType(IStructType structureType);
Пример #24
0
 /// <summary>Gets the offset of a structure element in bits</summary>
 /// <param name="llvmType">Structure type to get the element offset of</param>
 /// <param name="element">Index of the element in the structure</param>
 /// <returns>Offset of the element in bits</returns>
 public ulong BitOffsetOfElement(IStructType llvmType, uint element) => OffsetOfElement(llvmType, element) * 8;
Пример #25
0
 protected override IStructInstance OnCreateStruct(ISymbolInfo entry, IStructType structType, ISymbol parent) =>
 (!structType.HasRpcMethods ? new StructInstance((AdsSymbolEntry)entry, (StructType)structType, parent, base.services) : new RpcStructInstance((AdsSymbolEntry)entry, (RpcStructType)structType, parent, base.services));