public MetadataDefinition(MetadataDefinitionKind kind, string name, ParameterTypeInfo receiverTypeInfo = default) : this() { Kind = kind; Name = name; ReceiverTypeInfo = receiverTypeInfo; }
/// <summary> /// Creates <see cref="TypeMethodInfo" /> for given element. /// </summary> /// <param name="element">Element which <see cref="TypeMethodInfo" /> is created.</param> /// <returns>Created <see cref="TypeMethodInfo" />.</returns> internal TypeMethodInfo BuildFrom(CodeVariable element) { var isShared = element.IsShared; var isAbstract = false; //variables cannot be abstract var declaringType = CreateDescriptor(element.Parent as CodeClass); var variableType = CreateDescriptor(element.Type); //variables cannot have type arguments var methodTypeArguments = TypeDescriptor.NoDescriptors; TypeDescriptor returnType; ParameterTypeInfo[] parameters; var buildGetter = RequiredName.StartsWith(Naming.GetterPrefix); if (buildGetter) { returnType = variableType; parameters = ParameterTypeInfo.NoParams; } else { returnType = TypeDescriptor.Void; parameters = new[] { ParameterTypeInfo.Create("value", variableType) }; } var methodInfo = new TypeMethodInfo( declaringType, RequiredName, returnType, parameters, isShared, methodTypeArguments, isAbstract ); return(methodInfo); }
/// <summary> /// Creates the array that will contains given instances. /// </summary> /// <param name="itemType">Type of the array item.</param> /// <param name="instances">The instances.</param> /// <returns>Instance referene with created array.</returns> internal InstanceRef CreateArray(TypeDescriptor itemType, IEnumerable <InstanceRef> instances) { var instArray = instances.ToArray(); var arrayInfo = TypeDescriptor.Create(string.Format("Array<{0},1>", itemType.TypeName)); var intParam = ParameterTypeInfo.Create("p", TypeDescriptor.Create <int>()); var ctorID = Naming.Method(arrayInfo, Naming.CtorName, false, intParam); var setID = Naming.Method(arrayInfo, "set_Item", false, intParam, ParameterTypeInfo.Create("p2", itemType)); var arrayStorage = getFreeStorage("arr"); emit((e) => { //array construction e.AssignNewObject(arrayStorage, arrayInfo); var lengthVar = e.GetTemporaryVariable("len"); e.AssignLiteral(lengthVar, instArray.Length); e.Call(ctorID, arrayStorage, Arguments.Values(lengthVar)); //set instances to appropriate indexes var arrIndex = e.GetTemporaryVariable("set"); for (int i = 0; i < instArray.Length; ++i) { var instStorage = getStorage(instArray[i]); e.AssignLiteral(arrIndex, i); e.Call(setID, arrayStorage, Arguments.Values(arrIndex, instStorage)); } }); var array = new InstanceRef(this, arrayInfo, true); _instanceStorages[array] = arrayStorage; return(array); }
/// <summary> /// Get import target description where given attribute is defined /// </summary> /// <param name="attribute">Import attribute</param> /// <param name="componentType">Type of defining component</param> /// <param name="importMethodID">Id of method that can be used for import. It is <c>null</c> for self exports</param> /// <param name="importType">Type of defined export</param> /// <returns><c>true</c> if target has been successfully found, <c>false</c> otherwise</returns> private bool getImportTarget(CodeAttribute2 attribute, TypeDescriptor componentType, out MethodID importMethodID, out TypeDescriptor importType) { var target = attribute.Parent as CodeElement; importMethodID = null; importType = null; var name = target.Name(); switch (target.Kind) { case vsCMElement.vsCMElementVariable: //variables are represented by properties within type system importType = _assembly.InfoBuilder.CreateDescriptor((target as CodeVariable).Type); importMethodID = Naming.Method(componentType, Naming.SetterPrefix + name, false, ParameterTypeInfo.Create("value", importType) ); return(true); case vsCMElement.vsCMElementProperty: importType = _assembly.InfoBuilder.CreateDescriptor((target as CodeProperty).Type); importMethodID = Naming.Method(componentType, Naming.SetterPrefix + name, false, ParameterTypeInfo.Create("value", importType) ); return(true); default: return(false); } }
/// <summary> /// Get parameters info for given method base. /// </summary> /// <param name="method">Base method which parameters will be created.</param> /// <returns>Created parameters info.</returns> private ParameterTypeInfo[] getParametersInfo(MethodBase method) { var paramsInfo = new List <ParameterTypeInfo>(); var parameters = method.GetParameters(); var parametersAttribute = method.GetCustomAttributes(typeof(ParameterTypesAttribute), false).FirstOrDefault() as ParameterTypesAttribute; var explicitTypes = parametersAttribute == null ? null : parametersAttribute.ParameterTypes.ToArray(); for (var i = 0; i < parameters.Length; ++i) { var param = parameters[i]; var paramType = explicitTypes == null || explicitTypes.Length <= i ? null : explicitTypes[i]; if (paramType == null) { paramType = Translator.GetTypeDescriptorFromBase(method, (m) => m.GetParameters()[i].ParameterType); //translate default parameters if (TypeDescriptor.InstanceInfo.Equals(paramType)) { paramType = TypeDescriptor.ObjectInfo; } } var paramInfo = ParameterTypeInfo.From(param, paramType); paramsInfo.Add(paramInfo); } return(paramsInfo.ToArray()); }
/// <summary> /// Generate unary info for specified method. /// </summary> /// <param name="methodName">Name of the method.</param> /// <returns>TypeMethodInfo.</returns> private TypeMethodInfo unaryOperatorInfo(string methodName) { var thisInfo = TypeDescriptor.Create <T>(); var op = ParameterTypeInfo.Create("op", thisInfo); var methodInfo = new TypeMethodInfo(thisInfo, methodName, thisInfo, ParameterTypeInfo.NoParams, false, TypeDescriptor.NoDescriptors); return(methodInfo); }
/// <summary> /// Generate binary info for specified method. /// </summary> /// <param name="methodName">Name of the method.</param> /// <param name="resultInfo">The result information.</param> /// <returns>TypeMethodInfo.</returns> private TypeMethodInfo binaryInfo(string methodName, TypeDescriptor resultInfo) { var thisInfo = TypeDescriptor.Create <T>(); var op1 = ParameterTypeInfo.Create("op1", thisInfo); var op2 = ParameterTypeInfo.Create("op2", thisInfo); var methodInfo = new TypeMethodInfo(thisInfo, methodName, resultInfo, new ParameterTypeInfo[] { op2 }, false, TypeDescriptor.NoDescriptors); return(methodInfo); }
public MetadataDefinition( MetadataDefinitionKind kind, string name, ParameterTypeInfo receiverTypeInfo = default, NamespaceDefinition @namespace = default, TypeDefinition type = default) { Kind = kind; Name = name; ReceiverTypeInfo = receiverTypeInfo; Namespace = @namespace; Type = type; }
/// <summary> /// Creates <see cref="TypeMethodInfo" /> for given element. /// </summary> /// <param name="element">Element which <see cref="TypeMethodInfo" /> is created.</param> /// <returns>Created <see cref="TypeMethodInfo" />.</returns> protected TypeMethodInfo BuildFrom(CodeProperty element) { //translate name according to naming conventions of type system var buildGetter = RequiredName.StartsWith(Naming.GetterPrefix); var namePrefix = buildGetter ? Naming.GetterPrefix : Naming.SetterPrefix; var property2 = element as CodeProperty2; var isShared = property2 != null && property2.IsShared; var method = buildGetter ? element.Getter : element.Setter; var isAbstract = method == null || method.MustImplement; var declaringTypeNode = element.DeclaringClass(); var declaringType = CreateDescriptor(declaringTypeNode); var variableType = CreateDescriptor(element.Type); //properties cannot have type arguments var methodTypeArguments = TypeDescriptor.NoDescriptors; TypeDescriptor returnType; ParameterTypeInfo[] parameters; if (buildGetter) { returnType = variableType; parameters = ParameterTypeInfo.NoParams; } else { returnType = TypeDescriptor.Void; parameters = new[] { ParameterTypeInfo.Create("value", variableType) }; } var isIndexer = RequiredName == Naming.IndexerSetter || RequiredName == Naming.IndexerGetter; if (isIndexer) { var indexParameters = CreateParametersInfo(method.Parameters); parameters = indexParameters.Concat(parameters).ToArray(); } var methodInfo = new TypeMethodInfo( declaringType, RequiredName, returnType, parameters, isShared, methodTypeArguments, isAbstract ); return(methodInfo); }
/// <summary> /// Creates parameter's info from given <see cref="CodeElements" /> describing method parameters. /// </summary> /// <param name="parameters">Method parameters described by <see cref="CodeElements" />.</param> /// <returns>Created parameter's info.</returns> public ParameterTypeInfo[] CreateParametersInfo(CodeElements parameters) { var result = new List <ParameterTypeInfo>(); foreach (CodeParameter parameter in parameters) { var paramName = parameter.Name; var paramType = CreateDescriptor(parameter.Type); //TODO: default values handling var parameterInfo = ParameterTypeInfo.Create(paramName, paramType); result.Add(parameterInfo); } return(result.ToArray()); }
/// <summary> /// Create setter info for given field. /// </summary> /// <param name="field">Field which setter is needed.</param> /// <returns>Created setter.</returns> private TypeMethodInfo createSetter(FieldInfo field) { if (field == null) { return(null); } var name = Naming.SetterPrefix + field.Name; var declaringType = TypeDescriptor.Create(field.DeclaringType); var fieldType = TypeDescriptor.Create(field.FieldType); var isStatic = field.IsStatic; return(new TypeMethodInfo(declaringType, name, TypeDescriptor.Void, new ParameterTypeInfo[] { ParameterTypeInfo.Create("value", fieldType) }, isStatic, TypeDescriptor.NoDescriptors)); }
/// <summary> /// Create setter info for given field. /// </summary> /// <param name="field">Field which setter is needed.</param> /// <param name="context">Context of transcription.</param> /// <returns>Created setter.</returns> private TypeMethodInfo createSetter(FieldReference field, TranscriptionContext context) { if (field == null) { return(null); } var name = Naming.SetterPrefix + field.Name; var declaringType = createTypeInfo(field.DeclaringType, context); var fieldType = createTypeInfo(field.FieldType, context); var isStatic = resolveIsStatic(field, context); return(new TypeMethodInfo(declaringType, name, TypeDescriptor.Void, new ParameterTypeInfo[] { ParameterTypeInfo.Create("value", fieldType) }, isStatic, TypeDescriptor.NoDescriptors)); }
/// <summary> /// Apply information available in method reference. /// </summary> /// <param name="methodReference">Method reference of builded method info.</param> private void applyMethod(MethodReference methodReference) { //set default parameters foreach (var param in methodReference.Parameters) { var paramInfo = ParameterTypeInfo.Create(param.Name, GetDescriptor(param.ParameterType)); Parameters.Add(paramInfo); } var name = methodReference.Name; switch (name) { case ".ctor": name = Naming.CtorName; break; case ".cctor": name = Naming.ClassCtorName; break; } //set default MethodName MethodName = name; //set default IsStatic IsStatic = !methodReference.HasThis; //set default ReturnType ReturnType = GetDescriptor(methodReference.ReturnType); //set method generic parameters if available var parameters = methodReference.GenericParameters; //TODO ensure that this doesnt colide with GenericInstanceMethods handling // in applyGenericMethod foreach (var par in parameters) { var parType = TypeHelper.BuildDescriptor(par); TypeArguments.Add(parType); } }
private ParameterTypeInfo getParamToMatch(Argument argument) { ParameterTypeInfo paramToMatch = null; if (argument.IsNamed) { _unresolvedParameters.TryGetValue(argument.Name, out paramToMatch); } else { paramToMatch = getCurrentParam(); if (paramToMatch != null && !paramToMatch.HasParam) { //shift while we have HasParam parameter //we can accept multiple arguments ++_orderedArgIndex; } } return(paramToMatch); }
private void bind(ParameterTypeInfo param, Argument arg) { //TODO resolve score... if (param.Type.IsParameter) { _genericBindings.Add(param, arg.Value.Type); } else { var type = arg.Value.Type; var isNull = Null.TypeInfo.Equals(type); if (!isNull && !param.HasParam && !_context.Services.IsAssignable(param.Type, type)) { //type mismatch IsValid = false; return; } } _argBindings.Add(param, arg.Value); }
private bool isUnresolved(ParameterTypeInfo param) { return(_unresolvedParameters.ContainsKey(param.Name)); }