public override void VisitParameter(ParameterSyntax node) { var declaringMethodName = ".ctor"; var declaringMethodOrCtor = (BaseMethodDeclarationSyntax)node.Parent.Parent; var declaringType = declaringMethodOrCtor.ResolveDeclaringType(); if (node.Parent.Parent.IsKind(SyntaxKind.MethodDeclaration)) { var declaringMethod = (MethodDeclarationSyntax)declaringMethodOrCtor; declaringMethodName = declaringMethod.Identifier.ValueText; } var paramVar = TempLocalVar(node.Identifier.ValueText); Context.DefinitionVariables.RegisterNonMethod(string.Empty, node.Identifier.ValueText, MemberKind.Parameter, paramVar); var tbf = new MethodDefinitionVariable( declaringType.Identifier.Text, declaringMethodName, declaringMethodOrCtor.ParameterList.Parameters.Select(p => Context.GetTypeInfo(p.Type).Type.Name).ToArray()); var declaringMethodVariable = Context.DefinitionVariables.GetMethodVariable(tbf).VariableName; var exps = CecilDefinitionsFactory.Parameter(node, Context.SemanticModel, declaringMethodVariable, paramVar, ResolveType(node.Type)); AddCecilExpressions(exps); HandleAttributesInMemberDeclaration(node.AttributeLists, paramVar); base.VisitParameter(node); }
public override void VisitIndexerDeclaration(IndexerDeclarationSyntax node) { var propertyType = ResolveType(node.Type); var propertyDeclaringTypeVar = Context.DefinitionVariables.GetLastOf(MemberKind.Type).VariableName; var propName = "Item"; AddDefaultMemberAttribute(propertyDeclaringTypeVar, propName); var propDefVar = AddPropertyDefinition(propName, propertyType); var paramsVar = new List <string>(); foreach (var parameter in node.ParameterList.Parameters) { var paramVar = TempLocalVar(parameter.Identifier.ValueText); paramsVar.Add(paramVar); var exps = CecilDefinitionsFactory.Parameter(parameter, Context.SemanticModel, propDefVar, paramVar, ResolveType(parameter.Type)); AddCecilExpressions(exps); } ProcessPropertyAccessors(node, propertyDeclaringTypeVar, propName, propertyType, propDefVar, paramsVar); AddCecilExpression($"{propertyDeclaringTypeVar}.Properties.Add({propDefVar});"); HandleAttributesInMemberDeclaration(node.AttributeLists, TargetDoesNotMatch, SyntaxKind.FieldKeyword, propDefVar); // Normal property attrs HandleAttributesInMemberDeclaration(node.AttributeLists, TargetMatches, SyntaxKind.FieldKeyword, backingFieldVar); // [field: attr], i.e, attr belongs to the backing field. }
public override void VisitDelegateDeclaration(DelegateDeclarationSyntax node) { var typeVar = LocalVariableNameForId(NextLocalVariableTypeId()); var accessibility = ModifiersToCecil("TypeAttributes", node.Modifiers, "Private"); var typeDef = CecilDefinitionsFactory.Type( Context, typeVar, node.Identifier.ValueText, DefaultTypeAttributeFor(node).AppendModifier(accessibility), Context.TypeResolver.Resolve("System.MulticastDelegate"), false, Array.Empty <string>(), node.TypeParameterList, "IsAnsiClass = true"); AddCecilExpressions(typeDef); HandleAttributesInMemberDeclaration(node.AttributeLists, typeVar); using (Context.DefinitionVariables.WithCurrent("", node.Identifier.ValueText, MemberKind.Type, typeVar)) { // Delegate ctor AddCecilExpression(CecilDefinitionsFactory.Constructor(Context, out var ctorLocalVar, node.Identifier.Text, "MethodAttributes.FamANDAssem | MethodAttributes.Family", new[] { "System.Object", "System.IntPtr" }, "IsRuntime = true")); AddCecilExpression($"{ctorLocalVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("Object")}));"); AddCecilExpression($"{ctorLocalVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("IntPtr")}));"); AddCecilExpression($"{typeVar}.Methods.Add({ctorLocalVar});"); AddDelegateMethod(typeVar, "Invoke", ResolveType(node.ReturnType), node.ParameterList.Parameters, (methodVar, param) => CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, methodVar, TempLocalVar($"{param.Identifier.ValueText}"), ResolveType(param.Type))); // BeginInvoke() method var beginInvokeMethodVar = TempLocalVar("beginInvoke"); AddCecilExpression( $@"var {beginInvokeMethodVar} = new MethodDefinition(""BeginInvoke"", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, {Context.TypeResolver.Resolve("System.IAsyncResult")}) {{ HasThis = true, IsRuntime = true, }};" ); foreach (var param in node.ParameterList.Parameters) { var paramExps = CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, beginInvokeMethodVar, TempLocalVar($"{param.Identifier.ValueText}"), ResolveType(param.Type)); AddCecilExpressions(paramExps); } AddCecilExpression($"{beginInvokeMethodVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.Resolve("System.AsyncCallback")}));"); AddCecilExpression($"{beginInvokeMethodVar}.Parameters.Add(new ParameterDefinition({Context.TypeResolver.ResolvePredefinedType("Object")}));"); AddCecilExpression($"{typeVar}.Methods.Add({beginInvokeMethodVar});"); AddDelegateMethod(typeVar, "EndInvoke", ResolveType(node.ReturnType), node.ParameterList.Parameters, (methodVar, param) => CecilDefinitionsFactory.Parameter(param, Context.SemanticModel, methodVar, TempLocalVar("ar"), Context.TypeResolver.Resolve("System.IAsyncResult"))); base.VisitDelegateDeclaration(node); } void AddDelegateMethod(string typeLocalVar, string methodName, string returnTypeName, in SeparatedSyntaxList <ParameterSyntax> parameters, Func <string, ParameterSyntax, IEnumerable <string> > paramterHandler) { var methodLocalVar = TempLocalVar(methodName.ToLower()); AddCecilExpression( $@"var {methodLocalVar} = new MethodDefinition(""{methodName}"", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, {returnTypeName}) {{ HasThis = true, IsRuntime = true, }};" ); foreach (var param in parameters) { AddCecilExpressions(paramterHandler(methodLocalVar, param)); } AddCecilExpression($"{typeLocalVar}.Methods.Add({methodLocalVar});"); } }