private static bool FunctionTypeConformToFunctionType(IFunctionType derivedType, IFunctionType baseType, IErrorList errorList, ISource sourceLocation) { bool Result = true; Debug.Assert(derivedType.ResolvedBaseType.IsAssigned); Debug.Assert(baseType.ResolvedBaseType.IsAssigned); if (!TypeConformToBase(derivedType.ResolvedBaseType.Item, baseType.ResolvedBaseType.Item, errorList, sourceLocation, isConversionAllowed: false)) { errorList.AddError(new ErrorBaseConformance(sourceLocation, derivedType, baseType)); Result = false; } foreach (IQueryOverloadType BaseOverload in baseType.OverloadList) { bool MatchingDerivedOverload = false; foreach (IQueryOverloadType DerivedOverload in derivedType.OverloadList) { MatchingDerivedOverload |= QueryOverloadConformToBase(DerivedOverload, BaseOverload, ErrorList.Ignored, ErrorList.NoLocation); } if (!MatchingDerivedOverload) { errorList.AddError(new ErrorOverloadMismatchConformance(sourceLocation, derivedType, baseType)); Result = false; } } return(Result); }
private static bool FunctionTypeConformToIndexerType(IFunctionType derivedType, IIndexerType baseType, IErrorList errorList, ISource sourceLocation) { bool Result = true; Debug.Assert(derivedType.ResolvedBaseType.IsAssigned); Debug.Assert(baseType.ResolvedBaseType.IsAssigned); Result = TypeConformToBase(derivedType.ResolvedBaseType.Item, baseType.ResolvedBaseType.Item, errorList, sourceLocation, isConversionAllowed: false); if (baseType.IndexerKind != BaseNode.UtilityType.ReadOnly) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } bool MatchingOverload = false; foreach (IQueryOverloadType DerivedOverload in derivedType.OverloadList) { MatchingOverload |= FunctionTypeConformToIndexerTypeOverloads(DerivedOverload, baseType, errorList, sourceLocation); } if (!MatchingOverload) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } return(Result); }
public Function AddFunction(string name, IFunctionType signature) { ValidateHandle( ); name.ValidateNotNullOrWhiteSpace(nameof(name)); signature.ValidateNotNull(nameof(signature)); return(Value.FromHandle <Function>(LLVMGetOrInsertFunction(ModuleHandle, name, signature.GetTypeRef( )))); }
private void AssertParameterCount(IFunctionType ft) { var paramTypes = ft.ParameterTypes; if (paramTypes.Count() != Exprs.Count()) { throw new TranspilationException($"Expected {paramTypes.Count()} parameter(s) when invoking function '{Name}', but got {Exprs.Count()}", Location); } }
/// <summary>Creates a function</summary> /// <param name="name">Name of the function</param> /// <param name="isVarArg">Flag indicating if the function supports a variadic argument list</param> /// <param name="returnType">Return type of the function</param> /// <param name="argumentTypes">Arguments for the function</param> /// <returns> /// Function, matching the signature specified. This may be a previously declared or defined /// function or a new function if none matching the name and signature is already present. /// </returns> public Function CreateFunction(string name , bool isVarArg , IDebugType <ITypeRef, DIType> returnType , IEnumerable <IDebugType <ITypeRef, DIType> > argumentTypes ) { ValidateHandle( ); IFunctionType signature = Context.CreateFunctionType(DIBuilder, isVarArg, returnType, argumentTypes); return(AddFunction(name, signature)); }
public IrFunction CreateFunction(string name, IFunctionType signature) { ThrowIfDisposed( ); LLVMValueRef valueRef = ModuleHandle.GetNamedFunction(name); if (valueRef == default) { valueRef = ModuleHandle.AddFunction(name, signature.GetTypeRef( )); } return(Value.FromHandle <IrFunction>(valueRef) !); }
private static bool ResolveCall(IPrecursorExpression node, IFeatureInstance selectedPrecursor, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList) { selectedParameterList = null; selectedResultList = null; resolvedArgumentList = null; IList <IArgument> ArgumentList = node.ArgumentList; ICompiledFeature OperatorFeature = selectedPrecursor.Feature; IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >(); bool IsHandled = false; bool Success = false; switch (OperatorFeature) { case IAttributeFeature AsAttributeFeature: case ICreationFeature AsCreationFeature: case IProcedureFeature AsProcedureFeature: case IIndexerFeature AsIndexerFeature: errorList.AddError(new ErrorInvalidExpression(node)); IsHandled = true; break; case IConstantFeature AsConstantFeature: Success = ResolveCallClass(node, selectedPrecursor, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList); IsHandled = true; break; case IFunctionFeature AsFunctionFeature: IFunctionType FunctionType = AsFunctionFeature.ResolvedAgentType.Item as IFunctionType; Debug.Assert(FunctionType != null); Success = ResolveCallFunction(node, selectedPrecursor, FunctionType, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList); IsHandled = true; break; case IPropertyFeature AsPropertyFeature: IPropertyType PropertyType = AsPropertyFeature.ResolvedAgentType.Item as IPropertyType; Debug.Assert(PropertyType != null); Success = ResolveCallProperty(node, selectedPrecursor, PropertyType, mergedArgumentList, argumentStyle, errorList, ref resolvedExpression, out selectedParameterList, out selectedResultList, out resolvedArgumentList); IsHandled = true; break; } Debug.Assert(IsHandled); return(Success); }
/// <summary> /// Initializes a new instance of the <see cref="CSharpFunctionType"/> class. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">The Easly type from which the C# type is created.</param> /// <param name="originatingTypedef">The typedef where this type is declared.</param> protected CSharpFunctionType(ICSharpContext context, IFunctionType source, ICSharpTypedef originatingTypedef) : base(context, source, originatingTypedef) { Debug.Assert(source.OverloadList.Count > 0); ICSharpClass Owner = context.GetClass(source.EmbeddingClass); BaseType = Create(context, source.ResolvedBaseType.Item) as ICSharpTypeWithFeature; Debug.Assert(BaseType != null); foreach (IQueryOverloadType OverloadType in source.OverloadList) { ICSharpQueryOverloadType NewOverloadType = CSharpQueryOverloadType.Create(context, OverloadType, Owner); OverloadTypeList.Add(NewOverloadType); } }
/// <summary> /// Checks for errors before applying a rule. /// </summary> /// <param name="node">The node instance to check.</param> /// <param name="dataList">Optional data collected during inspection of sources.</param> /// <param name="data">Private data to give to Apply() upon return.</param> /// <returns>True if an error occured.</returns> public override bool CheckConsistency(IFunctionFeature node, IDictionary <ISourceTemplate, object> dataList, out object data) { data = null; bool Success = true; // This is ensured because the root node is valid. Debug.Assert(node.OverloadList.Count > 0); IFunctionType ResolvedFeatureType = node.ResolvedAgentType.Item as IFunctionType; Debug.Assert(ResolvedFeatureType != null); foreach (IQueryOverloadType OverloadType in ResolvedFeatureType.OverloadList) { // TODO find why not // Debug.Assert(OverloadType.ConformantResultTable.IsSealed); Debug.Assert(OverloadType.ResultTypeList.Count == OverloadType.ResultTable.Count); } IList <IQueryOverloadType> OverloadTypeList = ResolvedFeatureType.OverloadList; IResultType CommonResults = Feature.CommonResultType(OverloadTypeList); IErrorList CheckErrorList = new ErrorList(); for (int i = 0; i < CommonResults.Count; i++) { Success &= Feature.JoinedResultCheck(OverloadTypeList, i, CommonResults.At(i).ValueType, node, CheckErrorList); } if (!Success) { Debug.Assert(!CheckErrorList.IsEmpty); AddSourceErrorList(CheckErrorList); } if (Success) { data = CommonResults; } return(Success); }
private static bool TypeConformToFunctionType(ICompiledType derivedType, IFunctionType baseType, IErrorList errorList, ISource sourceLocation) { bool Result = false; bool IsHandled = false; if (derivedType is IClassType AsClassType) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; IsHandled = true; } else if (derivedType is IFunctionType AsFunctionType) { Result = FunctionTypeConformToFunctionType(AsFunctionType, baseType, errorList, sourceLocation); IsHandled = true; } else if (derivedType is IProcedureType AsProcedureType) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; IsHandled = true; } else if (derivedType is IPropertyType AsPropertyType) { Result = PropertyTypeConformToFunctionType(AsPropertyType, baseType, errorList, sourceLocation); IsHandled = true; } else if (derivedType is IIndexerType AsIndexerType) { Result = IndexerTypeConformToFunctionType(AsIndexerType, baseType, errorList, sourceLocation); IsHandled = true; } else if (derivedType is ITupleType AsTupleType) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; IsHandled = true; } Debug.Assert(IsHandled); return(Result); }
private static bool FunctionTypeConformToPropertyType(IFunctionType derivedType, IPropertyType baseType, IErrorList errorList, ISource sourceLocation) { bool Result = true; Debug.Assert(derivedType.ResolvedBaseType.IsAssigned); Debug.Assert(baseType.ResolvedBaseType.IsAssigned); Result &= TypeConformToBase(derivedType.ResolvedBaseType.Item, baseType.ResolvedBaseType.Item, errorList, sourceLocation, isConversionAllowed: false); if (baseType.PropertyKind != BaseNode.UtilityType.ReadOnly) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } IErrorList AllOverloadErrorList = new ErrorList(); bool MatchingDerivedOverload = false; foreach (IQueryOverloadType DerivedOverload in derivedType.OverloadList) { IErrorList OverloadErrorList = new ErrorList(); if (!QueryOverloadHasPropertyConformingBase(DerivedOverload, baseType, OverloadErrorList, sourceLocation)) { Debug.Assert(!OverloadErrorList.IsEmpty); AllOverloadErrorList.AddError(OverloadErrorList.At(0)); } else { MatchingDerivedOverload = true; } } if (!MatchingDerivedOverload) { errorList.AddErrors(AllOverloadErrorList); errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } return(Result); }
/// <summary> /// Compares two types. /// </summary> /// <param name="type1">The first type.</param> /// <param name="type2">The second type.</param> public static bool TypesHaveIdenticalSignature(IFunctionType type1, IFunctionType type2) { bool IsIdentical = true; IsIdentical &= ObjectType.TypesHaveIdenticalSignature(type1.ResolvedBaseType.Item, type2.ResolvedBaseType.Item); IsIdentical &= type1.OverloadList.Count != type2.OverloadList.Count; foreach (IQueryOverloadType Overload1 in type1.OverloadList) { bool MatchingOverload = false; foreach (IQueryOverloadType Overload2 in type2.OverloadList) { MatchingOverload |= QueryOverloadType.QueryOverloadsHaveIdenticalSignature(Overload1, Overload2); } IsIdentical &= MatchingOverload; } return(IsIdentical); }
// 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); }
private static bool PropertyTypeConformToFunctionType(IPropertyType derivedType, IFunctionType baseType, IErrorList errorList, ISource sourceLocation) { bool Result = true; Debug.Assert(derivedType.ResolvedBaseType.IsAssigned); Debug.Assert(baseType.ResolvedBaseType.IsAssigned); Result &= TypeConformToBase(derivedType.ResolvedBaseType.Item, baseType.ResolvedBaseType.Item, errorList, sourceLocation, isConversionAllowed: false); if (derivedType.PropertyKind == BaseNode.UtilityType.WriteOnly) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } if (baseType.OverloadList.Count > 1) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } IQueryOverloadType SingleOverload = baseType.OverloadList[0]; if (SingleOverload.ParameterList.Count > 0 || SingleOverload.ResultList.Count != 1 || SingleOverload.ParameterEnd == BaseNode.ParameterEndStatus.Open) { errorList.AddError(new ErrorTypeKindConformance(sourceLocation, derivedType, baseType)); Result = false; } IEntityDeclaration OverloadResult = SingleOverload.ResultList[0]; Debug.Assert(OverloadResult.ValidEntity.IsAssigned); Debug.Assert(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.IsAssigned); Debug.Assert(derivedType.ResolvedEntityType.IsAssigned); Result &= TypeConformToBase(OverloadResult.ValidEntity.Item.ResolvedEffectiveType.Item, derivedType.ResolvedEntityType.Item, errorList, sourceLocation, isConversionAllowed: false); Result &= ExceptionListConformToBase(derivedType.GetExceptionIdentifierList, SingleOverload.ExceptionIdentifierList, errorList, sourceLocation); return(Result); }
/// <summary> /// Create a new C# type. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">The Easly type from which the C# type is created.</param> public static ICSharpFunctionType Create(ICSharpContext context, IFunctionType source) { return(new CSharpFunctionType(context, source)); }
/// <summary> /// Create a new C# type. /// </summary> /// <param name="context">The creation context.</param> /// <param name="source">The Easly type from which the C# type is created.</param> /// <param name="originatingTypedef">The typedef where this type is declared.</param> public static ICSharpFunctionType Create(ICSharpContext context, IFunctionType source, ICSharpTypedef originatingTypedef) { return(new CSharpFunctionType(context, source, originatingTypedef)); }
/// <summary> /// Locates, or creates, a resolved function type. /// </summary> /// <param name="typeTable">The table of existing types.</param> /// <param name="baseTypeName">Name of the resolved base type.</param> /// <param name="baseType">The base type.</param> /// <param name="resolvedBaseType">The resolved base type.</param> /// <param name="overloadList">The list of resolved overloads.</param> /// <param name="resolvedTypeName">The type name upon return.</param> /// <param name="resolvedType">The type upon return.</param> public static void ResolveType(ISealableDictionary <ITypeName, ICompiledType> typeTable, ITypeName baseTypeName, IObjectType baseType, ICompiledTypeWithFeature resolvedBaseType, IList <IQueryOverloadType> overloadList, out ITypeName resolvedTypeName, out IFunctionType resolvedType) { if (!TypeTableContaining(typeTable, resolvedBaseType, overloadList, out resolvedTypeName, out resolvedType)) { BuildType(baseTypeName, baseType, resolvedBaseType, overloadList, out resolvedTypeName, out resolvedType); typeTable.Add(resolvedTypeName, resolvedType); } }
/// <summary> /// Checks if a matching function type exists in a type table. /// </summary> /// <param name="typeTable">The table of existing types.</param> /// <param name="baseType">The resolved base type.</param> /// <param name="overloadList">The list of resolved overloads.</param> /// <param name="resolvedTypeName">The type name upon return.</param> /// <param name="resolvedType">The type upon return.</param> public static bool TypeTableContaining(ISealableDictionary <ITypeName, ICompiledType> typeTable, ICompiledType baseType, IList <IQueryOverloadType> overloadList, out ITypeName resolvedTypeName, out IFunctionType resolvedType) { resolvedTypeName = null; resolvedType = null; bool Result = false; foreach (KeyValuePair <ITypeName, ICompiledType> Entry in typeTable) { if (Entry.Value is IFunctionType AsFunctionType) { if (AsFunctionType.ResolvedBaseType.Item == baseType && AsFunctionType.OverloadList.Count == overloadList.Count) { bool AllOverloadsEqual = true; foreach (IQueryOverloadType OverloadItem in overloadList) { AllOverloadsEqual &= IsQueryOverloadMatching(typeTable, OverloadItem, AsFunctionType.OverloadList); } if (AllOverloadsEqual) { resolvedTypeName = Entry.Key; resolvedType = AsFunctionType; Result = true; } } } } return(Result); }
/// <summary> /// Creates a function type with resolved arguments. /// </summary> /// <param name="baseTypeName">Name of the resolved base type.</param> /// <param name="baseType">The base type.</param> /// <param name="resolvedBaseType">The resolved base type.</param> /// <param name="overloadList">The list of resolved overloads.</param> /// <param name="resolvedTypeName">The type name upon return.</param> /// <param name="resolvedType">The type upon return.</param> public static void BuildType(ITypeName baseTypeName, IObjectType baseType, ICompiledTypeWithFeature resolvedBaseType, IList <IQueryOverloadType> overloadList, out ITypeName resolvedTypeName, out IFunctionType resolvedType) { FunctionType ResolvedFunctionType = new FunctionType(baseTypeName, baseType, resolvedBaseType, overloadList); foreach (IQueryOverloadType Item in overloadList) { foreach (IEntityDeclaration Entity in Item.ResultList) { ITypeName EntityTypeName = Entity.ValidEntity.Item.ResolvedEffectiveTypeName.Item; ICompiledType EntityType = Entity.ValidEntity.Item.ResolvedEffectiveType.Item; string EntityName = Entity.ValidEntity.Item.ValidFeatureName.Item.Name; IExpressionType ResultExpressionType = new ExpressionType(EntityTypeName, EntityType, EntityName); Item.ResultTypeList.Add(ResultExpressionType); } } resolvedTypeName = new TypeName(ResolvedFunctionType.TypeFriendlyName); resolvedType = ResolvedFunctionType; }
private static bool ResolveFeatureAsFunctionType(IQueryExpression node, IErrorList errorList, ICompiledFeature resolvedFinalFeature, List <IExpressionType> mergedArgumentList, TypeArgumentStyles typeArgumentStyle, IFunctionType finalType, ref ResolvedExpression resolvedExpression) { // IScopeAttributeFeature is the case of an agent. IList <IArgument> ArgumentList = node.ArgumentList; IFunctionFeature AsFunctionFeature = resolvedFinalFeature as IFunctionFeature; IScopeAttributeFeature AsScopeAttributeFeature = resolvedFinalFeature as IScopeAttributeFeature; Debug.Assert(AsFunctionFeature != null || AsScopeAttributeFeature != null); IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >(); foreach (IQueryOverloadType Overload in finalType.OverloadList) { ParameterTableList.Add(Overload.ParameterTable); } int SelectedIndex; if (!Argument.ArgumentsConformToParameters(ParameterTableList, mergedArgumentList, typeArgumentStyle, errorList, node, out SelectedIndex)) { return(false); } resolvedExpression.SelectedOverloadType = finalType.OverloadList[SelectedIndex]; resolvedExpression.ResolvedResult = new ResultType(resolvedExpression.SelectedOverloadType.ResultTypeList); resolvedExpression.ResolvedException = new ResultException(resolvedExpression.SelectedOverloadType.ExceptionIdentifierList); if (AsFunctionFeature != null) { Debug.Assert(AsFunctionFeature.OverloadList.Count == finalType.OverloadList.Count); Debug.Assert(AsFunctionFeature.ResolvedAgentType.IsAssigned); Debug.Assert(AsFunctionFeature.ResolvedAgentType.Item == finalType); resolvedExpression.SelectedOverload = AsFunctionFeature.OverloadList[SelectedIndex]; resolvedExpression.SelectedResultList = resolvedExpression.SelectedOverload.ResultTable; resolvedExpression.FeatureCall = new FeatureCall(resolvedExpression.SelectedOverload.ParameterTable, resolvedExpression.SelectedOverload.ResultTable, ArgumentList, mergedArgumentList, typeArgumentStyle); } else { resolvedExpression.SelectedResultList = resolvedExpression.SelectedOverloadType.ResultTable; resolvedExpression.FeatureCall = new FeatureCall(resolvedExpression.SelectedOverloadType.ParameterTable, resolvedExpression.SelectedOverloadType.ResultTable, ArgumentList, mergedArgumentList, typeArgumentStyle); } return(true); }
public IfThen(IFunctionType <bool> cond) { this.cond = cond; }
public IrFunction AddFunction(string name, IFunctionType signature) { return(CreateFunction(name, signature)); }
public GenericFunctionWrapper(IFunctionType child, IEnumerable <string> bindings) : base(child, bindings) { Child = child; }
private static bool ResolveCallFunction(IPrecursorExpression node, IFeatureInstance selectedPrecursor, IFunctionType callType, List <IExpressionType> mergedArgumentList, TypeArgumentStyles argumentStyle, IErrorList errorList, ref ResolvedExpression resolvedExpression, out ISealableList <IParameter> selectedParameterList, out ISealableList <IParameter> selectedResultList, out List <IExpressionType> resolvedArgumentList) { selectedParameterList = null; selectedResultList = null; resolvedArgumentList = null; IList <IArgument> ArgumentList = node.ArgumentList; ICompiledFeature OperatorFeature = selectedPrecursor.Feature; IList <ISealableList <IParameter> > ParameterTableList = new List <ISealableList <IParameter> >(); foreach (IQueryOverloadType Overload in callType.OverloadList) { ParameterTableList.Add(Overload.ParameterTable); } if (!Argument.ArgumentsConformToParameters(ParameterTableList, mergedArgumentList, argumentStyle, errorList, node, out int SelectedIndex)) { return(false); } IFunctionFeature AsFunctionFeature = OperatorFeature as IFunctionFeature; Debug.Assert(AsFunctionFeature != null); Debug.Assert(AsFunctionFeature.OverloadList.Count == callType.OverloadList.Count); resolvedExpression.SelectedOverload = AsFunctionFeature.OverloadList[SelectedIndex]; resolvedExpression.SelectedOverloadType = callType.OverloadList[SelectedIndex]; resolvedExpression.ResolvedResult = new ResultType(resolvedExpression.SelectedOverloadType.ResultTypeList); resolvedExpression.ResolvedException = new ResultException(resolvedExpression.SelectedOverloadType.ExceptionIdentifierList); selectedParameterList = resolvedExpression.SelectedOverloadType.ParameterTable; selectedResultList = resolvedExpression.SelectedOverloadType.ResultTable; resolvedArgumentList = mergedArgumentList; return(true); }
public GenericFunctionWrapper(IFunctionType child, IDictionary <string, IType> bindings, IList <string> handle) : base(child, bindings, handle) { Child = child; }