public static Expression Convert(NR.Expression expression, ConverterSettings settings) { if (expression == null) throw new ArgumentNullException("expression"); if (settings == null) throw new ArgumentNullException("settings"); return (Expression)expression.AcceptVisitor(new ConvertVisitor(settings), null); }
public IntroduceMethodContextAction(UnknownMethodResolveResult symbol, Ast.Expression invocationExpr, ITextEditor editor) { if (symbol == null) throw new ArgumentNullException("rr"); if (invocationExpr == null) throw new ArgumentNullException("ex"); if (editor == null) throw new ArgumentNullException("editor"); this.UnknownMethodCall = symbol; this.InvocationExpr = invocationExpr; this.Editor = editor; }
public static Module Convert(NR.CompilationUnit cu, ConverterSettings settings) { if (cu == null) throw new ArgumentNullException("cu"); if (settings == null) throw new ArgumentNullException("settings"); if (settings.IsVisualBasic) cu.AcceptVisitor(new VBNetConstructsConvertVisitor { AddDefaultValueInitializerToLocalVariableDeclarations = false }, null); else cu.AcceptVisitor(new CSharpConstructsConvertVisitor(), null); return (Module)cu.AcceptVisitor(new ConvertVisitor(settings), null); }
IEnumerable<Ast.ParameterDeclarationExpression> CreateParameters(UnknownMethodResolveResult rr, ClassFinder context, Ast.InvocationExpression invocation) { List<string> usedNames = new List<string>(); for (int i = 0; i < rr.Arguments.Count; i++) { IReturnType type = rr.Arguments[i]; if (type is LambdaReturnType) type = (type as LambdaReturnType).ToDefaultDelegate(); Ast.TypeReference typeRef = CodeGenerator.ConvertType(type, context); typeRef = typeRef.IsNull ? new Ast.TypeReference("object", true) : typeRef; Ast.Expression ex = invocation.Arguments[i]; string paramName = IsNumericType(type) ? "num" + i : type.Name + i.ToString(); if (ex is Ast.IdentifierExpression) { paramName = (ex as Ast.IdentifierExpression).Identifier; } if (ex is Ast.MemberReferenceExpression) { paramName = (ex as Ast.MemberReferenceExpression).MemberName; } Ast.ParameterModifiers mod = Ast.ParameterModifiers.None; if (ex is Ast.DirectionExpression) { var dex = ex as Ast.DirectionExpression; if (dex.Expression is Ast.IdentifierExpression) { paramName = (dex.Expression as Ast.IdentifierExpression).Identifier; } if (dex.Expression is Ast.MemberReferenceExpression) { paramName = (dex.Expression as Ast.MemberReferenceExpression).MemberName; } mod = dex.FieldDirection == Ast.FieldDirection.Out ? Ast.ParameterModifiers.Out : (dex.FieldDirection == Ast.FieldDirection.Ref ? Ast.ParameterModifiers.Ref : Ast.ParameterModifiers.None); } paramName = rr.CallingClass.ProjectContent.Language.CodeGenerator.GetParameterName(paramName); if (usedNames.Contains(paramName)) paramName += i.ToString(); usedNames.Add(paramName); yield return new Ast.ParameterDeclarationExpression(typeRef, paramName) { ParamModifier = mod }; } }
internal void ExecuteIntroduceMethod(UnknownMethodResolveResult rr, Ast.Expression invocationExpr, ITextEditor editor, bool isNew, object result) { IClass targetClass = IsEqualClass(rr.CallingClass, rr.Target.GetUnderlyingClass()) ? rr.CallingClass : rr.Target.GetUnderlyingClass(); CodeGenerator gen = targetClass.ProjectContent.Language.CodeGenerator; IAmbience ambience = targetClass.ProjectContent.Language.GetAmbience(); ClassFinder finder = new ClassFinder(rr.CallingMember); ModifierEnum modifiers = ModifierEnum.None; bool isExtension = !targetClass.IsUserCode(); if (IsEqualClass(rr.CallingClass, targetClass)) { if (rr.CallingMember != null) modifiers |= (rr.CallingMember.Modifiers & ModifierEnum.Static); } else { if (isExtension) { if (isNew) targetClass = rr.CallingClass; else targetClass = result as IClass; } // exclude in Unit Test mode if (WorkbenchSingleton.Workbench != null) editor = (FileService.OpenFile(targetClass.CompilationUnit.FileName) as ITextEditorProvider).TextEditor; if (targetClass.ClassType != ClassType.Interface) modifiers |= ModifierEnum.Public; if (rr.IsStaticContext) modifiers |= ModifierEnum.Static; } NRefactoryResolver resolver = Extensions.CreateResolverForContext(targetClass.ProjectContent.Language, editor); IReturnType type = resolver.GetExpectedTypeFromContext(invocationExpr); Ast.TypeReference typeRef = CodeGenerator.ConvertType(type, finder); if (typeRef.IsNull) { if (invocationExpr.Parent is Ast.ExpressionStatement) typeRef = new Ast.TypeReference("void", true); else typeRef = new Ast.TypeReference("object", true); } Ast.MethodDeclaration method = new Ast.MethodDeclaration { Name = rr.CallName, Modifier = CodeGenerator.ConvertModifier(modifiers, finder), TypeReference = typeRef, Parameters = CreateParameters(rr, finder, invocationExpr as Ast.InvocationExpression).ToList(), }; if (targetClass.ClassType != ClassType.Interface) method.Body = CodeGenerator.CreateNotImplementedBlock(); RefactoringDocumentAdapter documentWrapper = new RefactoringDocumentAdapter(editor.Document); if (isExtension) { method.Parameters.Insert(0, new Ast.ParameterDeclarationExpression(CodeGenerator.ConvertType(rr.Target, finder), "thisInstance")); method.IsExtensionMethod = true; method.Modifier |= Ast.Modifiers.Static; } if (isNew) { Ast.TypeDeclaration newType = new Ast.TypeDeclaration(isExtension ? Ast.Modifiers.Static : Ast.Modifiers.None, null); newType.Name = result as string; newType.AddChild(method); gen.InsertCodeAfter(targetClass, documentWrapper, newType); } else { if (IsEqualClass(rr.CallingClass, targetClass)) gen.InsertCodeAfter(rr.CallingMember, documentWrapper, method); else gen.InsertCodeAtEnd(targetClass.BodyRegion, documentWrapper, method); } if (targetClass.ClassType == ClassType.Interface) return; ParseInformation info = ParserService.ParseFile(targetClass.CompilationUnit.FileName); if (info != null) { IMember newMember; if (isNew) targetClass = info.CompilationUnit.Classes.FirstOrDefault(c => c.DotNetName == c.Namespace + "." + (result as string)); else targetClass = info.CompilationUnit.Classes.Flatten(c => c.InnerClasses).FirstOrDefault(c => c.DotNetName == targetClass.DotNetName); if (targetClass == null) return; if (IsEqualClass(rr.CallingClass, targetClass)) { newMember = targetClass.GetInnermostMember(editor.Caret.Line, editor.Caret.Column); newMember = targetClass.AllMembers .OrderBy(m => m.BodyRegion.BeginLine) .ThenBy(m2 => m2.BodyRegion.BeginColumn) .First(m3 => m3.BodyRegion.BeginLine > newMember.BodyRegion.BeginLine); } else { newMember = targetClass.Methods.Last(); } IDocumentLine line = editor.Document.GetLine(newMember.BodyRegion.BeginLine + 2); int indentLength = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset).Length; editor.Select(line.Offset + indentLength, "throw new NotImplementedException();".Length); } }
static Ast.Expression ConvertIntToBool(Ast.Expression astInt) { return astInt; // return new Ast.ParenthesizedExpression(new Ast.BinaryOperatorExpression(astInt, BinaryOperatorType.InEquality, new Ast.PrimitiveExpression(0, "0"))); }
static Ast.Expression Convert(Ast.Expression expr, string reqType) { // if (expr.UserData.ContainsKey("Type")) { // Cecil.TypeReference exprType = (Cecil.TypeReference)expr.UserData["Type"]; // if (exprType == ByteCode.TypeZero && // reqType == ByteCode.TypeBool.FullName) { // return new PrimitiveExpression(false, "false"); // } // if (exprType == ByteCode.TypeOne && // reqType == ByteCode.TypeBool.FullName) { // return new PrimitiveExpression(true, "true"); // } // } return expr; }
static Ast.Expression Convert(Ast.Expression expr, Cecil.TypeReference reqType) { if (reqType == null) { return expr; } else { return Convert(expr, reqType.FullName); } }
static bool NeedsBody(Ast.AttributedNode type, Ast.AttributedNode member) { if (type is Ast.TypeDeclaration) { var decl = type as Ast.TypeDeclaration; if (decl.Type == Ast.ClassType.Interface) return false; } if (member.Modifier.HasFlag(Ast.Modifiers.Abstract) || member.Modifier.HasFlag(Ast.Modifiers.Extern)) return false; return true; }
static bool IsExternAllowed(Ast.AttributedNode type, Ast.AttributedNode member) { if (type is Ast.TypeDeclaration) { var decl = type as Ast.TypeDeclaration; if (decl.Type == Ast.ClassType.Interface || (decl.Type == Ast.ClassType.Struct && member is Ast.PropertyDeclaration)) return false; } if (member.Modifier.HasFlag(Ast.Modifiers.Abstract)) return false; return true; }
static void CleanType(Ast.AttributedNode type) { foreach (Ast.AttributeSection section in type.Attributes) { section.Attributes.RemoveAll(a => !allowedAttributes.Contains(a.Name)); } type.Attributes.RemoveAll(s => !s.Attributes.Any()); foreach (Ast.MethodDeclaration node in type.Children.OfType<Ast.MethodDeclaration>()) { if (node.Body != null) { if (IsExternAllowed(type, node)) node.Modifier |= Ast.Modifiers.Extern; if (!NeedsBody(type, node)) node.Body = null; } } foreach (Ast.ConstructorDeclaration node in type.Children.OfType<Ast.ConstructorDeclaration>()) { if (IsExternAllowed(type, node)) node.Modifier |= Ast.Modifiers.Extern; if (!NeedsBody(type, node)) node.Body = null; } foreach (Ast.DestructorDeclaration node in type.Children.OfType<Ast.DestructorDeclaration>()) { if (IsExternAllowed(type, node)) node.Modifier |= Ast.Modifiers.Extern; if (!NeedsBody(type, node)) node.Body = null; } foreach (Ast.PropertyDeclaration node in type.Children.OfType<Ast.PropertyDeclaration>()) { if (IsExternAllowed(type, node)) node.Modifier |= Ast.Modifiers.Extern; if (node.HasGetRegion && !NeedsBody(type, node)) node.GetRegion.Block = null; if (node.HasSetRegion && !NeedsBody(type, node)) node.SetRegion.Block = null; } foreach (Ast.AttributedNode node in type.Children.OfType<Ast.AttributedNode>()) CleanType(node); }