private static void GenerateMethod() { Method method = new Method(); method.Name = "MyNewProc"; method.MethodType = MethodTypeEnum.Void; Param newParam = new Param(); TypeReferenceExpression newTypeReferenceExpression = new TypeReferenceExpression(); newTypeReferenceExpression.Name = CodeRush.Language.GetSimpleTypeName("System.Int32"); newParam.MemberTypeReference = newTypeReferenceExpression; newParam.Name = "MyKillerParameter"; method.Parameters.Add(newParam); MethodCall statement = new MethodCall(); statement.Name = "Start"; //UnaryIncrement newUnaryIncrement = new UnaryIncrement(); //ElementReferenceExpression elementReferenceExpression = new ElementReferenceExpression(newParam.Name); //newUnaryIncrement.Expression = elementReferenceExpression; //statement.AddDetailNode(newUnaryIncrement); //int MyKillerParameter = 0; //MyKillerParameter++; method.AddNode(statement); string newCode = CodeRush.Language.GenerateElement(method); TextDocument activeTextDocument = CodeRush.Documents.ActiveTextDocument; if (activeTextDocument == null) return; activeTextDocument.InsertText(activeTextDocument.ActiveView.Caret.SourcePoint, newCode); }
public void Run (RefactoringContext context) { var switchStatement = GetSwitchStatement (context); var result = context.Resolve (switchStatement.Expression); var type = result.Type; var newSwitch = (SwitchStatement)switchStatement.Clone (); var target = new TypeReferenceExpression (context.CreateShortType (result.Type.Resolve (context.TypeResolveContext))); foreach (var field in type.GetFields (context.TypeResolveContext)) { if (field.IsSynthetic || !field.IsConst) continue; newSwitch.SwitchSections.Add (new SwitchSection () { CaseLabels = { new CaseLabel (new MemberReferenceExpression (target.Clone (), field.Name)) }, Statements = { new BreakStatement () } }); } newSwitch.SwitchSections.Add (new SwitchSection () { CaseLabels = { new CaseLabel () }, Statements = { new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System", "ArgumentOutOfRangeException"))) } }); using (var script = context.StartScript ()) { script.Replace (switchStatement, newSwitch); } }
public override object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data) { if (objectCreateExpression.Arguments.Count() == 2) { Expression obj = objectCreateExpression.Arguments.First(); Expression func = objectCreateExpression.Arguments.Last(); Annotation annotation = func.Annotation<Annotation>(); if (annotation != null) { IdentifierExpression methodIdent = (IdentifierExpression)((InvocationExpression)func).Arguments.Single(); MethodReference method = methodIdent.Annotation<MethodReference>(); if (method != null) { if (HandleAnonymousMethod(objectCreateExpression, obj, method)) return null; // Perform the transformation to "new Action(obj.func)". obj.Remove(); methodIdent.Remove(); if (!annotation.IsVirtual && obj is ThisReferenceExpression) { // maybe it's getting the pointer of a base method? if (method.DeclaringType != context.CurrentType) { obj = new BaseReferenceExpression(); } } if (!annotation.IsVirtual && obj is NullReferenceExpression && !method.HasThis) { // We're loading a static method. // However it is possible to load extension methods with an instance, so we compare the number of arguments: bool isExtensionMethod = false; TypeReference delegateType = objectCreateExpression.Type.Annotation<TypeReference>(); if (delegateType != null) { TypeDefinition delegateTypeDef = delegateType.Resolve(); if (delegateTypeDef != null) { MethodDefinition invokeMethod = delegateTypeDef.Methods.FirstOrDefault(m => m.Name == "Invoke"); if (invokeMethod != null) { isExtensionMethod = (invokeMethod.Parameters.Count + 1 == method.Parameters.Count); } } } if (!isExtensionMethod) { obj = new TypeReferenceExpression { Type = AstBuilder.ConvertType(method.DeclaringType) }; } } // now transform the identifier into a member reference MemberReferenceExpression mre = new MemberReferenceExpression(); mre.Target = obj; mre.MemberName = methodIdent.Identifier; methodIdent.TypeArguments.MoveTo(mre.TypeArguments); mre.AddAnnotation(method); objectCreateExpression.Arguments.Clear(); objectCreateExpression.Arguments.Add(mre); return null; } } } return base.VisitObjectCreateExpression(objectCreateExpression, data); }
public override IEnumerable<CodeAction> GetActions(RefactoringContext context) { var switchStatement = GetSwitchStatement(context); if (switchStatement == null) { yield break; } var result = context.Resolve(switchStatement.Expression); if (result.Type.Kind != TypeKind.Enum) { yield break; } yield return new CodeAction (context.TranslateString("Create switch labels"), script => { var type = result.Type; var newSwitch = (SwitchStatement)switchStatement.Clone(); var target = new TypeReferenceExpression (context.CreateShortType(result.Type)); foreach (var field in type.GetFields ()) { if (field.IsSynthetic || !field.IsConst) { continue; } newSwitch.SwitchSections.Add(new SwitchSection () { CaseLabels = { new CaseLabel (new MemberReferenceExpression (target.Clone(), field.Name)) }, Statements = { new BreakStatement () } }); } newSwitch.SwitchSections.Add(new SwitchSection () { CaseLabels = { new CaseLabel () }, Statements = { new ThrowStatement (new ObjectCreateExpression (context.CreateShortType("System", "ArgumentOutOfRangeException"))) } }); script.Replace(switchStatement, newSwitch); }, switchStatement); }
public void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { typeReferenceExpression.Type.AcceptVisitor(this); }
public void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { StartNode(typeReferenceExpression); typeReferenceExpression.Type.AcceptVisitor(this); EndNode(typeReferenceExpression); }
Expression ConvertProperty(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) return NotSupported(invocation); Match m = getMethodFromHandlePattern.Match(invocation.Arguments.ElementAt(1)); if (!m.Success) return NotSupported(invocation); MethodReference mr = m.Get<AstNode>("method").Single().Annotation<MethodReference>(); if (mr == null) return null; Expression target = invocation.Arguments.ElementAt(0); Expression convertedTarget; if (target is NullReferenceExpression) { if (m.Has("declaringType")) convertedTarget = new TypeReferenceExpression(m.Get<AstType>("declaringType").Single().Clone()); else convertedTarget = new TypeReferenceExpression(AstBuilder.ConvertType(mr.DeclaringType)); } else { convertedTarget = Convert(target); if (convertedTarget == null) return null; } return convertedTarget.Member(GetPropertyName(mr)).WithAnnotation(mr); }
Expression ConvertCall(InvocationExpression invocation) { if (invocation.Arguments.Count < 2) return NotSupported(invocation); Expression target; int firstArgumentPosition; Match m = getMethodFromHandlePattern.Match(invocation.Arguments.ElementAt(0)); if (m.Success) { target = null; firstArgumentPosition = 1; } else { m = getMethodFromHandlePattern.Match(invocation.Arguments.ElementAt(1)); if (!m.Success) return NotSupported(invocation); target = invocation.Arguments.ElementAt(0); firstArgumentPosition = 2; } MethodReference mr = m.Get<AstNode>("method").Single().Annotation<MethodReference>(); if (mr == null) return null; Expression convertedTarget; if (target == null || target is NullReferenceExpression) { // static method if (m.Has("declaringType")) convertedTarget = new TypeReferenceExpression(m.Get<AstType>("declaringType").Single().Clone()); else convertedTarget = new TypeReferenceExpression(AstBuilder.ConvertType(mr.DeclaringType)); } else { convertedTarget = Convert(target); if (convertedTarget == null) return null; } MemberReferenceExpression mre = convertedTarget.Member(mr.Name); GenericInstanceMethod gim = mr as GenericInstanceMethod; if (gim != null) { foreach (TypeReference tr in gim.GenericArguments) { mre.TypeArguments.Add(AstBuilder.ConvertType(tr)); } } IList<Expression> arguments = null; if (invocation.Arguments.Count == firstArgumentPosition + 1) { Expression argumentArray = invocation.Arguments.ElementAt(firstArgumentPosition); arguments = ConvertExpressionsArray(argumentArray); } if (arguments == null) { arguments = new List<Expression>(); foreach (Expression argument in invocation.Arguments.Skip(firstArgumentPosition)) { Expression convertedArgument = Convert(argument); if (convertedArgument == null) return null; arguments.Add(convertedArgument); } } MethodDefinition methodDef = mr.Resolve(); if (methodDef != null && methodDef.IsGetter) { PropertyDefinition indexer = AstMethodBodyBuilder.GetIndexer(methodDef); if (indexer != null) return new IndexerExpression(mre.Target.Detach(), arguments).WithAnnotation(indexer); } return new InvocationExpression(mre, arguments).WithAnnotation(mr); }
public virtual void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { if (this.ThrowException) { throw (Exception)this.CreateException(typeReferenceExpression); } }
static AstNode TransformCall(bool isVirtual, object operand, MethodDefinition methodDef, List<Ast.Expression> args) { Cecil.MethodReference cecilMethod = ((MethodReference)operand); Ast.Expression target; List<Ast.Expression> methodArgs = new List<Ast.Expression>(args); if (cecilMethod.HasThis) { target = methodArgs[0]; methodArgs.RemoveAt(0); // Unpack any DirectionExpression that is used as target for the call // (calling methods on value types implicitly passes the first argument by reference) if (target is DirectionExpression) { target = ((DirectionExpression)target).Expression; target.Remove(); // detach from DirectionExpression } } else { target = new TypeReferenceExpression { Type = AstBuilder.ConvertType(cecilMethod.DeclaringType) }; } if (target is ThisReferenceExpression && !isVirtual) { // a non-virtual call on "this" might be a "base"-call. if (cecilMethod.DeclaringType != methodDef.DeclaringType) { // If we're not calling a method in the current class; we must be calling one in the base class. target = new BaseReferenceExpression(); } } if (cecilMethod.Name == "Get" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 1) { return target.Indexer(methodArgs); } else if (cecilMethod.Name == "Set" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 2) { return new AssignmentExpression(target.Indexer(methodArgs.GetRange(0, methodArgs.Count - 1)), methodArgs.Last()); } // Resolve the method to figure out whether it is an accessor: Cecil.MethodDefinition cecilMethodDef = cecilMethod.Resolve(); if (cecilMethodDef != null) { if (cecilMethodDef.IsGetter && methodArgs.Count == 0) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.GetMethod == cecilMethodDef) return target.Member(prop.Name).WithAnnotation(prop); } } else if (cecilMethodDef.IsGetter) { // with parameters PropertyDefinition indexer = GetIndexer(cecilMethodDef); if (indexer != null) return target.Indexer(methodArgs).WithAnnotation(indexer); } else if (cecilMethodDef.IsSetter && methodArgs.Count == 1) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.SetMethod == cecilMethodDef) return new Ast.AssignmentExpression(target.Member(prop.Name).WithAnnotation(prop), methodArgs[0]); } } else if (cecilMethodDef.IsSetter && methodArgs.Count > 1) { PropertyDefinition indexer = GetIndexer(cecilMethodDef); if (indexer != null) return new AssignmentExpression( target.Indexer(methodArgs.GetRange(0, methodArgs.Count - 1)).WithAnnotation(indexer), methodArgs[methodArgs.Count - 1] ); } else if (cecilMethodDef.IsAddOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.AddMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Add, Right = methodArgs[0] }; } } } else if (cecilMethodDef.IsRemoveOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.RemoveMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Subtract, Right = methodArgs[0] }; } } } } // If the method has multiple signatures and an null argument is present, then we must cast the null to the correct type. This only needs to be done // when the parameter number being passed a null has different types. if (methodArgs.Exists(arg => arg is NullReferenceExpression)) { var sameNames = cecilMethodDef.DeclaringType.Methods.Where(m => m.Name == cecilMethodDef.Name && m.Parameters.Count == cecilMethodDef.Parameters.Count).ToList(); if (sameNames.Count > 1) { for (int i = cecilMethod.Parameters.Count - 1; 0 <= i; --i) { var arg = methodArgs[i] as NullReferenceExpression; if (arg != null) { if (sameNames.Count != sameNames.Count(m => m.Parameters[i].ParameterType.FullName == cecilMethodDef.Parameters[i].ParameterType.FullName)) methodArgs[i] = arg.CastTo(AstBuilder.ConvertType(cecilMethod.Parameters[i].ParameterType)); } } } } // Default invocation return target.Invoke(cecilMethod.Name, ConvertTypeArguments(cecilMethod), methodArgs).WithAnnotation(cecilMethod); }
public virtual void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { DebugExpression(typeReferenceExpression); StartNode(typeReferenceExpression); typeReferenceExpression.Type.AcceptVisitor(this); EndNode(typeReferenceExpression); }
void MemberAccess( #line 1983 "cs.ATG" out Expression expr, Expression target) { #line 1984 "cs.ATG" List<TypeReference> typeList; #line 1986 "cs.ATG" if (ShouldConvertTargetExpressionToTypeReference(target)) { TypeReference type = GetTypeReferenceFromExpression(target); if (type != null) { target = new TypeReferenceExpression(type) { StartLocation = t.Location, EndLocation = t.EndLocation }; } } Expect(15); Identifier(); #line 1995 "cs.ATG" expr = new MemberReferenceExpression(target, t.val); expr.StartLocation = t.Location; expr.EndLocation = t.EndLocation; if ( #line 1996 "cs.ATG" IsGenericInSimpleNameOrMemberAccess()) { TypeArgumentList( #line 1997 "cs.ATG" out typeList, false); #line 1998 "cs.ATG" ((MemberReferenceExpression)expr).TypeArguments = typeList; } }
void PrimaryExpr( #line 1862 "cs.ATG" out Expression pexpr) { #line 1864 "cs.ATG" TypeReference type = null; Expression expr; pexpr = null; #line 1869 "cs.ATG" Location startLocation = la.Location; if (la.kind == 113) { lexer.NextToken(); #line 1871 "cs.ATG" pexpr = new PrimitiveExpression(true, "true"); } else if (la.kind == 72) { lexer.NextToken(); #line 1872 "cs.ATG" pexpr = new PrimitiveExpression(false, "false"); } else if (la.kind == 90) { lexer.NextToken(); #line 1873 "cs.ATG" pexpr = new PrimitiveExpression(null, "null"); } else if (la.kind == 2) { lexer.NextToken(); #line 1874 "cs.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; } else if ( #line 1875 "cs.ATG" StartOfQueryExpression()) { QueryExpression( #line 1876 "cs.ATG" out pexpr); } else if ( #line 1877 "cs.ATG" IdentAndDoubleColon()) { Identifier(); #line 1878 "cs.ATG" type = new TypeReference(t.val); Expect(10); #line 1879 "cs.ATG" pexpr = new TypeReferenceExpression(type); Identifier(); #line 1880 "cs.ATG" if (type.Type == "global") { type.IsGlobal = true; type.Type = t.val ?? "?"; } else type.Type += "." + (t.val ?? "?"); } else if (StartOf(19)) { Identifier(); #line 1884 "cs.ATG" pexpr = new IdentifierExpression(t.val); if (la.kind == 48 || #line 1887 "cs.ATG" IsGenericInSimpleNameOrMemberAccess()) { if (la.kind == 48) { ShortedLambdaExpression( #line 1886 "cs.ATG" (IdentifierExpression)pexpr, out pexpr); } else { #line 1888 "cs.ATG" List<TypeReference> typeList; TypeArgumentList( #line 1889 "cs.ATG" out typeList, false); #line 1890 "cs.ATG" ((IdentifierExpression)pexpr).TypeArguments = typeList; } } } else if ( #line 1892 "cs.ATG" IsLambdaExpression()) { LambdaExpression( #line 1893 "cs.ATG" out pexpr); } else if (la.kind == 20) { lexer.NextToken(); Expr( #line 1896 "cs.ATG" out expr); Expect(21); #line 1896 "cs.ATG" pexpr = new ParenthesizedExpression(expr); } else if (StartOf(35)) { #line 1899 "cs.ATG" string val = null; switch (la.kind) { case 52: { lexer.NextToken(); #line 1900 "cs.ATG" val = "System.Boolean"; break; } case 54: { lexer.NextToken(); #line 1901 "cs.ATG" val = "System.Byte"; break; } case 57: { lexer.NextToken(); #line 1902 "cs.ATG" val = "System.Char"; break; } case 62: { lexer.NextToken(); #line 1903 "cs.ATG" val = "System.Decimal"; break; } case 66: { lexer.NextToken(); #line 1904 "cs.ATG" val = "System.Double"; break; } case 75: { lexer.NextToken(); #line 1905 "cs.ATG" val = "System.Single"; break; } case 82: { lexer.NextToken(); #line 1906 "cs.ATG" val = "System.Int32"; break; } case 87: { lexer.NextToken(); #line 1907 "cs.ATG" val = "System.Int64"; break; } case 91: { lexer.NextToken(); #line 1908 "cs.ATG" val = "System.Object"; break; } case 102: { lexer.NextToken(); #line 1909 "cs.ATG" val = "System.SByte"; break; } case 104: { lexer.NextToken(); #line 1910 "cs.ATG" val = "System.Int16"; break; } case 108: { lexer.NextToken(); #line 1911 "cs.ATG" val = "System.String"; break; } case 116: { lexer.NextToken(); #line 1912 "cs.ATG" val = "System.UInt32"; break; } case 117: { lexer.NextToken(); #line 1913 "cs.ATG" val = "System.UInt64"; break; } case 120: { lexer.NextToken(); #line 1914 "cs.ATG" val = "System.UInt16"; break; } case 123: { lexer.NextToken(); #line 1915 "cs.ATG" val = "System.Void"; break; } } #line 1917 "cs.ATG" pexpr = new TypeReferenceExpression(new TypeReference(val, true)) { StartLocation = t.Location, EndLocation = t.EndLocation }; } else if (la.kind == 111) { lexer.NextToken(); #line 1920 "cs.ATG" pexpr = new ThisReferenceExpression(); pexpr.StartLocation = t.Location; pexpr.EndLocation = t.EndLocation; } else if (la.kind == 51) { lexer.NextToken(); #line 1922 "cs.ATG" pexpr = new BaseReferenceExpression(); pexpr.StartLocation = t.Location; pexpr.EndLocation = t.EndLocation; } else if (la.kind == 89) { NewExpression( #line 1925 "cs.ATG" out pexpr); } else if (la.kind == 115) { lexer.NextToken(); Expect(20); if ( #line 1929 "cs.ATG" NotVoidPointer()) { Expect(123); #line 1929 "cs.ATG" type = new TypeReference("System.Void", true); } else if (StartOf(10)) { TypeWithRestriction( #line 1930 "cs.ATG" out type, true, true); } else SynErr(208); Expect(21); #line 1932 "cs.ATG" pexpr = new TypeOfExpression(type); } else if (la.kind == 63) { lexer.NextToken(); Expect(20); Type( #line 1934 "cs.ATG" out type); Expect(21); #line 1934 "cs.ATG" pexpr = new DefaultValueExpression(type); } else if (la.kind == 105) { lexer.NextToken(); Expect(20); Type( #line 1935 "cs.ATG" out type); Expect(21); #line 1935 "cs.ATG" pexpr = new SizeOfExpression(type); } else if (la.kind == 58) { lexer.NextToken(); Expect(20); Expr( #line 1936 "cs.ATG" out expr); Expect(21); #line 1936 "cs.ATG" pexpr = new CheckedExpression(expr); } else if (la.kind == 118) { lexer.NextToken(); Expect(20); Expr( #line 1937 "cs.ATG" out expr); Expect(21); #line 1937 "cs.ATG" pexpr = new UncheckedExpression(expr); } else if (la.kind == 64) { lexer.NextToken(); AnonymousMethodExpr( #line 1938 "cs.ATG" out expr); #line 1938 "cs.ATG" pexpr = expr; } else SynErr(209); #line 1940 "cs.ATG" if (pexpr != null) { if (pexpr.StartLocation.IsEmpty) pexpr.StartLocation = startLocation; if (pexpr.EndLocation.IsEmpty) pexpr.EndLocation = t.EndLocation; } while (StartOf(36)) { if (la.kind == 31 || la.kind == 32) { #line 1948 "cs.ATG" startLocation = la.Location; if (la.kind == 31) { lexer.NextToken(); #line 1950 "cs.ATG" pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostIncrement); } else if (la.kind == 32) { lexer.NextToken(); #line 1951 "cs.ATG" pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostDecrement); } else SynErr(210); } else if (la.kind == 47) { PointerMemberAccess( #line 1954 "cs.ATG" out pexpr, pexpr); } else if (la.kind == 15) { MemberAccess( #line 1955 "cs.ATG" out pexpr, pexpr); } else if (la.kind == 20) { lexer.NextToken(); #line 1959 "cs.ATG" List<Expression> parameters = new List<Expression>(); #line 1960 "cs.ATG" pexpr = new InvocationExpression(pexpr, parameters); if (StartOf(26)) { Argument( #line 1961 "cs.ATG" out expr); #line 1961 "cs.ATG" SafeAdd(pexpr, parameters, expr); while (la.kind == 14) { lexer.NextToken(); Argument( #line 1962 "cs.ATG" out expr); #line 1962 "cs.ATG" SafeAdd(pexpr, parameters, expr); } } Expect(21); } else { #line 1968 "cs.ATG" List<Expression> indices = new List<Expression>(); pexpr = new IndexerExpression(pexpr, indices); lexer.NextToken(); Expr( #line 1971 "cs.ATG" out expr); #line 1971 "cs.ATG" SafeAdd(pexpr, indices, expr); while (la.kind == 14) { lexer.NextToken(); Expr( #line 1972 "cs.ATG" out expr); #line 1972 "cs.ATG" SafeAdd(pexpr, indices, expr); } Expect(19); #line 1975 "cs.ATG" if (pexpr != null) { pexpr.StartLocation = startLocation; pexpr.EndLocation = t.EndLocation; } } } }
/// <summary> /// Gets the code for interface attribute. /// </summary> /// <returns> /// The code for the interface attribute. /// </returns> public string CreateCodeForInterfaceAttribute() { var eb = new ElementBuilder(); var attributeSection = eb.AddAttributeSection(null); var attribute = eb.AddAttribute(attributeSection, ContractClassAttributeName); var contractClassTypeReference = new TypeReferenceExpression(this.CreateContractClassName()); eb.AddArgument(attribute, new TypeOfExpression(contractClassTypeReference)); return eb.GenerateCode(this.TextDocument.Language); }
public virtual object VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression, object data) { Debug.Assert((typeReferenceExpression != null)); Debug.Assert((typeReferenceExpression.TypeReference != null)); return typeReferenceExpression.TypeReference.AcceptVisitor(this, data); }
Tuple<CSharpParsedFile, AstNode, CompilationUnit> GetExpressionBeforeCursor() { CompilationUnit baseUnit; if (currentMember == null) { baseUnit = ParseStub ("st {}", false); var type = baseUnit.GetNodeAt<MemberType> (location); if (type == null) { baseUnit = ParseStub ("a;", false); type = baseUnit.GetNodeAt<MemberType> (location); } if (type != null) { // insert target type into compilation unit, to respect the var target = type.Target; target.Remove (); var node = Unit.GetNodeAt (location) ?? Unit; node.AddChild (target, AstNode.Roles.Type); return Tuple.Create (CSharpParsedFile, (AstNode)target, Unit); } } if (currentMember == null && currentType == null) { return null; } baseUnit = ParseStub ("a()"); // Hack for handle object initializer continuation expressions if (baseUnit.GetNodeAt (location) is AttributedNode || baseUnit.GetNodeAt<Expression> (location) == null) { baseUnit = ParseStub ("a()};"); } var memberLocation = currentMember != null ? currentMember.Region.Begin : currentType.Region.Begin; var mref = baseUnit.GetNodeAt<MemberReferenceExpression> (location); Print (baseUnit); if (mref == null) { var invoke = baseUnit.GetNodeAt<InvocationExpression> (location); if (invoke != null) mref = invoke.Target as MemberReferenceExpression; } Expression expr = null; if (mref != null) { expr = mref.Target.Clone (); mref.Parent.ReplaceWith (expr); } else { Expression tref = baseUnit.GetNodeAt<TypeReferenceExpression> (location); var memberType = tref != null ? ((TypeReferenceExpression)tref).Type as MemberType : null; if (memberType == null) { memberType = baseUnit.GetNodeAt<MemberType> (location); if (memberType != null) { tref = baseUnit.GetNodeAt<Expression> (location); if (tref == null) return null; } if (tref is ObjectCreateExpression) { expr = new TypeReferenceExpression (memberType.Target.Clone ()); expr.AddAnnotation (new ObjectCreateExpression ()); } } if (memberType == null) return null; if (expr == null) expr = new TypeReferenceExpression (memberType.Target.Clone ()); tref.ReplaceWith (expr); } var member = Unit.GetNodeAt<AttributedNode> (memberLocation); var member2 = baseUnit.GetNodeAt<AttributedNode> (memberLocation); member2.Remove (); member.ReplaceWith (member2); var tsvisitor = new TypeSystemConvertVisitor (this.CSharpParsedFile.FileName); Unit.AcceptVisitor (tsvisitor, null); return Tuple.Create (tsvisitor.ParsedFile, (AstNode)expr, Unit); }
static AstNode TransformCall(bool isVirtual, object operand, MethodDefinition methodDef, List<Ast.Expression> args) { Cecil.MethodReference cecilMethod = ((MethodReference)operand); Ast.Expression target; List<Ast.Expression> methodArgs = new List<Ast.Expression>(args); if (cecilMethod.HasThis) { target = methodArgs[0]; methodArgs.RemoveAt(0); // Unpack any DirectionExpression that is used as target for the call // (calling methods on value types implicitly passes the first argument by reference) if (target is DirectionExpression) { target = ((DirectionExpression)target).Expression; target.Remove(); // detach from DirectionExpression } } else { target = new TypeReferenceExpression { Type = AstBuilder.ConvertType(cecilMethod.DeclaringType) }; } if (target is ThisReferenceExpression && !isVirtual) { // a non-virtual call on "this" might be a "base"-call. if (cecilMethod.DeclaringType != methodDef.DeclaringType) { // If we're not calling a method in the current class; we must be calling one in the base class. target = new BaseReferenceExpression(); } } // Resolve the method to figure out whether it is an accessor: Cecil.MethodDefinition cecilMethodDef = cecilMethod.Resolve(); if (cecilMethodDef != null) { if (cecilMethodDef.IsGetter && methodArgs.Count == 0) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.GetMethod == cecilMethodDef) return target.Member(prop.Name).WithAnnotation(prop); } } else if (cecilMethodDef.IsSetter && methodArgs.Count == 1) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.SetMethod == cecilMethodDef) return new Ast.AssignmentExpression(target.Member(prop.Name).WithAnnotation(prop), methodArgs[0]); } } else if (cecilMethodDef.IsAddOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.AddMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Add, Right = methodArgs[0] }; } } } else if (cecilMethodDef.IsRemoveOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.RemoveMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Subtract, Right = methodArgs[0] }; } } } } // Default invocation return target.Invoke(cecilMethod.Name, ConvertTypeArguments(cecilMethod), methodArgs).WithAnnotation(cecilMethod); }
private void Format_Type_Reference_Expression(StringBuilder sb, TypeReferenceExpression exp) { sb.Append(FormatterUtility.FormatDataType(exp.TypeReference, document, controller)); }
public override void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { ReplaceType (typeReferenceExpression.Type); }
void SimpleNonInvocationExpression( #line 1645 "VBNET.ATG" out Expression pexpr) { #line 1647 "VBNET.ATG" Expression expr; TypeReference type = null; string name = String.Empty; pexpr = null; if (StartOf(32)) { switch (la.kind) { case 3: { lexer.NextToken(); #line 1655 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 4: { lexer.NextToken(); #line 1656 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 7: { lexer.NextToken(); #line 1657 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 6: { lexer.NextToken(); #line 1658 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 5: { lexer.NextToken(); #line 1659 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 9: { lexer.NextToken(); #line 1660 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 8: { lexer.NextToken(); #line 1661 "VBNET.ATG" pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 202: { lexer.NextToken(); #line 1663 "VBNET.ATG" pexpr = new PrimitiveExpression(true, "true"); break; } case 109: { lexer.NextToken(); #line 1664 "VBNET.ATG" pexpr = new PrimitiveExpression(false, "false"); break; } case 151: { lexer.NextToken(); #line 1665 "VBNET.ATG" pexpr = new PrimitiveExpression(null, "null"); break; } case 25: { lexer.NextToken(); Expr( #line 1666 "VBNET.ATG" out expr); Expect(26); #line 1666 "VBNET.ATG" pexpr = new ParenthesizedExpression(expr); break; } case 2: case 45: case 49: case 51: case 52: case 53: case 54: case 57: case 74: case 85: case 91: case 94: case 103: case 108: case 113: case 120: case 126: case 130: case 133: case 156: case 162: case 169: case 188: case 197: case 198: case 208: case 209: case 215: { Identifier(); #line 1668 "VBNET.ATG" pexpr = new IdentifierExpression(t.val); pexpr.StartLocation = t.Location; pexpr.EndLocation = t.EndLocation; if ( #line 1671 "VBNET.ATG" la.kind == Tokens.OpenParenthesis && Peek(1).kind == Tokens.Of) { lexer.NextToken(); Expect(155); TypeArgumentList( #line 1672 "VBNET.ATG" ((IdentifierExpression)pexpr).TypeArguments); Expect(26); } break; } case 55: case 58: case 69: case 86: case 87: case 96: case 128: case 137: case 154: case 181: case 186: case 187: case 193: case 206: case 207: case 210: { #line 1674 "VBNET.ATG" string val = String.Empty; if (StartOf(11)) { PrimitiveTypeName( #line 1675 "VBNET.ATG" out val); } else if (la.kind == 154) { lexer.NextToken(); #line 1675 "VBNET.ATG" val = "System.Object"; } else SynErr(257); #line 1676 "VBNET.ATG" pexpr = new TypeReferenceExpression(new TypeReference(val, true)); break; } case 139: { lexer.NextToken(); #line 1677 "VBNET.ATG" pexpr = new ThisReferenceExpression(); break; } case 144: case 145: { #line 1678 "VBNET.ATG" Expression retExpr = null; if (la.kind == 144) { lexer.NextToken(); #line 1679 "VBNET.ATG" retExpr = new BaseReferenceExpression(); } else if (la.kind == 145) { lexer.NextToken(); #line 1680 "VBNET.ATG" retExpr = new ClassReferenceExpression(); } else SynErr(258); Expect(16); IdentifierOrKeyword( #line 1682 "VBNET.ATG" out name); #line 1682 "VBNET.ATG" pexpr = new MemberReferenceExpression(retExpr, name); break; } case 117: { lexer.NextToken(); Expect(16); Identifier(); #line 1684 "VBNET.ATG" type = new TypeReference(t.val ?? ""); #line 1686 "VBNET.ATG" type.IsGlobal = true; #line 1687 "VBNET.ATG" pexpr = new TypeReferenceExpression(type); break; } case 148: { ObjectCreateExpression( #line 1688 "VBNET.ATG" out expr); #line 1688 "VBNET.ATG" pexpr = expr; break; } case 81: case 93: case 204: { #line 1690 "VBNET.ATG" CastType castType = CastType.Cast; if (la.kind == 93) { lexer.NextToken(); } else if (la.kind == 81) { lexer.NextToken(); #line 1692 "VBNET.ATG" castType = CastType.Conversion; } else if (la.kind == 204) { lexer.NextToken(); #line 1693 "VBNET.ATG" castType = CastType.TryCast; } else SynErr(259); Expect(25); Expr( #line 1695 "VBNET.ATG" out expr); Expect(12); TypeName( #line 1695 "VBNET.ATG" out type); Expect(26); #line 1696 "VBNET.ATG" pexpr = new CastExpression(type, expr, castType); break; } case 63: case 64: case 65: case 66: case 67: case 68: case 70: case 72: case 73: case 77: case 78: case 79: case 80: case 82: case 83: case 84: { CastTarget( #line 1697 "VBNET.ATG" out type); Expect(25); Expr( #line 1697 "VBNET.ATG" out expr); Expect(26); #line 1697 "VBNET.ATG" pexpr = new CastExpression(type, expr, CastType.PrimitiveConversion); break; } case 44: { lexer.NextToken(); Expr( #line 1698 "VBNET.ATG" out expr); #line 1698 "VBNET.ATG" pexpr = new AddressOfExpression(expr); break; } case 116: { lexer.NextToken(); Expect(25); GetTypeTypeName( #line 1699 "VBNET.ATG" out type); Expect(26); #line 1699 "VBNET.ATG" pexpr = new TypeOfExpression(type); break; } case 205: { lexer.NextToken(); SimpleExpr( #line 1700 "VBNET.ATG" out expr); Expect(131); TypeName( #line 1700 "VBNET.ATG" out type); #line 1700 "VBNET.ATG" pexpr = new TypeOfIsExpression(expr, type); break; } case 122: { ConditionalExpression( #line 1701 "VBNET.ATG" out pexpr); break; } } } else if (la.kind == 16) { lexer.NextToken(); IdentifierOrKeyword( #line 1705 "VBNET.ATG" out name); #line 1705 "VBNET.ATG" pexpr = new MemberReferenceExpression(null, name); } else SynErr(260); }
/// <summary> /// Adds the default return statement. /// </summary> /// <param name="contractElement">The contract element.</param> /// <param name="memberTypeReference">The member type reference.</param> private void AddDefaultReturn(LanguageElement contractElement, TypeReferenceExpression memberTypeReference) { Contract.Requires(contractElement != null, "contractElement is null."); Contract.Requires(memberTypeReference != null, "memberTypeReference is null."); var returnValue = !this.CodeRushProxy.Language.IsCSharp ? this.CodeRushProxy.Language.GetNullReferenceExpression() : new DefaultValueExpression(memberTypeReference); var methodReturn = new Return(returnValue); methodReturn.AddCommentNode(new Comment() { Name = RuntimeIgnored, CommentType = CommentType.SingleLine }); contractElement.AddNode(methodReturn); }
public override void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { new TypeBlock(this, typeReferenceExpression.Type).Emit(); }
AstNode TransformCall(bool isVirtual, ILExpression byteCode, List<Ast.Expression> args) { Cecil.MethodReference cecilMethod = (MethodReference)byteCode.Operand; Cecil.MethodDefinition cecilMethodDef = cecilMethod.Resolve(); Ast.Expression target; List<Ast.Expression> methodArgs = new List<Ast.Expression>(args); if (cecilMethod.HasThis) { target = methodArgs[0]; methodArgs.RemoveAt(0); // Unpack any DirectionExpression that is used as target for the call // (calling methods on value types implicitly passes the first argument by reference) if (target is DirectionExpression) { target = ((DirectionExpression)target).Expression; target.Remove(); // detach from DirectionExpression } if (cecilMethodDef != null && cecilMethodDef.DeclaringType.IsInterface) { TypeReference tr = byteCode.Arguments[0].InferredType; if (tr != null) { TypeDefinition td = tr.Resolve(); if (td != null && !td.IsInterface) { // Calling an interface method on a non-interface object: // we need to introduce an explicit cast target = target.CastTo(AstBuilder.ConvertType(cecilMethod.DeclaringType)); } } } } else { target = new TypeReferenceExpression { Type = AstBuilder.ConvertType(cecilMethod.DeclaringType) }; } if (target is ThisReferenceExpression && !isVirtual) { // a non-virtual call on "this" might be a "base"-call. if (cecilMethod.DeclaringType.GetElementType() != methodDef.DeclaringType) { // If we're not calling a method in the current class; we must be calling one in the base class. target = new BaseReferenceExpression(); } } if (cecilMethod.Name == ".ctor" && cecilMethod.DeclaringType.IsValueType) { // On value types, the constructor can be called. // This is equivalent to 'target = new ValueType(args);'. ObjectCreateExpression oce = new ObjectCreateExpression(); oce.Type = AstBuilder.ConvertType(cecilMethod.DeclaringType); AdjustArgumentsForMethodCall(cecilMethod, methodArgs); oce.Arguments.AddRange(methodArgs); return new AssignmentExpression(target, oce); } if (cecilMethod.Name == "Get" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 1) { return target.Indexer(methodArgs); } else if (cecilMethod.Name == "Set" && cecilMethod.DeclaringType is ArrayType && methodArgs.Count > 2) { return new AssignmentExpression(target.Indexer(methodArgs.GetRange(0, methodArgs.Count - 1)), methodArgs.Last()); } // Test whether the method is an accessor: if (cecilMethodDef != null) { if (cecilMethodDef.IsGetter && methodArgs.Count == 0) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.GetMethod == cecilMethodDef) return target.Member(prop.Name).WithAnnotation(prop); } } else if (cecilMethodDef.IsGetter) { // with parameters PropertyDefinition indexer = GetIndexer(cecilMethodDef); if (indexer != null) return target.Indexer(methodArgs).WithAnnotation(indexer); } else if (cecilMethodDef.IsSetter && methodArgs.Count == 1) { foreach (var prop in cecilMethodDef.DeclaringType.Properties) { if (prop.SetMethod == cecilMethodDef) return new Ast.AssignmentExpression(target.Member(prop.Name).WithAnnotation(prop), methodArgs[0]); } } else if (cecilMethodDef.IsSetter && methodArgs.Count > 1) { PropertyDefinition indexer = GetIndexer(cecilMethodDef); if (indexer != null) return new AssignmentExpression( target.Indexer(methodArgs.GetRange(0, methodArgs.Count - 1)).WithAnnotation(indexer), methodArgs[methodArgs.Count - 1] ); } else if (cecilMethodDef.IsAddOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.AddMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Add, Right = methodArgs[0] }; } } } else if (cecilMethodDef.IsRemoveOn && methodArgs.Count == 1) { foreach (var ev in cecilMethodDef.DeclaringType.Events) { if (ev.RemoveMethod == cecilMethodDef) { return new Ast.AssignmentExpression { Left = target.Member(ev.Name).WithAnnotation(ev), Operator = AssignmentOperatorType.Subtract, Right = methodArgs[0] }; } } } else if (cecilMethodDef.Name == "Invoke" && cecilMethodDef.DeclaringType.BaseType != null && cecilMethodDef.DeclaringType.BaseType.FullName == "System.MulticastDelegate") { AdjustArgumentsForMethodCall(cecilMethod, methodArgs); return target.Invoke(methodArgs); } } // Default invocation AdjustArgumentsForMethodCall(cecilMethodDef ?? cecilMethod, methodArgs); return target.Invoke(cecilMethod.Name, ConvertTypeArguments(cecilMethod), methodArgs).WithAnnotation(cecilMethod); }
} // GetClassBaseType /// <summary> /// Nav easy goto check availability /// </summary> private void navEasyGoto_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea) { LanguageElement element = ea.Element; if (element == null) return; _currentClass = element.GetClass(); if (_currentClass != null) { ea.Available = true; ea.AddSubMenuItem(MenuItem_ClassDefinition, "Class definition"); _currentBaseClassRef = GetClassBaseType(_currentClass); if (_currentBaseClassRef != null) { ea.AddSubMenuItem(MenuItem_ClassDefinition_InheritedClass, "Base class"); ea.AddSubMenuItem(MenuItem_ClassDefinition_InheritedClass_Definition, "Base class definition"); } } // if } // navEasyGoto_CheckAvailability
Expression ConvertField(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) return NotSupported(invocation); Expression fieldInfoExpr = invocation.Arguments.ElementAt(1); Match m = getFieldFromHandlePattern.Match(fieldInfoExpr); if (!m.Success) return NotSupported(invocation); FieldReference fr = m.Get<AstNode>("field").Single().Annotation<FieldReference>(); if (fr == null) return null; Expression target = invocation.Arguments.ElementAt(0); Expression convertedTarget; if (target is NullReferenceExpression) { if (m.Has("declaringType")) convertedTarget = new TypeReferenceExpression(m.Get<AstType>("declaringType").Single().Clone()); else convertedTarget = new TypeReferenceExpression(AstBuilder.ConvertType(fr.DeclaringType)); } else { convertedTarget = Convert(target); if (convertedTarget == null) return null; } return convertedTarget.Member(fr.Name).WithAnnotation(fr); }
public override object Visit(TypeReferenceExpression typeReferenceExpression, object data) { return new ReturnType(typeReferenceExpression.TypeReference); }
public virtual void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression) { VisitChildren (typeReferenceExpression); }
public StringBuilder VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression, int data) { throw new SLSharpException("SL# primitives do not methods."); }
ExpressionResult GetExpressionBeforeCursor() { SyntaxTree baseUnit; if (currentMember == null) { baseUnit = ParseStub("a", false); var type = baseUnit.GetNodeAt<MemberType>(location); if (type == null) { baseUnit = ParseStub("a;", false); type = baseUnit.GetNodeAt<MemberType>(location); } if (type == null) { baseUnit = ParseStub("A a;", false); type = baseUnit.GetNodeAt<MemberType>(location); } if (type != null) { return new ExpressionResult((AstNode)type.Target, baseUnit); } } baseUnit = ParseStub("ToString()", false); var curNode = baseUnit.GetNodeAt(location); // hack for local variable declaration missing ';' issue - remove that if it works. if (curNode is EntityDeclaration || baseUnit.GetNodeAt<Expression>(location) == null && baseUnit.GetNodeAt<MemberType>(location) == null) { baseUnit = ParseStub("a"); curNode = baseUnit.GetNodeAt(location); } // Hack for handle object initializer continuation expressions if (curNode is EntityDeclaration || baseUnit.GetNodeAt<Expression>(location) == null && baseUnit.GetNodeAt<MemberType>(location) == null) { baseUnit = ParseStub("a};"); } var mref = baseUnit.GetNodeAt<MemberReferenceExpression>(location); if (currentMember == null && currentType == null) { if (mref != null) { return new ExpressionResult((AstNode)mref.Target, baseUnit); } return null; } //var memberLocation = currentMember != null ? currentMember.Region.Begin : currentType.Region.Begin; if (mref == null) { var type = baseUnit.GetNodeAt<MemberType>(location); if (type != null) { return new ExpressionResult((AstNode)type.Target, baseUnit); } var pref = baseUnit.GetNodeAt<PointerReferenceExpression>(location); if (pref != null) { return new ExpressionResult((AstNode)pref.Target, baseUnit); } } if (mref == null) { baseUnit = ParseStub("A a;", false); var type = baseUnit.GetNodeAt<MemberType>(location); if (type != null) { return new ExpressionResult((AstNode)type.Target, baseUnit); } } AstNode expr = null; if (mref != null) { expr = mref.Target; } else { Expression tref = baseUnit.GetNodeAt<TypeReferenceExpression>(location); MemberType memberType = tref != null ? ((TypeReferenceExpression)tref).Type as MemberType : null; if (memberType == null) { memberType = baseUnit.GetNodeAt<MemberType>(location); if (memberType != null) { if (memberType.Parent is ObjectCreateExpression) { var mt = memberType.Target.Clone(); memberType.ReplaceWith(mt); expr = mt; goto exit; } else { tref = baseUnit.GetNodeAt<Expression>(location); if (tref == null) { tref = new TypeReferenceExpression(memberType.Clone()); memberType.Parent.AddChild(tref, Roles.Expression); } if (tref is ObjectCreateExpression) { expr = memberType.Target.Clone(); expr.AddAnnotation(new ObjectCreateExpression()); } } } } if (memberType == null) { return null; } if (expr == null) { expr = memberType.Target.Clone(); } tref.ReplaceWith(expr); } exit: return new ExpressionResult((AstNode)expr, baseUnit); }
public void VisitTypeReferenceExpression(TypeReferenceExpression node) { NotSupported(node); }