bool ReadClasses() { int classCount = reader.ReadInt32(); int externalTypeCount = reader.ReadInt32(); types = new IReturnType[classCount + externalTypeCount]; DefaultClass[] classes = new DefaultClass[classCount]; for (int i = 0; i < classes.Length; i++) { DefaultClass c = new DefaultClass(pc.AssemblyCompilationUnit, reader.ReadString()); classes[i] = c; types[i] = c.DefaultReturnType; } for (int i = classCount; i < types.Length; i++) { string name = reader.ReadString(); types[i] = new GetClassReturnType(pc, name, reader.ReadByte()); } stringArray = new string[reader.ReadInt32()]; for (int i = 0; i < stringArray.Length; i++) { stringArray[i] = reader.ReadString(); } ReadAttributes(pc.AssemblyCompilationUnit); for (int i = 0; i < classes.Length; i++) { ReadClass(classes[i]); pc.AddClassToNamespaceList(classes[i]); if (reader.ReadByte() != 64) { return(false); } } return(true); }
public IReturnType ToDefaultDelegate() { IReturnType type = new GetClassReturnType(cu.ProjectContent, "System.Func", 0); List <IReturnType> parameters = new List <IReturnType>(); if (this.HasParameterList) { parameters = MethodParameters.Select(p => p.ReturnType ?? new GetClassReturnType(cu.ProjectContent, "System.Object", 0)).ToList(); } if (this.MethodReturnType != null && this.MethodReturnType.FullyQualifiedName == "System.Void") { type = new GetClassReturnType(cu.ProjectContent, "System.Action", 0); } else { var rt = this.MethodReturnType; if (rt == null) { rt = new GetClassReturnType(cu.ProjectContent, "System.Object", 0); } parameters.Add(rt); } return(new ConstructedReturnType(type, parameters)); }
protected override void OnFullyQualifiedNameChanged(EventArgs e) { base.OnFullyQualifiedNameChanged(e); GetClassReturnType rt = defaultReturnType as GetClassReturnType; if (rt != null) { rt.SetFullyQualifiedName(FullyQualifiedName); } }
protected override string GenerateCode(LanguageProperties language, IClass currentClass) { StringBuilder builder = new StringBuilder(); IDocumentLine line = editor.Document.GetLineForOffset(anchor.Offset); string indent = DocumentUtilitites.GetWhitespaceAfter(editor.Document, line.Offset); bool implementInterface = this.implementInterface.IsChecked == true; bool hasOnPropertyChanged = HasOnPropertyChanged(currentClass); bool useEventArgs = false; if (implementInterface && !currentClass.IsStatic) { if (!hasOnPropertyChanged) { var nodes = new List<AbstractNode>(); var rt = new GetClassReturnType(currentClass.ProjectContent, "System.ComponentModel.INotifyPropertyChanged", 0); if (!currentClass.ClassInheritanceTree.Any(bt => bt.FullyQualifiedName == "System.ComponentModel.INotifyPropertyChanged")) { int insertion = editor.Document.PositionToOffset(currentClass.BodyRegion.BeginLine, currentClass.BodyRegion.BeginColumn); if (currentClass.BaseTypes.Count > 0) editor.Document.Insert(insertion, ", INotifyPropertyChanged"); else editor.Document.Insert(insertion, " : INotifyPropertyChanged"); } language.CodeGenerator.ImplementInterface(nodes, rt, false, currentClass); var ev = rt.GetEvents().First(e => e.Name == "PropertyChanged"); MethodDeclaration onEvent = language.CodeGenerator.CreateOnEventMethod(new DefaultEvent(ev.Name, ev.ReturnType, ev.Modifiers, ev.Region, ev.BodyRegion, currentClass)); nodes.Add(onEvent); onEvent.Parameters[0].TypeReference = new TypeReference("string", true); onEvent.Parameters[0].ParameterName = "propertyName"; ((RaiseEventStatement)onEvent.Body.Children[0]).Arguments[1] = new ObjectCreateExpression(new TypeReference("PropertyChangedEventArgs"), new List<Expression> { new IdentifierExpression("propertyName") }); foreach (var node in nodes) builder.AppendLine(language.CodeGenerator.GenerateCode(node, indent)); useEventArgs = false; } else { useEventArgs = currentClass.DefaultReturnType.GetMethods().First(m => m.Name == "OnPropertyChanged").Parameters[0].ReturnType.FullyQualifiedName != "System.String"; } } foreach (FieldWrapper field in listBox.SelectedItems) { var prop = language.CodeGenerator.CreateProperty(field.Field, true, field.AddSetter); if (!field.Field.IsStatic && !currentClass.IsStatic && field.AddSetter && implementInterface) { var invocation = new ExpressionStatement(CreateInvocation(field.PropertyName, useEventArgs)); var assignment = prop.SetRegion.Block.Children[0]; prop.SetRegion.Block.Children.Clear(); prop.SetRegion.Block.AddChild( new IfElseStatement( new BinaryOperatorExpression(new IdentifierExpression(field.MemberName), BinaryOperatorType.InEquality, new IdentifierExpression("value")), new BlockStatement { Children = { assignment, invocation } } ) ); } builder.AppendLine(language.CodeGenerator.GenerateCode(prop, indent)); } return builder.ToString().Trim(); }
public override bool Equals(object o) { GetClassReturnType rt = o as GetClassReturnType; if (rt == null) { IReturnType rt2 = o as IReturnType; if (rt2 != null && rt2.IsDefaultReturnType) { return(rt2.FullyQualifiedName == fullName && rt2.TypeParameterCount == this.TypeParameterCount); } else { return(false); } } return(fullName == rt.fullName && typeParameterCount == rt.typeParameterCount && content == rt.content); }
bool ReadClasses() { int classCount = reader.ReadInt32(); int externalTypeCount = reader.ReadInt32(); types = new IReturnType[classCount + externalTypeCount]; DefaultClass[] classes = new DefaultClass[classCount]; for (int i = 0; i < classes.Length; i++) { DefaultClass c = new DefaultClass(pc.AssemblyCompilationUnit, reader.ReadString()); classes[i] = c; types[i] = c.DefaultReturnType; } for (int i = classCount; i < types.Length; i++) { string name = reader.ReadString(); types[i] = new GetClassReturnType(pc, name, reader.ReadByte()); } stringArray = new string[reader.ReadInt32()]; for (int i = 0; i < stringArray.Length; i++) { stringArray[i] = reader.ReadString(); } ReadAttributes(pc.AssemblyCompilationUnit); for (int i = 0; i < classes.Length; i++) { ReadClass(classes[i]); pc.AddClassToNamespaceList(classes[i]); if (reader.ReadByte() != 64) { return false; } } return true; }
public static IReturnType CreateReturnType(AST.TypeReference reference, IClass callingClass, IMethodOrProperty callingMember, int caretLine, int caretColumn, IProjectContent projectContent) { System.Diagnostics.Debug.Assert(projectContent != null); if (reference == null) { return GetDefaultReturnType(projectContent); } if (reference is AST.ArrayTypeReference) { AST.ArrayTypeReference arr = (AST.ArrayTypeReference)reference; return new ArrayReturnType(projectContent, CreateReturnType(arr.ElementType, callingClass, callingMember, caretLine, caretColumn, projectContent), (arr.Rank != null) ? (int)arr.Rank.Value : 1); } else if (reference is AST.SimpleTypeReference) { string name = ((AST.SimpleTypeReference)reference).Name; IReturnType rt; int typeParameterCount = (reference is AST.GenericTypeReference) ? ((AST.GenericTypeReference)reference).GenericArguments.Count : 0; if (name == "duck") rt = new BooResolver.DuckClass(new DefaultCompilationUnit(projectContent)).DefaultReturnType; else if (BooAmbience.ReverseTypeConversionTable.ContainsKey(name)) rt = new GetClassReturnType(projectContent, BooAmbience.ReverseTypeConversionTable[name], typeParameterCount); else if (callingClass == null) rt = new GetClassReturnType(projectContent, name, typeParameterCount); else rt = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn, name, typeParameterCount); if (typeParameterCount > 0) { AST.TypeReferenceCollection arguments = ((AST.GenericTypeReference)reference).GenericArguments; // GenericTypeReference derives from SimpleTypeReference IReturnType[] typeArguments = new IReturnType[arguments.Count]; for (int i = 0; i < typeArguments.Length; i++) { typeArguments[i] = CreateReturnType(arguments[i], callingClass, callingMember, caretLine, caretColumn, projectContent); } rt = new ConstructedReturnType(rt, typeArguments); } return rt; } else if (reference is AST.CallableTypeReference) { AST.CallableTypeReference ctr = (AST.CallableTypeReference)reference; AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(projectContent)); if (ctr.ReturnType != null) { amrt.MethodReturnType = CreateReturnType(ctr.ReturnType, callingClass, callingMember, caretLine, caretColumn, projectContent); } amrt.MethodParameters = new List<IParameter>(); AddParameters(ctr.Parameters, amrt.MethodParameters, callingMember, callingClass ?? new DefaultClass(new DefaultCompilationUnit(projectContent), "__Dummy")); return amrt; } else { throw new NotSupportedException("unknown reference type: " + reference.ToString()); } }
public override void OnGeneratorExpression(GeneratorExpression node) { ClearResult(); node.Expression.Accept(this); if (resolveResult != null) { IReturnType enumerable = new GetClassReturnType(projectContent, "System.Collections.Generic.IEnumerable", 1); MakeResult(new ConstructedReturnType(enumerable, new IReturnType[] { resolveResult.ResolvedType })); } else { MakeResult(new GetClassReturnType(projectContent, "System.Collections.IEnumerable", 0)); } }
CodeExpression CreateMemberExpression(CodeExpression target, string parentName, string name, bool isStatic) { _fieldReferenceType = null; string combinedName = parentName + "." + name; if (pc.GetClass(combinedName, 0) != null) return new CodeTypeReferenceExpression(combinedName); else if (pc.NamespaceExists(combinedName)) return new CodeTypeReferenceExpression(combinedName); GetClassReturnType rt = new GetClassReturnType(pc, parentName, 0); foreach (IProperty prop in rt.GetProperties()) { if (prop.IsStatic == isStatic && prop.Name == name) { _fieldReferenceType = prop.ReturnType; return new CodePropertyReferenceExpression(target, name); } } foreach (IEvent ev in rt.GetEvents()) { if (ev.IsStatic == isStatic && ev.Name == name) { _fieldReferenceType = ev.ReturnType; return new CodeEventReferenceExpression(target, name); } } foreach (IMethod me in rt.GetMethods()) { if (me.IsStatic == isStatic && me.Name == name) { _fieldReferenceType = me.ReturnType; CodeMethodReferenceExpression cmre = new CodeMethodReferenceExpression(target, name); cmre.UserData["methodUserData"] = me; return cmre; } } foreach (IField field in rt.GetFields()) { if (field.IsStatic == isStatic && field.Name == name) { _fieldReferenceType = field.ReturnType; return new CodeFieldReferenceExpression(target, name); } } // unknown member, guess: if (char.IsUpper(name, 0)) return new CodePropertyReferenceExpression(target, name); else return new CodeFieldReferenceExpression(target, name); }
public override void OnYieldStatement(YieldStatement node) { noReturnStatement = false; IProjectContent pc = context != null ? context.ProjectContent : ParserService.CurrentProjectContent; IReturnType enumerable = new GetClassReturnType(pc, "System.Collections.Generic.IEnumerable", 1); // Prevent creating an infinite number of InferredReturnTypes in inferring cycles parentReturnType.expression = new NullLiteralExpression(); IReturnType returnType; if (node.Expression == null) returnType = ConvertVisitor.GetDefaultReturnType(pc); else returnType = new BooResolver().GetTypeOfExpression(node.Expression, context); if (returnType != null) { returnType.GetUnderlyingClass(); // force to infer type } if (parentReturnType.expression == null) { // inferrence cycle with parentReturnType returnType = new GetClassReturnType(pc, "?", 0); } parentReturnType.expression = null; result = new ConstructedReturnType(enumerable, new IReturnType[] { returnType }); }