Expression GetDefaultValueExpression (AstType astType) { var type = ctx.ResolveType (astType); if ((type.IsReferenceType ?? false) || type.Kind == TypeKind.Dynamic) return new NullReferenceExpression (); var typeDefinition = type.GetDefinition (); if (typeDefinition != null) { switch (typeDefinition.KnownTypeCode) { case KnownTypeCode.Boolean: return new PrimitiveExpression (false); case KnownTypeCode.Char: return new PrimitiveExpression ('\0'); case KnownTypeCode.SByte: case KnownTypeCode.Byte: case KnownTypeCode.Int16: case KnownTypeCode.UInt16: case KnownTypeCode.Int32: return new PrimitiveExpression (0); case KnownTypeCode.Int64: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0L) }; case KnownTypeCode.UInt32: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0U) }; case KnownTypeCode.UInt64: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0U), new PrimitiveExpression (0UL) }; case KnownTypeCode.Single: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0F) }; case KnownTypeCode.Double: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0F), new PrimitiveExpression (0D) }; case KnownTypeCode.Decimal: return new Choice { new PrimitiveExpression (0), new PrimitiveExpression (0M) }; case KnownTypeCode.NullableOfT: return new NullReferenceExpression (); } if (type.Kind == TypeKind.Struct) return new ObjectCreateExpression (astType.Clone ()); } return new DefaultValueExpression (astType.Clone ()); }
/// <summary> /// Converts template types to Object type (useful for inline methods and template specialization types) /// </summary> /// <param name="type"></param> /// <param name="newType"></param> /// <returns>Returns if the type is actually changed or not</returns> public static bool TryPatchTemplateToObjectType(AstType type, out AstType newType) { newType = (AstType)type.Clone(); string name = ""; if (type is SimpleType) { SimpleType st = type as SimpleType; name = st.Identifier; if (Cache.GetTemplateTypes().Contains(name)) { newType = new SimpleType("Object"); return true; } else { if (st.TypeArguments.Any()) { List<AstType> args = new List<AstType>(); bool converted = false; foreach (AstType t in st.TypeArguments) { AstType discard; if (TryPatchTemplateToObjectType(t, out discard)) { converted = true; args.Add(new SimpleType("Object")); } else args.Add((AstType)t.Clone()); } SimpleType nType = (SimpleType)st.Clone(); nType.TypeArguments.Clear(); nType.TypeArguments.AddRange(args.ToArray()); newType = nType; return converted; } } } if (type is PtrType) { if ((type as PtrType).Target is SimpleType) { SimpleType pst = (type as PtrType).Target as SimpleType; AstType tmp; bool converted = TryPatchTemplateToObjectType(pst, out tmp); newType = new PtrType(tmp); return converted; } } return false; }
static MemberReferenceExpression CreateMethodImplReferenceNode(MethodImplOptions option, AstType methodImplOptionsType) { return(new MemberReferenceExpression(methodImplOptionsType.Clone(), Enum.GetName(typeof(MethodImplOptions), option))); }
static List <AstNode> SearchCasts(RefactoringContext ctx, Statement embeddedStatement, Expression obj, AstType type, out ResolveResult rr) { var cast = new Choice { PatternHelper.OptionalParentheses(PatternHelper.OptionalParentheses(obj.Clone()).CastTo(type.Clone())), PatternHelper.OptionalParentheses(PatternHelper.OptionalParentheses(obj.Clone()).CastAs(type.Clone())) }; rr = ctx.Resolve(type); if (rr == null || rr.IsError) { return(null); } return(embeddedStatement.DescendantNodesAndSelf(n => !cast.IsMatch(n)).Where(n => cast.IsMatch(n)).ToList()); }
static Expression CreateReplacementMemberReference(string enumName, AstType baseType, Dictionary <string, string> newNames, MemberReferenceExpression memberToReplace) { return(new ParenthesizedExpression(new CastExpression(baseType.Clone(), new MemberReferenceExpression(new MemberReferenceExpression(memberToReplace.Target.Clone(), enumName), newNames [memberToReplace.MemberName])))); }
static ParenthesizedExpression CreateIdentifierReplacement(string enumName, AstType baseType, Dictionary <string, string> newNames, IdentifierExpression identifier) { var replacement = new ParenthesizedExpression(new CastExpression(baseType.Clone(), new MemberReferenceExpression(new IdentifierExpression(enumName), newNames [identifier.Identifier]))); return(replacement); }
public IEnumerable <CodeAction> GetActions(RefactoringContext context) { //TODO: implement variable assignment & ctor param var varInit = context.GetNode <VariableInitializer>(); if (varInit != null) { AstType type = varInit.GetPrevNode() as AstType; if (type == null) { yield break; } if (varInit.Parent is FieldDeclaration) { yield break; } if (CannotExtractField(varInit)) { yield break; } yield return(new CodeAction("Extract field", s => { var name = varInit.Name; FieldDeclaration field = new FieldDeclaration() { ReturnType = type.Clone(), Variables = { new VariableInitializer(name) } }; AstNode nodeToRemove = RemoveDeclaration(varInit) ? varInit.Parent : type; s.Remove(nodeToRemove, true); s.InsertWithCursor(context.TranslateString("Extract field"), Script.InsertPosition.Before, field); s.FormatText(varInit.Parent); })); } var idntf = context.GetNode <Identifier>(); if (idntf == null) { yield break; } var paramDec = idntf.Parent as ParameterDeclaration; if (paramDec != null) { var ctor = paramDec.Parent as ConstructorDeclaration; if (ctor == null) { yield break; } MemberReferenceExpression thisField = new MemberReferenceExpression(new ThisReferenceExpression(), idntf.Name, new AstType[] {}); var assign = new AssignmentExpression(thisField, AssignmentOperatorType.Assign, new IdentifierExpression(idntf.Name)); var statement = new ExpressionStatement(assign); var type = (idntf.GetPrevNode() as AstType).Clone(); FieldDeclaration field = new FieldDeclaration() { ReturnType = type.Clone(), Variables = { new VariableInitializer(idntf.Name) } }; yield return(new CodeAction("Extract field", s => { s.InsertWithCursor(context.TranslateString("Extract field"), Script.InsertPosition.Before, field); s.AddTo(ctor.Body, statement); })); } }
public IEnumerable <CodeAction> GetActions(RefactoringContext context) { //TODO: implement variable assignment & ctor param var varInit = context.GetNode <VariableInitializer>(); if (varInit != null) { AstType type = varInit.GetPrevNode() as AstType; if (type == null) { yield break; } if (varInit.Parent is FieldDeclaration) { yield break; } if (CannotExtractField(context, varInit)) { yield break; } yield return(new CodeAction(context.TranslateString("Assign to new field"), s => { var name = varInit.Name; AstType extractedType; if (type.IsVar()) { IType resolvedType = context.Resolve(varInit.Initializer).Type; extractedType = context.CreateShortType(resolvedType); } else { extractedType = (AstType)type.Clone(); } AstNode entityDeclarationNode = varInit.Parent; while (!(entityDeclarationNode is EntityDeclaration) || (entityDeclarationNode is Accessor)) { entityDeclarationNode = entityDeclarationNode.Parent; } var entity = (EntityDeclaration)entityDeclarationNode; bool isStatic = entity.HasModifier(Modifiers.Static); FieldDeclaration field = new FieldDeclaration() { Modifiers = isStatic ? Modifiers.Static : Modifiers.None, ReturnType = extractedType, Variables = { new VariableInitializer(name) } }; AstNode nodeToRemove = RemoveDeclaration(varInit) ? varInit.Parent : type; s.Remove(nodeToRemove, true); s.InsertWithCursor(context.TranslateString("Insert new field"), Script.InsertPosition.Before, field); s.FormatText(varInit.Parent); }, varInit)); } var idntf = context.GetNode <Identifier>(); if (idntf == null) { yield break; } var paramDec = idntf.Parent as ParameterDeclaration; if (paramDec != null) { var ctor = paramDec.Parent as ConstructorDeclaration; if (ctor == null) { yield break; } MemberReferenceExpression thisField = new MemberReferenceExpression(new ThisReferenceExpression(), idntf.Name, new AstType[] {}); var assign = new AssignmentExpression(thisField, AssignmentOperatorType.Assign, new IdentifierExpression(idntf.Name)); var statement = new ExpressionStatement(assign); var type = (idntf.GetPrevNode() as AstType).Clone(); FieldDeclaration field = new FieldDeclaration() { ReturnType = type.Clone(), Variables = { new VariableInitializer(idntf.Name) } }; yield return(new CodeAction(context.TranslateString("Assign to new field"), s => { s.InsertWithCursor(context.TranslateString("Insert new field"), Script.InsertPosition.Before, field); s.AddTo(ctor.Body, statement); }, paramDec)); } }
Expression GetDefaultValueExpression(AstType astType) { var type = ctx.ResolveType(astType); if ((type.IsReferenceType ?? false) || type.Kind == TypeKind.Dynamic) { return(new NullReferenceExpression()); } var typeDefinition = type.GetDefinition(); if (typeDefinition != null) { switch (typeDefinition.KnownTypeCode) { case KnownTypeCode.Boolean: return(new PrimitiveExpression(false)); case KnownTypeCode.Char: return(new PrimitiveExpression('\0')); case KnownTypeCode.SByte: case KnownTypeCode.Byte: case KnownTypeCode.Int16: case KnownTypeCode.UInt16: case KnownTypeCode.Int32: return(new PrimitiveExpression(0)); case KnownTypeCode.Int64: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0L) }); case KnownTypeCode.UInt32: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0U) }); case KnownTypeCode.UInt64: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0U), new PrimitiveExpression(0UL) }); case KnownTypeCode.Single: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0F) }); case KnownTypeCode.Double: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0F), new PrimitiveExpression(0D) }); case KnownTypeCode.Decimal: return(new Choice { new PrimitiveExpression(0), new PrimitiveExpression(0M) }); case KnownTypeCode.NullableOfT: return(new NullReferenceExpression()); } if (type.Kind == TypeKind.Struct) { return(new ObjectCreateExpression(astType.Clone())); } } return(new DefaultValueExpression(astType.Clone())); }
static Expression GetDefaultValueExpression(RefactoringContext context, AstType astType) { var type = context.ResolveType(astType); // array if (type.Kind == TypeKind.Array) { return(new ObjectCreateExpression(astType.Clone())); } // enum if (type.Kind == TypeKind.Enum) { var members = type.GetMembers().ToArray(); if (members.Length == 0) { return(new DefaultValueExpression(astType.Clone())); } return(astType.Member(members[0].Name).Clone()); } if ((type.IsReferenceType ?? false) || type.Kind == TypeKind.Dynamic) { return(new NullReferenceExpression()); } var typeDefinition = type.GetDefinition(); if (typeDefinition != null) { switch (typeDefinition.KnownTypeCode) { case KnownTypeCode.Boolean: return(new PrimitiveExpression(false)); case KnownTypeCode.Char: return(new PrimitiveExpression('\0')); case KnownTypeCode.SByte: case KnownTypeCode.Byte: case KnownTypeCode.Int16: case KnownTypeCode.UInt16: case KnownTypeCode.Int32: case KnownTypeCode.UInt32: case KnownTypeCode.Int64: case KnownTypeCode.UInt64: case KnownTypeCode.Single: case KnownTypeCode.Double: case KnownTypeCode.Decimal: return(new PrimitiveExpression(0)); case KnownTypeCode.NullableOfT: return(new NullReferenceExpression()); } if (type.Kind == TypeKind.Struct) { return(new ObjectCreateExpression(astType.Clone())); } } return(new DefaultValueExpression(astType.Clone())); }
static AstType MergeTypes (AstType x, AstType y) { if (TypesEqual (x, y)) { return x.Clone (); } return new PrimitiveType ("object"); }
static Expression GetNotNullTypeCheck (string id, AstType type) { return new IsExpression { Expression = new IdentifierExpression (id), Type = type.Clone (), }; }
List <MethodDeclaration> CreateEqualsOverrides(IType currentClass) { List <MethodDeclaration> methods = new List <MethodDeclaration>(); AstType boolReference = ConvertType(KnownTypeCode.Boolean); AstType objectReference = ConvertType(KnownTypeCode.Object); MethodDeclaration method = new MethodDeclaration { Name = "Equals", Modifiers = Modifiers.Public | Modifiers.Override, ReturnType = boolReference }; method.Parameters.Add(new ParameterDeclaration(objectReference, "obj")); method.Body = new BlockStatement(); AstType currentType = ConvertType(currentClass); Expression expr = null; if (currentClass.Kind == TypeKind.Struct) { // return obj is CurrentType && Equals((CurrentType)obj); expr = new IsExpression() { Expression = new IdentifierExpression("obj"), Type = currentType.Clone() }; expr = new ParenthesizedExpression(expr); expr = new BinaryOperatorExpression( expr, BinaryOperatorType.ConditionalAnd, new InvocationExpression( new IdentifierExpression("Equals"), new List <Expression> { new CastExpression(currentType.Clone(), new IdentifierExpression("obj")) })); method.Body.Add(new ReturnStatement(expr)); methods.Add(method); // IEquatable implementation: method = new MethodDeclaration { Name = "Equals", Modifiers = Modifiers.Public, ReturnType = boolReference.Clone() }; method.Parameters.Add(new ParameterDeclaration(currentType, "other")); method.Body = new BlockStatement(); } else { method.Body.Add(new VariableDeclarationStatement( currentType.Clone(), "other", new IdentifierExpression("obj").CastAs(currentType.Clone()))); method.Body.Add(new IfElseStatement( new BinaryOperatorExpression(new IdentifierExpression("other"), BinaryOperatorType.Equality, new PrimitiveExpression(null, "null")), new ReturnStatement(new PrimitiveExpression(false, "false")))); } expr = null; foreach (IField field in currentClass.GetFields()) { if (field.IsStatic) { continue; } if (expr == null) { expr = TestEquality("other", field); } else { expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd, TestEquality("other", field)); } } foreach (IProperty property in currentClass.GetProperties()) { if (property.IsStatic || !property.IsAutoImplemented()) { continue; } if (expr == null) { expr = TestEquality("other", property); } else { expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd, TestEquality("other", property)); } } method.Body.Add(new ReturnStatement(expr ?? new PrimitiveExpression(true, "true"))); methods.Add(method); return(methods); }