public void ExtensionMethodPreserveParameterTest() { // build "T MyMethod<T, S> (T a, S b)" DomMethod method = new DomMethod(); method.Name = "MyMethod"; method.ReturnType = new DomReturnType("T"); method.AddTypeParameter(new TypeParameter("T")); method.AddTypeParameter(new TypeParameter("S")); method.Add(new DomParameter(method, "a", new DomReturnType("T"))); method.Add(new DomParameter(method, "b", new DomReturnType("S"))); // extend method List <IReturnType> genArgs = new List <IReturnType> (); List <IReturnType> args = new List <IReturnType> (); DomType extType = new DomType("MyType"); ExtensionMethod extMethod = new ExtensionMethod(extType, method, genArgs, args); // check for MyType MyMethod<S> (S b) Assert.AreEqual("MyType", extMethod.ReturnType.FullName); Assert.AreEqual("S", extMethod.Parameters[0].ReturnType.FullName); Assert.AreEqual(1, extMethod.TypeParameters.Count); Assert.AreEqual("S", extMethod.TypeParameters[0].Name); }
public void InstantiatedMethodByArgumentTest_Complex() { // build "T MyMethod<T,S> (S b, KeyValuePair<S, T> a)" DomMethod method = new DomMethod(); method.Name = "MyMethod"; method.ReturnType = new DomReturnType("T"); method.AddTypeParameter(new TypeParameter("T")); method.AddTypeParameter(new TypeParameter("S")); method.Add(new DomParameter(method, "b", new DomReturnType("S"))); DomReturnType returnType = new DomReturnType("KeyValuePair"); returnType.AddTypeParameter(new DomReturnType("T")); returnType.AddTypeParameter(new DomReturnType("S")); method.Add(new DomParameter(method, "a", returnType)); // give int, object as param type List <IReturnType> genArgs = new List <IReturnType> (); List <IReturnType> args = new List <IReturnType> (); genArgs.Add(DomReturnType.Int32); genArgs.Add(DomReturnType.Object); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod(method, genArgs, args); // check Assert.AreEqual(DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); Assert.AreEqual(DomReturnType.Object.FullName, instMethod.Parameters[0].ReturnType.FullName); Assert.AreEqual(DomReturnType.Int32.FullName, instMethod.Parameters[1].ReturnType.GenericArguments[0].FullName); Assert.AreEqual(DomReturnType.Object.FullName, instMethod.Parameters[1].ReturnType.GenericArguments[1].FullName); }
public void InstantiatedMethodByArgumentTestComplex2() { // build "T MyMethod<T> (MyType<T> a)" DomMethod method = new DomMethod(); method.Name = "MyMethod"; method.ReturnType = new DomReturnType("T"); method.AddTypeParameter(new TypeParameter("T")); DomReturnType returnType = new DomReturnType("MyType"); returnType.AddTypeParameter(new DomReturnType("T")); method.Add(new DomParameter(method, "a", returnType)); // give int as param type List <IReturnType> genArgs = new List <IReturnType> (); List <IReturnType> args = new List <IReturnType> (); returnType = new DomReturnType("MyType"); returnType.AddTypeParameter(DomReturnType.Int32); args.Add(returnType); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod(method, genArgs, args); // check Assert.AreEqual(DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); }
IMethod ConstructMethodFromInvocation(RefactoringOptions options) { var resolver = options.GetResolver(); var data = options.GetTextEditorData(); DomMethod result = new DomMethod(methodName, modifiers, MethodModifier.None, DomLocation.Empty, DomRegion.Empty, returnType); result.DeclaringType = new DomType("GeneratedType") { ClassType = declaringType.ClassType }; int i = 1; foreach (var curArg in invocation.Arguments) { var argument = curArg; DomParameter arg = new DomParameter(); if (argument is DirectionExpression) { var de = (DirectionExpression)argument; arg.ParameterModifiers = de.FieldDirection == FieldDirection.Out ? ParameterModifiers.Out : ParameterModifiers.Ref; argument = de.Expression; } string argExpression = data.GetTextBetween(argument.StartLocation.Line, argument.StartLocation.Column, argument.EndLocation.Line, argument.EndLocation.Column); var resolveResult = resolver.Resolve(new ExpressionResult(argExpression), resolvePosition); if (argument is MemberReferenceExpression) { arg.Name = ((MemberReferenceExpression)argument).MemberName; } else if (argument is IdentifierExpression) { arg.Name = ((IdentifierExpression)argument).Identifier; int idx = arg.Name.LastIndexOf('.'); if (idx >= 0) { arg.Name = arg.Name.Substring(idx + 1); } } else { arg.Name = "par" + i++; } arg.Name = char.ToLower(arg.Name[0]) + arg.Name.Substring(1); if (resolveResult != null && resolveResult.ResolvedType != null && !string.IsNullOrEmpty(resolveResult.ResolvedType.FullName)) { arg.ReturnType = resolveResult.ResolvedType; } else { arg.ReturnType = DomReturnType.Object; } result.Add(arg); } return(result); }
public void InstantiatedMethodByParameterTest () { // build "T MyMethod<T> (T[] a)" DomMethod method = new DomMethod (); method.Name = "MyMethod"; method.ReturnType = new DomReturnType ("T"); method.AddTypeParameter (new TypeParameter ("T")); DomReturnType returnType = new DomReturnType ("T"); returnType.ArrayDimensions = 1; method.Add (new DomParameter (method, "a", returnType)); // give int[] as param type. List<IReturnType> genArgs = new List<IReturnType> (); List<IReturnType> args = new List<IReturnType> (); returnType = new DomReturnType (DomReturnType.Int32.FullName); returnType.ArrayDimensions = 1; args.Add (returnType); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod (method, genArgs, args); // check (note that return type should be int and not int[]) Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); Assert.AreEqual (0, instMethod.ReturnType.ArrayDimensions); Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.Parameters[0].ReturnType.FullName); }
//TODO: handle generics public static IMethod CodeDomToMDDomMethod(CodeMemberMethod method) { DomMethod meth = new DomMethod(); meth.Name = method.Name; meth.ReturnType = new DomReturnType(method.ReturnType.BaseType); meth.Modifiers = CodeDomModifiersToMDDom(method.Attributes); foreach (CodeParameterDeclarationExpression dec in method.Parameters) { DomParameter par = new DomParameter(meth, dec.Name, new DomReturnType(dec.Type.BaseType)); if (dec.Direction == FieldDirection.Ref) { par.ParameterModifiers &= ParameterModifiers.Ref; } else if (dec.Direction == FieldDirection.Out) { par.ParameterModifiers &= ParameterModifiers.Out; } else { par.ParameterModifiers &= ParameterModifiers.In; } meth.Add(par); } return(meth); }
public static DomMethod ReadMethod(BinaryReader reader, INameDecoder nameTable) { DomMethod result = new DomMethod(); ReadMemberInformation(reader, nameTable, result); uint explicitInterfaces = ReadUInt(reader, 500); while (explicitInterfaces-- > 0) { result.AddExplicitInterface(ReadReturnType(reader, nameTable)); } result.BodyRegion = ReadRegion(reader, nameTable); result.ReturnType = ReadReturnType(reader, nameTable); result.MethodModifier = (MethodModifier)reader.ReadInt32(); uint arguments = ReadUInt(reader, 5000); while (arguments-- > 0) { result.Add(ReadParameter(reader, nameTable)); } arguments = ReadUInt(reader, 500); while (arguments-- > 0) { result.AddTypeParameter(ReadTypeParameter(reader, nameTable)); } return(result); }
public void InstantiatedMethodByParameterTest() { // build "T MyMethod<T> (T[] a)" DomMethod method = new DomMethod(); method.Name = "MyMethod"; method.ReturnType = new DomReturnType("T"); method.AddTypeParameter(new TypeParameter("T")); DomReturnType returnType = new DomReturnType("T"); returnType.ArrayDimensions = 1; method.Add(new DomParameter(method, "a", returnType)); // give int[] as param type. List <IReturnType> genArgs = new List <IReturnType> (); List <IReturnType> args = new List <IReturnType> (); returnType = new DomReturnType(DomReturnType.Int32.FullName); returnType.ArrayDimensions = 1; args.Add(returnType); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod(method, genArgs, args); // check (note that return type should be int and not int[]) Assert.AreEqual(DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); Assert.AreEqual(0, instMethod.ReturnType.ArrayDimensions); Assert.AreEqual(DomReturnType.Int32.FullName, instMethod.Parameters[0].ReturnType.FullName); }
public override object VisitOperatorDeclaration (ICSharpCode.NRefactory.Ast.OperatorDeclaration operatorDeclaration, object data) { DomMethod method = new DomMethod (); method.Name = GetOperatorName (operatorDeclaration); method.Documentation = RetrieveDocumentation (operatorDeclaration.StartLocation.Line); method.Location = ConvertLocation (operatorDeclaration.StartLocation); method.BodyRegion = ConvertRegion (operatorDeclaration.EndLocation, operatorDeclaration.Body != null ? operatorDeclaration.Body.EndLocation : new ICSharpCode.NRefactory.Location (-1, -1)); method.Modifiers = ConvertModifiers (operatorDeclaration.Modifier) | Modifiers.SpecialName; if (operatorDeclaration.IsExtensionMethod) method.MethodModifier |= MethodModifier.IsExtension; method.ReturnType = ConvertReturnType (operatorDeclaration.TypeReference); AddAttributes (method, operatorDeclaration.Attributes); method.Add (ConvertParameterList (method, operatorDeclaration.Parameters)); AddExplicitInterfaces (method, operatorDeclaration.InterfaceImplementations); if (operatorDeclaration.Templates != null && operatorDeclaration.Templates.Count > 0) { foreach (ICSharpCode.NRefactory.Ast.TemplateDefinition td in operatorDeclaration.Templates) { method.AddTypeParameter (ConvertTemplateDefinition (td)); } } method.DeclaringType = typeStack.Peek (); typeStack.Peek ().Add (method); return null; }
/// <summary> /// Create an IMember from a LanguageItem, /// using the source document to locate declaration bounds. /// </summary> /// <param name="pi"> /// A <see cref="ProjectInformation"/> for the current project. /// </param> /// <param name="item"> /// A <see cref="LanguageItem"/>: The item to convert. /// </param> /// <param name="contentLines"> /// A <see cref="System.String[]"/>: The document in which item is defined. /// </param> static IMember LanguageItemToIMember(ProjectInformation pi, LanguageItem item, string[] contentLines) { if (item is Class || item is Structure) { DomType klass = new DomType(new CompilationUnit(item.File), ClassType.Class, item.Name, new DomLocation((int)item.Line, 1), string.Empty, new DomRegion((int)item.Line + 1, FindFunctionEnd(contentLines, (int)item.Line - 1) + 2), new List <IMember> ()); foreach (LanguageItem li in pi.AllItems()) { if (klass.Equals(li.Parent) && FilePath.Equals(li.File, item.File)) { klass.Add(LanguageItemToIMember(pi, li, contentLines)); } } return(klass); } if (item is Enumeration) { return(new DomType(new CompilationUnit(item.File), ClassType.Enum, item.Name, new DomLocation((int)item.Line, 1), string.Empty, new DomRegion((int)item.Line + 1, (int)item.Line + 1), new List <IMember> ())); } if (item is Function) { DomMethod method = new DomMethod(item.Name, Modifiers.None, MethodModifier.None, new DomLocation((int)item.Line, 1), new DomRegion((int)item.Line + 1, FindFunctionEnd(contentLines, (int)item.Line - 1) + 2), new DomReturnType()); Function function = (Function)item; Match match; bool abort = false; List <IParameter> parameters = new List <IParameter> (); foreach (string parameter in function.Parameters) { match = paramExpression.Match(parameter); if (null == match) { abort = true; break; } DomParameter p = (new DomParameter(method, match.Groups["name"].Value, new DomReturnType(string.Format("{0}{1}{2}", match.Groups["type"].Value, match.Groups["subtype"].Value, match.Groups["array"].Value)))); parameters.Add(p); } if (!abort) { method.Add(parameters); } return(method); } if (item is Member) { return(new DomField(item.Name, Modifiers.None, new DomLocation((int)item.Line, 1), new DomReturnType())); } return(null); }
public void ExtensionMethodTest() { // build "T MyMethod<T, S> (this KeyValuePair<T, S> a, S b)" DomMethod method = new DomMethod(); method.Name = "MyMethod"; method.ReturnType = new DomReturnType("T"); method.AddTypeParameter(new TypeParameter("T")); method.AddTypeParameter(new TypeParameter("S")); DomReturnType returnType = new DomReturnType("KeyValuePair"); returnType.AddTypeParameter(new DomReturnType("T")); returnType.AddTypeParameter(new DomReturnType("S")); method.Add(new DomParameter(method, "a", returnType)); method.Add(new DomParameter(method, "b", new DomReturnType("S"))); // Build extendet type KeyValuePair<int, object> DomType type = new DomType("KeyValuePair"); type.AddTypeParameter(new TypeParameter("T")); type.AddTypeParameter(new TypeParameter("S")); IType extType = DomType.CreateInstantiatedGenericTypeInternal(type, new IReturnType[] { DomReturnType.Int32, DomReturnType.Object }); Console.WriteLine(extType); // extend method List <IReturnType> genArgs = new List <IReturnType> (); List <IReturnType> args = new List <IReturnType> (); ExtensionMethod extMethod = new ExtensionMethod(extType, method, genArgs, args); Console.WriteLine(extMethod); // check Assert.AreEqual(DomReturnType.Int32.FullName, extMethod.ReturnType.FullName); Assert.AreEqual(DomReturnType.Object.FullName, extMethod.Parameters[0].ReturnType.FullName); }
public override object VisitConstructorDeclaration (ICSharpCode.NRefactory.Ast.ConstructorDeclaration constructorDeclaration, object data) { DomMethod constructor = new DomMethod (); constructor.Documentation = RetrieveDocumentation (constructorDeclaration.StartLocation.Line); constructor.Name = ".ctor"; constructor.MethodModifier |= MethodModifier.IsConstructor; constructor.Location = ConvertLocation (constructorDeclaration.StartLocation); constructor.BodyRegion = ConvertRegion (constructorDeclaration.EndLocation, constructorDeclaration.Body != null ? constructorDeclaration.Body.EndLocation : new ICSharpCode.NRefactory.Location (-1, -1)); constructor.Modifiers = ConvertModifiers (constructorDeclaration.Modifier); AddAttributes (constructor, constructorDeclaration.Attributes); constructor.Add (ConvertParameterList (constructor, constructorDeclaration.Parameters)); constructor.DeclaringType = typeStack.Peek (); typeStack.Peek ().Add (constructor); return null; }
IMethod ConstructMethodFromDelegate(RefactoringOptions options) { DomMethod result = new DomMethod(methodName, modifiers, MethodModifier.None, DomLocation.Empty, DomRegion.Empty, returnType); result.DeclaringType = new DomType("GeneratedType") { ClassType = declaringType.ClassType }; IMethod invocation = (IMethod)delegateType.SearchMember("Invoke", true).First(); foreach (var arg in invocation.Parameters) { result.Add(arg); } result.ReturnType = invocation.ReturnType; return(result); }
public void InstantiatedMethodByArgumentTest () { // build "T MyMethod<T> (T a)" DomMethod method = new DomMethod (); method.Name = "MyMethod"; method.ReturnType = new DomReturnType ("T"); method.AddTypeParameter (new TypeParameter ("T")); method.Add (new DomParameter (method, "a", new DomReturnType ("T"))); // give int as param type List<IReturnType> genArgs = new List<IReturnType> (); List<IReturnType> args = new List<IReturnType> (); args.Add (DomReturnType.Int32); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod (method, genArgs, args); // check Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.Parameters[0].ReturnType.FullName); }
IEnumerable <IMethod> BuildFunctions(IEnumerable <PythonFunction> functions) { if (functions == null) { yield break; } foreach (PythonFunction pyFunc in functions) { var domFunc = new DomMethod() { Name = pyFunc.Name, Documentation = pyFunc.Documentation, BodyRegion = pyFunc.Region, Location = new DomLocation(pyFunc.Region.Start.Line - 1, 0), ReturnType = new DomReturnType() { Name = pyFunc.Name, // FIXME: Get inferred type Namespace = Module.FullName, }, }; m_AllWrapped.Add(domFunc); foreach (PythonArgument pyArg in pyFunc.Arguments) { var domArg = new DomParameter() { Name = pyArg.Name, ReturnType = new DomReturnType() { Name = pyArg.Name, // FIXME: Get inferred type Namespace = Module.FullName, }, }; m_AllWrapped.Add(domArg); domFunc.Add(domArg); } yield return(domFunc); } }
public void ReadWriteMethodTest() { DomMethod input = new DomMethod(); input.Name = "Test"; input.MethodModifier = MethodModifier.IsConstructor; input.Add(new DomParameter(input, "par1", DomReturnType.Void)); input.AddTypeParameter(new TypeParameter("T")); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); DomPersistence.Write(writer, DefaultNameEncoder, input); byte[] bytes = ms.ToArray(); DomMethod result = DomPersistence.ReadMethod(CreateReader(bytes), DefaultNameDecoder, null); Assert.AreEqual("Test", result.Name); Assert.AreEqual(true, result.IsConstructor); Assert.AreEqual("par1", result.Parameters [0].Name); Assert.AreEqual("Void", result.Parameters [0].ReturnType.Name); Assert.AreEqual(1, result.TypeParameters.Count); Assert.AreEqual("T", result.TypeParameters [0].Name); }
static DomMethod GenerateMethodStub(RefactoringOptions options, ExtractMethodParameters param) { DomMethod result = new DomMethod(); result.Name = param.Name; result.ReturnType = param.ExpressionType ?? DomReturnType.Void; result.Modifiers = param.Modifiers; if (!param.ReferencesMember) { result.Modifiers |= Modifiers.Static; } if (param.Parameters == null) { return(result); } foreach (var p in param.Parameters) { if (param.OneChangedVariable && p.UsedAfterCutRegion && !p.UsedInCutRegion) { continue; } var newParameter = new DomParameter(); newParameter.Name = p.Name; newParameter.ReturnType = p.ReturnType; if (!param.OneChangedVariable) { if (!p.IsDefinedInsideCutRegion && p.IsChangedInsideCutRegion) { newParameter.ParameterModifiers = p.UsedBeforeCutRegion ? ParameterModifiers.Ref : ParameterModifiers.Out; } } result.Add(newParameter); } return(result); }
/// Adds a signal handler to the class public void BindSignal(Stetic.Signal signal) { if (targetObject == null) { return; } IType cls = GetClass(); if (cls == null) { return; } if (FindSignalHandler(cls, signal) != null) { return; } var met = new DomMethod() { Name = signal.Handler, Modifiers = Modifiers.Protected, ReturnType = new DomReturnType(signal.SignalDescriptor.HandlerReturnTypeName) }; foreach (Stetic.ParameterDescriptor pinfo in signal.SignalDescriptor.HandlerParameters) { met.Add(new DomParameter() { Name = pinfo.Name, ReturnType = new DomReturnType(pinfo.TypeName) }); } CodeGenerationService.AddNewMember(cls, met); }
IEnumerable<IMethod> BuildFunctions(IEnumerable<PythonFunction> functions) { if (functions == null) yield break; foreach (PythonFunction pyFunc in functions) { var domFunc = new DomMethod () { Name = pyFunc.Name, Documentation = pyFunc.Documentation, BodyRegion = pyFunc.Region, Location = new DomLocation (pyFunc.Region.Start.Line - 1, 0), ReturnType = new DomReturnType () { Name = pyFunc.Name, // FIXME: Get inferred type Namespace = Module.FullName, }, }; m_AllWrapped.Add (domFunc); foreach (PythonArgument pyArg in pyFunc.Arguments) { var domArg = new DomParameter () { Name = pyArg.Name, ReturnType = new DomReturnType () { Name = pyArg.Name, // FIXME: Get inferred type Namespace = Module.FullName, }, }; m_AllWrapped.Add (domArg); domFunc.Add (domArg); } yield return domFunc; } }
public override ParsedDocument Parse (ProjectDom dom, string fileName, string content) { ParsedDocument doc = new ParsedDocument (fileName); ProjectInformation pi = ProjectInformationManager.Instance.Get ((null == dom)? null: dom.Project); if(null == doc.CompilationUnit){ doc.CompilationUnit = new CompilationUnit (fileName); } CompilationUnit cu = (CompilationUnit)doc.CompilationUnit; int lastLine = 0; ICollection<Symbol> classes = pi.GetClassesForFile (fileName); if (null == classes || 0 == classes.Count) { return lastGood; } foreach (Symbol node in classes) { if (null == node){ continue; } List<IMember> members = new List<IMember> (); lastLine = node.SourceReferences[0].LastLine; foreach (Symbol child in node.Children) { if (1 > child.SourceReferences.Count || child.SourceReferences[0].File != node.SourceReferences[0].File){ continue; } lastLine = Math.Max (lastLine, child.SourceReferences[0].LastLine+1); switch (child.SymbolType.ToLower ()) { case "class": members.Add (new DomType (new CompilationUnit (fileName), ClassType.Class, child.Name, new DomLocation (child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List<IMember> ())); break; case "interface": members.Add (new DomType (new CompilationUnit (fileName), ClassType.Interface, child.Name, new DomLocation (child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List<IMember> ())); break; case "delegate": members.Add (new DomType (new CompilationUnit (fileName), ClassType.Delegate, child.Name, new DomLocation (child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List<IMember> ())); break; case "struct": members.Add (new DomType (new CompilationUnit (fileName), ClassType.Struct, child.Name, new DomLocation (child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List<IMember> ())); break; case "enum": members.Add (new DomType (new CompilationUnit (fileName), ClassType.Enum, child.Name, new DomLocation (child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List<IMember> ())); break; case "method": case "creationmethod": case "constructor": DomMethod method = new DomMethod (child.Name, Modifiers.None, MethodModifier.None, new DomLocation (child.SourceReferences[0].FirstLine, 1), new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new DomReturnType (child.ReturnType.TypeName)); foreach (DataType param in child.Parameters) { method.Add (new DomParameter (method, param.Name, new DomReturnType (param.TypeName))); } members.Add (method); break; case "property": members.Add (new DomProperty (child.Name, Modifiers.None, new DomLocation (child.SourceReferences[0].FirstLine, 1), new DomRegion (child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new DomReturnType ())); break; case "field": case "constant": case "errorcode": members.Add (new DomField (child.Name, Modifiers.None, new DomLocation (child.SourceReferences[0].FirstLine, 1), new DomReturnType ())); break; case "signal": members.Add (new DomEvent (child.Name, Modifiers.None, new DomLocation (child.SourceReferences[0].FirstLine, 1), new DomReturnType ())); break; default: MonoDevelop.Core.LoggingService.LogDebug ("ValaDocumentParser: Unsupported member type: {0}", child.SymbolType); break; }// Switch on node type }// Collect members cu.Add (new DomType (new CompilationUnit (fileName), ClassType.Class, node.Name, new DomLocation (node.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion (node.SourceReferences[0].FirstLine, int.MaxValue, lastLine, int.MaxValue), members)); }// Add each class in file return (lastGood = doc); }// Parse
public void ReadWriteMethodTest () { DomMethod input = new DomMethod (); input.Name = "Test"; input.MethodModifier = MethodModifier.IsConstructor; input.Add (new DomParameter (input, "par1", DomReturnType.Void)); input.AddTypeParameter (new TypeParameter ("T")); MemoryStream ms = new MemoryStream (); BinaryWriter writer = new BinaryWriter (ms); DomPersistence.Write (writer, DefaultNameEncoder, input); byte[] bytes = ms.ToArray (); DomMethod result = DomPersistence.ReadMethod (CreateReader (bytes), DefaultNameDecoder); Assert.AreEqual ("Test", result.Name); Assert.AreEqual (true, result.IsConstructor); Assert.AreEqual ("par1", result.Parameters [0].Name); Assert.AreEqual ("Void", result.Parameters [0].ReturnType.Name); Assert.AreEqual (1, result.TypeParameters.Count); Assert.AreEqual ("T", result.TypeParameters [0].Name); }
static DomMethod GenerateMethodStub (RefactoringOptions options, ExtractMethodParameters param) { DomMethod result = new DomMethod (); result.Name = param.Name; result.ReturnType = param.ExpressionType ?? DomReturnType.Void; result.Modifiers = param.Modifiers; if (!param.ReferencesMember) result.Modifiers |= MonoDevelop.Projects.Dom.Modifiers.Static; if (param.Parameters == null) return result; foreach (var p in param.Parameters) { if (param.OneChangedVariable && p.UsedAfterCutRegion && !p.UsedInCutRegion) continue; var newParameter = new DomParameter (); newParameter.Name = p.Name; newParameter.ReturnType = p.ReturnType; if (!param.OneChangedVariable) { if (!p.IsDefinedInsideCutRegion && p.IsChangedInsideCutRegion) { newParameter.ParameterModifiers = p.UsedBeforeCutRegion ? ParameterModifiers.Ref : ParameterModifiers.Out; } } result.Add (newParameter); } return result; }
//TODO: handle generics public static IMethod CodeDomToMDDomMethod (CodeMemberMethod method) { DomMethod meth = new DomMethod (); meth.Name = method.Name; meth.ReturnType = new DomReturnType (method.ReturnType.BaseType); meth.Modifiers = CodeDomModifiersToMDDom (method.Attributes); foreach (CodeParameterDeclarationExpression dec in method.Parameters) { DomParameter par = new DomParameter (meth, dec.Name, new DomReturnType (dec.Type.BaseType)); if (dec.Direction == FieldDirection.Ref) par.ParameterModifiers &= ParameterModifiers.Ref; else if (dec.Direction == FieldDirection.Out) par.ParameterModifiers &= ParameterModifiers.Out; else par.ParameterModifiers &= ParameterModifiers.In; meth.Add (par); } return meth; }
/// <summary> /// Populate a DomType with methods /// </summary> void PopulateMethods(DomType parent) { List<int> removal = new List<int> (); Match match; DomMethod m; foreach (KeyValuePair<int,RubyDeclaration> mpair in methods) { if (mpair.Key > parent.Location.Line && mpair.Key < parent.BodyRegion.End.Line) { parent.Add (m = new DomMethod (mpair.Value.name, Modifiers.None, MethodModifier.None, new DomLocation (mpair.Value.beginLine, 1), new DomRegion (mpair.Value.beginLine, mpair.Value.beginColumn+1, mpair.Value.endLine, int.MaxValue), new DomReturnType (string.Empty))); match = methodDefinition.Match (mpair.Value.declaration); if (match.Groups["params"].Success) { foreach (string param in match.Groups["params"].Value.Split (new char[]{',',' ','\t'}, StringSplitOptions.RemoveEmptyEntries)) { m.Add (new DomParameter (m, param, new DomReturnType (param))); } } removal.Add (mpair.Key); }// Add methods that are declared within the parent's scope }// Check detected methods // Remove used methods from map foreach (int key in removal){ methods.Remove (key); } }
IMethod ConstructMethodFromInvocation (RefactoringOptions options) { var resolver = options.GetResolver (); var data = options.GetTextEditorData (); DomMethod result = new DomMethod (methodName, modifiers, MethodModifier.None, DomLocation.Empty, DomRegion.Empty, returnType); result.DeclaringType = new DomType ("GeneratedType") { ClassType = declaringType.ClassType }; int i = 1; foreach (var curArg in invocation.Arguments) { var argument = curArg; DomParameter arg = new DomParameter (); if (argument is DirectionExpression) { var de = (DirectionExpression)argument; arg.ParameterModifiers = de.FieldDirection == FieldDirection.Out ? ParameterModifiers.Out : ParameterModifiers.Ref; argument = de.Expression; } string argExpression = data.GetTextBetween (argument.StartLocation.Line, argument.StartLocation.Column, argument.EndLocation.Line, argument.EndLocation.Column); var resolveResult = resolver.Resolve (new ExpressionResult (argExpression), resolvePosition); if (argument is MemberReferenceExpression) { arg.Name = ((MemberReferenceExpression)argument).Identifier.Name; } else if (argument is IdentifierExpression) { arg.Name = ((IdentifierExpression)argument).Identifier; int idx = arg.Name.LastIndexOf ('.'); if (idx >= 0) arg.Name = arg.Name.Substring (idx + 1); } else { arg.Name = "par" + i++; } arg.Name = char.ToLower (arg.Name[0]) + arg.Name.Substring (1); if (resolveResult != null) { arg.ReturnType = resolveResult.ResolvedType; } else { arg.ReturnType = DomReturnType.Object; } result.Add (arg); } return result; }
/// Adds a signal handler to the class public void BindSignal (Stetic.Signal signal) { if (targetObject == null) return; IType cls = GetClass (); if (cls == null) return; if (FindSignalHandler (cls, signal) != null) return; var met = new DomMethod () { Name = signal.Handler, Modifiers = Modifiers.Protected, ReturnType = new DomReturnType (signal.SignalDescriptor.HandlerReturnTypeName) }; foreach (Stetic.ParameterDescriptor pinfo in signal.SignalDescriptor.HandlerParameters) met.Add (new DomParameter () { Name = pinfo.Name, ReturnType = new DomReturnType (pinfo.TypeName) }); CodeGenerationService.AddNewMember (cls, met); }
public void ExtensionMethodTest () { // build "T MyMethod<T, S> (this KeyValuePair<T, S> a, S b)" DomMethod method = new DomMethod (); method.Name = "MyMethod"; method.ReturnType = new DomReturnType ("T"); method.AddTypeParameter (new TypeParameter ("T")); method.AddTypeParameter (new TypeParameter ("S")); DomReturnType returnType = new DomReturnType ("KeyValuePair"); returnType.AddTypeParameter (new DomReturnType ("T")); returnType.AddTypeParameter (new DomReturnType ("S")); method.Add (new DomParameter (method, "a", returnType)); method.Add (new DomParameter (method, "b", new DomReturnType ("S"))); // Build extendet type KeyValuePair<int, object> DomType type = new DomType ("KeyValuePair"); type.AddTypeParameter (new TypeParameter ("T")); type.AddTypeParameter (new TypeParameter ("S")); IType extType = DomType.CreateInstantiatedGenericTypeInternal (type, new IReturnType[] { DomReturnType.Int32, DomReturnType.Object }); Console.WriteLine (extType); // extend method List<IReturnType> genArgs = new List<IReturnType> (); List<IReturnType> args = new List<IReturnType> (); ExtensionMethod extMethod = new ExtensionMethod (extType, method, genArgs, args); Console.WriteLine (extMethod); // check Assert.AreEqual (DomReturnType.Int32.FullName, extMethod.ReturnType.FullName); Assert.AreEqual (DomReturnType.Object.FullName, extMethod.Parameters[0].ReturnType.FullName); }
public static MonoDevelop.Projects.Dom.INode ConvertDParserToDomNode(D_Parser.Dom.INode n, ParsedDocument doc) { //TODO: DDoc comments! if (n is DMethod) { var dm = n as DMethod; var domMethod = new DomMethod( n.Name, GetNodeModifiers(dm), dm.SpecialType == DMethod.MethodType.Constructor ? MethodModifier.IsConstructor : MethodModifier.None, FromCodeLocation(n.StartLocation), GetBlockBodyRegion(dm), GetReturnType(n)); foreach (var pn in dm.Parameters) { domMethod.Add(new DomParameter(domMethod, pn.Name, GetReturnType(pn))); } domMethod.AddTypeParameter(GetTypeParameters(dm)); foreach (var subNode in dm) { domMethod.AddChild(ConvertDParserToDomNode(subNode, doc)); } return(domMethod); } else if (n is DEnum) { var de = n as DEnum; var domType = new DomType( doc.CompilationUnit, ClassType.Enum, GetNodeModifiers(de), n.Name, FromCodeLocation(n.StartLocation), BuildTypeNamespace(n), GetBlockBodyRegion(de)); foreach (var subNode in de) { domType.Add(ConvertDParserToDomNode(subNode, doc) as IMember); } return(domType); } else if (n is DClassLike) { var dc = n as DClassLike; ClassType ct = ClassType.Unknown; switch (dc.ClassType) { case DTokens.Template: case DTokens.Class: ct = ClassType.Class; break; case DTokens.Interface: ct = ClassType.Interface; break; case DTokens.Union: case DTokens.Struct: ct = ClassType.Struct; break; } var domType = new DomType( doc.CompilationUnit, ct, GetNodeModifiers(dc), n.Name, FromCodeLocation(n.StartLocation), BuildTypeNamespace(n), GetBlockBodyRegion(dc)); domType.AddTypeParameter(GetTypeParameters(dc)); foreach (var subNode in dc) { domType.Add(ConvertDParserToDomNode(subNode, doc) as IMember); } return(domType); } else if (n is DVariable) { var dv = n as DVariable; return(new DomField(n.Name, GetNodeModifiers(dv), FromCodeLocation(n.StartLocation), GetReturnType(n))); } return(null); }
public static MonoDevelop.Projects.Dom.INode ConvertDParserToDomNode(D_Parser.Dom.INode n, ParsedDocument doc) { //TODO: DDoc comments! if (n is DMethod) { var dm = n as DMethod; var domMethod = new DomMethod( n.Name, GetNodeModifiers(dm), dm.SpecialType == DMethod.MethodType.Constructor ? MethodModifier.IsConstructor : MethodModifier.None, FromCodeLocation(n.StartLocation), GetBlockBodyRegion(dm), GetReturnType(n)); foreach (var pn in dm.Parameters) domMethod.Add(new DomParameter(domMethod, pn.Name, GetReturnType(pn))); domMethod.AddTypeParameter(GetTypeParameters(dm)); foreach (var subNode in dm) domMethod.AddChild(ConvertDParserToDomNode(subNode, doc)); return domMethod; } else if (n is DEnum) { var de = n as DEnum; var domType = new DomType( doc.CompilationUnit, ClassType.Enum, GetNodeModifiers(de), n.Name, FromCodeLocation(n.StartLocation), BuildTypeNamespace(n), GetBlockBodyRegion(de)); foreach (var subNode in de) domType.Add(ConvertDParserToDomNode(subNode, doc) as IMember); return domType; } else if (n is DClassLike) { var dc = n as DClassLike; ClassType ct = ClassType.Unknown; switch (dc.ClassType) { case DTokens.Template: case DTokens.Class: ct = ClassType.Class; break; case DTokens.Interface: ct = ClassType.Interface; break; case DTokens.Union: case DTokens.Struct: ct = ClassType.Struct; break; } var domType = new DomType( doc.CompilationUnit, ct, GetNodeModifiers(dc), n.Name, FromCodeLocation(n.StartLocation), BuildTypeNamespace(n), GetBlockBodyRegion(dc)); domType.AddTypeParameter(GetTypeParameters(dc)); foreach (var subNode in dc) domType.Add(ConvertDParserToDomNode(subNode, doc) as IMember); return domType; } else if (n is DVariable) { var dv = n as DVariable; return new DomField(n.Name, GetNodeModifiers(dv), FromCodeLocation(n.StartLocation), GetReturnType(n)); } return null; }
public void ExtensionMethodPreserveParameterTest () { // build "T MyMethod<T, S> (T a, S b)" DomMethod method = new DomMethod (); method.Name = "MyMethod"; method.ReturnType = new DomReturnType ("T"); method.AddTypeParameter (new TypeParameter ("T")); method.AddTypeParameter (new TypeParameter ("S")); method.Add (new DomParameter (method, "a", new DomReturnType ("T"))); method.Add (new DomParameter (method, "b", new DomReturnType ("S"))); // extend method List<IReturnType> genArgs = new List<IReturnType> (); List<IReturnType> args = new List<IReturnType> (); DomType extType = new DomType ("MyType"); ExtensionMethod extMethod = new ExtensionMethod (extType, method, genArgs, args); // check for MyType MyMethod<S> (S b) Assert.AreEqual ("MyType", extMethod.ReturnType.FullName); Assert.AreEqual ("S", extMethod.Parameters[0].ReturnType.FullName); Assert.AreEqual (1, extMethod.TypeParameters.Count); Assert.AreEqual ("S", extMethod.TypeParameters[0].Name); }
public static DomMethod ReadMethod (BinaryReader reader, INameDecoder nameTable, IDomObjectTable objectTable) { DomMethod result = new DomMethod (); ReadMemberInformation (reader, nameTable, objectTable, result); uint explicitInterfaces = ReadUInt (reader, 500); while (explicitInterfaces-- > 0) { result.AddExplicitInterface (ReadReturnType (reader, nameTable, objectTable)); } result.BodyRegion = ReadRegion (reader, nameTable); result.ReturnType = ReadReturnType (reader, nameTable, objectTable); result.MethodModifier = (MethodModifier)reader.ReadInt32 (); uint arguments = ReadUInt (reader, 5000); while (arguments-- > 0) { result.Add (ReadParameter (reader, nameTable, objectTable)); } arguments = ReadUInt (reader, 500); while (arguments-- > 0) { result.AddTypeParameter (ReadTypeParameter (reader, nameTable, objectTable)); } return result; }
IMethod ConstructMethodFromDelegate (RefactoringOptions options) { DomMethod result = new DomMethod (methodName, modifiers, MethodModifier.None, DomLocation.Empty, DomRegion.Empty, returnType); result.DeclaringType = new DomType ("GeneratedType") { ClassType = declaringType.ClassType }; IMethod invocation = (IMethod)delegateType.SearchMember ("Invoke", true).First (); foreach (var arg in invocation.Parameters) { result.Add (arg); } result.ReturnType = invocation.ReturnType; return result; }
public void InstantiatedMethodByArgumentTest_Complex () { // build "T MyMethod<T,S> (S b, KeyValuePair<S, T> a)" DomMethod method = new DomMethod (); method.Name = "MyMethod"; method.ReturnType = new DomReturnType ("T"); method.AddTypeParameter (new TypeParameter ("T")); method.AddTypeParameter (new TypeParameter ("S")); method.Add (new DomParameter (method, "b", new DomReturnType ("S"))); DomReturnType returnType = new DomReturnType ("KeyValuePair"); returnType.AddTypeParameter (new DomReturnType ("T")); returnType.AddTypeParameter (new DomReturnType ("S")); method.Add (new DomParameter (method, "a", returnType)); // give int, object as param type List<IReturnType> genArgs = new List<IReturnType> (); List<IReturnType> args = new List<IReturnType> (); genArgs.Add (DomReturnType.Int32); genArgs.Add (DomReturnType.Object); IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod (method, genArgs, args); // check Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.ReturnType.FullName); Assert.AreEqual (DomReturnType.Object.FullName, instMethod.Parameters[0].ReturnType.FullName); Assert.AreEqual (DomReturnType.Int32.FullName, instMethod.Parameters[1].ReturnType.GenericArguments[0].FullName); Assert.AreEqual (DomReturnType.Object.FullName, instMethod.Parameters[1].ReturnType.GenericArguments[1].FullName); }
public override ParsedDocument Parse(ProjectDom dom, string fileName, string content) { ParsedDocument doc = new ParsedDocument(fileName); ProjectInformation pi = ProjectInformationManager.Instance.Get((null == dom)? null: dom.Project); if (null == doc.CompilationUnit) { doc.CompilationUnit = new CompilationUnit(fileName); } CompilationUnit cu = (CompilationUnit)doc.CompilationUnit; int lastLine = 0; ICollection <Symbol> classes = pi.GetClassesForFile(fileName); if (null == classes || 0 == classes.Count) { return(lastGood); } foreach (Symbol node in classes) { if (null == node) { continue; } List <IMember> members = new List <IMember> (); lastLine = node.SourceReferences[0].LastLine; foreach (Symbol child in node.Children) { if (1 > child.SourceReferences.Count || child.SourceReferences[0].File != node.SourceReferences[0].File) { continue; } lastLine = Math.Max(lastLine, child.SourceReferences[0].LastLine + 1); switch (child.MemberType.ToLower()) { case "class": members.Add(new DomType(new CompilationUnit(fileName), ClassType.Class, child.Name, new DomLocation(child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List <IMember> ())); break; case "interface": members.Add(new DomType(new CompilationUnit(fileName), ClassType.Interface, child.Name, new DomLocation(child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List <IMember> ())); break; case "delegate": members.Add(new DomType(new CompilationUnit(fileName), ClassType.Delegate, child.Name, new DomLocation(child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List <IMember> ())); break; case "struct": members.Add(new DomType(new CompilationUnit(fileName), ClassType.Struct, child.Name, new DomLocation(child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List <IMember> ())); break; case "enum": members.Add(new DomType(new CompilationUnit(fileName), ClassType.Enum, child.Name, new DomLocation(child.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new List <IMember> ())); break; case "method": case "creationmethod": case "constructor": DomMethod method = new DomMethod(child.Name, Modifiers.None, MethodModifier.None, new DomLocation(child.SourceReferences[0].FirstLine, 1), new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new DomReturnType(child.ReturnType.TypeName)); foreach (DataType param in child.Parameters) { method.Add(new DomParameter(method, param.Name, new DomReturnType(param.TypeName))); } members.Add(method); break; case "property": members.Add(new DomProperty(child.Name, Modifiers.None, new DomLocation(child.SourceReferences[0].FirstLine, 1), new DomRegion(child.SourceReferences[0].FirstLine, int.MaxValue, child.SourceReferences[0].LastLine, int.MaxValue), new DomReturnType())); break; case "field": case "constant": case "errorcode": members.Add(new DomField(child.Name, Modifiers.None, new DomLocation(child.SourceReferences[0].FirstLine, 1), new DomReturnType())); break; case "signal": members.Add(new DomEvent(child.Name, Modifiers.None, new DomLocation(child.SourceReferences[0].FirstLine, 1), new DomReturnType())); break; default: MonoDevelop.Core.LoggingService.LogDebug("ValaDocumentParser: Unsupported member type: {0}", child.MemberType); break; } // Switch on node type } // Collect members cu.Add(new DomType(new CompilationUnit(fileName), ClassType.Class, node.Name, new DomLocation(node.SourceReferences[0].FirstLine, 1), string.Empty, new DomRegion(node.SourceReferences[0].FirstLine, int.MaxValue, lastLine, int.MaxValue), members)); } // Add each class in file return(lastGood = doc); } // Parse
/// <summary> /// Create an IMember from a LanguageItem, /// using the source document to locate declaration bounds. /// </summary> /// <param name="pi"> /// A <see cref="ProjectInformation"/> for the current project. /// </param> /// <param name="item"> /// A <see cref="LanguageItem"/>: The item to convert. /// </param> /// <param name="contentLines"> /// A <see cref="System.String[]"/>: The document in which item is defined. /// </param> static IMember LanguageItemToIMember (ProjectInformation pi, LanguageItem item, string[] contentLines) { if (item is Class || item is Structure) { DomType klass = new DomType (new CompilationUnit (item.File), ClassType.Class, item.Name, new DomLocation ((int)item.Line, 1), string.Empty, new DomRegion ((int)item.Line+1, FindFunctionEnd (contentLines, (int)item.Line-1)+2), new List<IMember> ()); foreach (LanguageItem li in pi.AllItems ()) { if (klass.Equals (li.Parent) && FilePath.Equals (li.File, item.File)) { klass.Add (LanguageItemToIMember (pi, li, contentLines)); } } return klass; } if (item is Enumeration) { return new DomType (new CompilationUnit (item.File), ClassType.Enum, item.Name, new DomLocation ((int)item.Line, 1), string.Empty, new DomRegion ((int)item.Line+1, (int)item.Line+1), new List<IMember> ()); } if (item is Function) { DomMethod method = new DomMethod (item.Name, Modifiers.None, MethodModifier.None, new DomLocation ((int)item.Line, 1), new DomRegion ((int)item.Line+1, FindFunctionEnd (contentLines, (int)item.Line-1)+2), new DomReturnType ()); Function function = (Function)item; Match match; bool abort = false; List<IParameter> parameters = new List<IParameter> (); foreach (string parameter in function.Parameters) { match = paramExpression.Match (parameter); if (null == match) { abort = true; break; } DomParameter p = (new DomParameter (method, match.Groups["name"].Value, new DomReturnType (string.Format ("{0}{1}{2}", match.Groups["type"].Value, match.Groups["subtype"].Value, match.Groups["array"].Value)))); parameters.Add (p); } if (!abort) method.Add (parameters); return method; } if (item is Member) { return new DomField (item.Name, Modifiers.None, new DomLocation ((int)item.Line, 1), new DomReturnType ()); } return null; }