コード例 #1
0
		public override void Complete(CompletionContext context)
		{
			if (declarationBegin > context.StartOffset) {
				base.Complete(context);
				return;
			}
			
			TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
			b.ShowTypeParameterConstraints = false;
			b.GenerateBody = true;
			
			var entityDeclaration = b.ConvertEntity(this.Entity);
			entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
			entityDeclaration.Modifiers |= Modifiers.Override;
			
			if (!this.Entity.IsAbstract) {
				// modify body to call the base method
				if (this.Entity.SymbolKind == SymbolKind.Method) {
					var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
					var body = entityDeclaration.GetChildByRole(Roles.Body);
					body.Statements.Clear();
					if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
						body.Statements.Add(new ExpressionStatement(baseCall));
					else
						body.Statements.Add(new ReturnStatement(baseCall));
				} else if (this.Entity.SymbolKind == SymbolKind.Indexer || this.Entity.SymbolKind == SymbolKind.Property) {
					Expression baseCall;
					if (this.Entity.SymbolKind == SymbolKind.Indexer)
						baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
					else
						baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
					var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
					if (!getterBody.IsNull) {
						getterBody.Statements.Clear();
						getterBody.Add(new ReturnStatement(baseCall.Clone()));
					}
					var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
					if (!setterBody.IsNull) {
						setterBody.Statements.Clear();
						setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
					}
				}
			}
			
			var document = context.Editor.Document;
			StringWriter w = new StringWriter();
			var formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
			var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, context.Editor.Options);
			
			using (document.OpenUndoGroup()) {
				string newText = w.ToString().TrimEnd();
				document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
				var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
				if (throwStatement != null) {
					var segment = segmentDict[throwStatement];
					context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
				}
				CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions);
			}
		}
コード例 #2
0
		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);
		}
コード例 #3
0
 public override StringBuilder VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, int data)
 {
     return null;
 }
コード例 #4
0
ファイル: Parser.cs プロジェクト: mgagne-atman/Projects
	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;
				}
				
			}
		}
	}
コード例 #5
0
ファイル: AstCsToJson.cs プロジェクト: CompilerKit/CodeWalk
 public void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     JsonObject expression = CreateJsonExpression(baseReferenceExpression);
     Push(expression);
 }
コード例 #6
0
ファイル: CSharpOutputVisitor.cs プロジェクト: x-strong/ILSpy
		public void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
		{
			StartNode(baseReferenceExpression);
			WriteKeyword("base", baseReferenceExpression.Role);
			EndNode(baseReferenceExpression);
		}
コード例 #7
0
ファイル: AstMethodBodyBuilder.cs プロジェクト: petr-k/ILSpy
        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);
        }
コード例 #8
0
 public override object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data)
 {
     return(new CodeBaseReferenceExpression());
 }
 public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     HandleExpressionNode(baseReferenceExpression);
 }
コード例 #10
0
 public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     UsesNonStaticMember = true;
     base.VisitBaseReferenceExpression(baseReferenceExpression);
 }
コード例 #11
0
 public virtual void VisitBaseReferenceExpression(BaseReferenceExpression node)
 {
 }
コード例 #12
0
 public object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data)
 {
     return(new B.SuperLiteralExpression(GetLexicalInfo(baseReferenceExpression)));
 }
コード例 #13
0
            void AnalyzeThisReferenceExpression(BaseReferenceExpression baseReferenceExpression)
            {
                var memberReference = baseReferenceExpression.Parent as MemberReferenceExpression;

                if (memberReference == null)
                {
                    return;
                }

                var state       = ctx.GetResolverStateAfter(baseReferenceExpression);
                var wholeResult = ctx.Resolve(memberReference);

                IMember member = GetMember(wholeResult);

                if (member == null)
                {
                    return;
                }

                var localDeclarationSpace = declarationsSpaceVisitor.GetDeclarationSpace(baseReferenceExpression);

                if (localDeclarationSpace == null || localDeclarationSpace.IsNameUsed(member.Name))
                {
                    return;
                }

                var result       = state.LookupSimpleNameOrTypeName(memberReference.MemberName, EmptyList <IType> .Instance, NameLookupMode.Expression);
                var parentResult = ctx.Resolve(memberReference.Parent) as AlInvocationResolveResult;

                bool isRedundant;

                if (result is MemberResolveResult)
                {
                    isRedundant = ((MemberResolveResult)result).Member.Region.Equals(member.Region);
                }
                else if (parentResult != null && parentResult.IsExtensionMethodInvocation)
                {
                    // 'this.' is required for extension method invocation
                    isRedundant = false;
                }
                else if (result is MethodGroupResolveResult)
                {
                    isRedundant = ((MethodGroupResolveResult)result).Methods.Any(m => m.Region.Equals(member.Region));
                }
                else
                {
                    return;
                }

                var basicMembers    = state.CurrentTypeDefinition.DirectBaseTypes.First().GetMembers();
                var extendedMembers = state.CurrentTypeDefinition.GetMembers().Except(basicMembers);

                if (extendedMembers.Any(f => f.Name.Equals(member.Name)))
                {
                    return;
                }

                if (isRedundant)
                {
                    AddIssue(new CodeIssue(
                                 baseReferenceExpression.StartLocation,
                                 memberReference.MemberNameToken.StartLocation,
                                 ctx.TranslateString("'base.' is redundant and can be removed safely."),
                                 ctx.TranslateString("Remove 'base.'"),
                                 script => {
                        script.Replace(memberReference, RefactoringAstHelper.RemoveTarget(memberReference));
                    }
                                 )
                    {
                        IssueMarker = IssueMarker.GrayOut
                    });
                }
            }
コード例 #14
0
 public abstract StringBuilder VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, int data);
コード例 #15
0
 public abstract StringBuilder VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, int data);
コード例 #16
0
		public sealed override object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data) {
			BeginVisit(baseReferenceExpression);
			object result = TrackedVisitBaseReferenceExpression(baseReferenceExpression, data);
			EndVisit(baseReferenceExpression);
			return result;
		}
コード例 #17
0
 public void VisitBaseReferenceExpression(BaseReferenceExpression expression)
 {
     Formatter.StartNode(expression);
     Formatter.WriteKeyword("base");
     Formatter.EndNode();
 }
コード例 #18
0
		public virtual object TrackedVisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data) {
			return base.VisitBaseReferenceExpression(baseReferenceExpression, data);
		}
コード例 #19
0
		public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
		{
			UsesNonStaticMember = true;
			base.VisitBaseReferenceExpression(baseReferenceExpression);
		}
コード例 #20
0
 public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
 }
		public virtual void VisitBaseReferenceExpression (BaseReferenceExpression baseReferenceExpression)
		{
			VisitChildren (baseReferenceExpression);
		}
コード例 #22
0
 public virtual Node VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     throw new System.NotImplementedException();
 }
コード例 #23
0
		public override object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data)
		{
			return new CodeBaseReferenceExpression();
		}
コード例 #24
0
 public Node VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     throw new NotImplementedException();
 }
コード例 #25
0
		public virtual object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data) {
			Debug.Assert((baseReferenceExpression != null));
			return null;
		}
コード例 #26
0
 public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     new BaseReferenceBlock(this, baseReferenceExpression).Emit();
 }
コード例 #27
0
        public override void Complete(ICSharpCode.AvalonEdit.Editing.TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
        {
            if (declarationBegin > completionSegment.Offset)
            {
                base.Complete(textArea, completionSegment, insertionRequestEventArgs);
                return;
            }
            TypeSystemAstBuilder b = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret));
            b.ShowTypeParameterConstraints = false;
            b.GenerateBody = true;

            var entityDeclaration = b.ConvertEntity(this.Entity);
            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.EntityType == EntityType.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
                    var body = entityDeclaration.GetChildByRole(Roles.Body);
                    body.Statements.Clear();
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                        body.Statements.Add(new ExpressionStatement(baseCall));
                    else
                        body.Statements.Add(new ReturnStatement(baseCall));
                }
                else if (this.Entity.EntityType == EntityType.Indexer || this.Entity.EntityType == EntityType.Property)
                {
                    Expression baseCall;
                    if (this.Entity.EntityType == EntityType.Indexer)
                        baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
                    else
                        baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
                    var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
                    if (!getterBody.IsNull)
                    {
                        getterBody.Statements.Clear();
                        getterBody.Add(new ReturnStatement(baseCall.Clone()));
                    }
                    var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
                    if (!setterBody.IsNull)
                    {
                        setterBody.Statements.Clear();
                        setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
                    }
                }
            }

            var document = textArea.Document;
            StringWriter w = new StringWriter();
            var formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
            var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, textArea.Options);

            string newText = w.ToString().TrimEnd();
            document.Replace(declarationBegin, completionSegment.EndOffset - declarationBegin, newText);
            var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
            if (throwStatement != null)
            {
                var segment = segmentDict[throwStatement];
                textArea.Selection = new RectangleSelection(textArea, new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset)), new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset + segment.Length)));
            }

            //format the inserted code nicely
            var formatter = new CSharpFormatter(formattingOptions);
            formatter.AddFormattingRegion(new DomRegion(document.GetLocation(declarationBegin), document.GetLocation(declarationBegin + newText.Length)));
            var syntaxTree = new CSharpParser().Parse(document);
            formatter.AnalyzeFormatting(document, syntaxTree).ApplyChanges();
        }
コード例 #28
0
 public virtual object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data)
 {
     throw new global::System.NotImplementedException("BaseReferenceExpression");
 }
コード例 #29
0
ファイル: Parser.cs プロジェクト: mgagne-atman/Projects
	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);
	}
コード例 #30
0
ファイル: CSharpWriter.cs プロジェクト: xliuGeonx/solid
 public override void VisitBaseReferenceExpression(BaseReferenceExpression node)
 {
     WriteKeyword("base");
 }
コード例 #31
0
 public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     new BaseReferenceBlock(this, baseReferenceExpression).Emit();
 }
コード例 #32
0
ファイル: Parser.cs プロジェクト: KnowNo/test-code-backup
        private void PrimaryExpr(out Expression pexpr)
        {
            TypeReference typeReference = null;
            List<TypeReference> types = null;
            Expression expression;
            bool flag = false;
            pexpr = null;
            if (this.la.kind == 0x70)
            {
                base.lexer.NextToken();
                pexpr = new PrimitiveExpression(true, "true");
            }
            else if (this.la.kind == 0x47)
            {
                base.lexer.NextToken();
                pexpr = new PrimitiveExpression(false, "false");
            }
            else if (this.la.kind == 0x59)
            {
                base.lexer.NextToken();
                pexpr = new PrimitiveExpression(null, "null");
            }
            else if (this.la.kind == 2)
            {
                base.lexer.NextToken();
                pexpr = new PrimitiveExpression(this.t.literalValue, this.t.val);
            }
            else if ((this.la.kind == 1) && (this.Peek(1).kind == 10))
            {
                base.Expect(1);
                typeReference = new TypeReference(this.t.val);
                base.Expect(10);
                pexpr = new TypeReferenceExpression(typeReference);
                base.Expect(1);
                if (typeReference.Type == "global")
                {
                    typeReference.IsGlobal = true;
                    typeReference.Type = this.t.val ?? "?";
                }
                else
                {
                    typeReference.Type = typeReference.Type + "." + (this.t.val ?? "?");
                }
            }
            else if (this.la.kind == 1)
            {
                base.lexer.NextToken();
                pexpr = new IdentifierExpression(this.t.val);
            }
            else if (this.la.kind == 20)
            {
                base.lexer.NextToken();
                this.Expr(out expression);
                base.Expect(0x15);
                pexpr = new ParenthesizedExpression(expression);
            }
            else if (!this.StartOf(0x1a))
            {
                if (this.la.kind == 110)
                {
                    base.lexer.NextToken();
                    pexpr = new ThisReferenceExpression();
                }
                else if (this.la.kind == 50)
                {
                    base.lexer.NextToken();
                    Expression targetObject = new BaseReferenceExpression();
                    if (this.la.kind == 15)
                    {
                        base.lexer.NextToken();
                        base.Expect(1);
                        targetObject = new FieldReferenceExpression(targetObject, this.t.val);
                    }
                    else if (this.la.kind == 0x12)
                    {
                        base.lexer.NextToken();
                        this.Expr(out expression);
                        List<Expression> indices = new List<Expression>();
                        if (expression != null)
                        {
                            indices.Add(expression);
                        }
                        while (this.la.kind == 14)
                        {
                            base.lexer.NextToken();
                            this.Expr(out expression);
                            if (expression != null)
                            {
                                indices.Add(expression);
                            }
                        }
                        base.Expect(0x13);
                        targetObject = new IndexerExpression(targetObject, indices);
                    }
                    else
                    {
                        base.SynErr(0xb3);
                    }
                    pexpr = targetObject;
                }
                else if (this.la.kind == 0x58)
                {
                    base.lexer.NextToken();
                    this.NonArrayType(out typeReference);
                    List<Expression> parameters = new List<Expression>();
                    if (this.la.kind == 20)
                    {
                        base.lexer.NextToken();
                        ObjectCreateExpression expression3 = new ObjectCreateExpression(typeReference, parameters);
                        if (this.StartOf(0x15))
                        {
                            this.Argument(out expression);
                            if (expression != null)
                            {
                                parameters.Add(expression);
                            }
                            while (this.la.kind == 14)
                            {
                                base.lexer.NextToken();
                                this.Argument(out expression);
                                if (expression != null)
                                {
                                    parameters.Add(expression);
                                }
                            }
                        }
                        base.Expect(0x15);
                        pexpr = expression3;
                    }
                    else if (this.la.kind == 0x12)
                    {
                        base.lexer.NextToken();
                        flag = true;
                        ArrayCreateExpression expression4 = new ArrayCreateExpression(typeReference);
                        pexpr = expression4;
                        int item = 0;
                        List<int> list4 = new List<int>();
                        if ((this.la.kind == 14) || (this.la.kind == 0x13))
                        {
                            while (this.la.kind == 14)
                            {
                                base.lexer.NextToken();
                                item++;
                            }
                            base.Expect(0x13);
                            list4.Add(item);
                            item = 0;
                            while (this.la.kind == 0x12)
                            {
                                base.lexer.NextToken();
                                while (this.la.kind == 14)
                                {
                                    base.lexer.NextToken();
                                    item++;
                                }
                                base.Expect(0x13);
                                list4.Add(item);
                                item = 0;
                            }
                            expression4.CreateType.RankSpecifier = list4.ToArray();
                            this.ArrayInitializer(out expression);
                            expression4.ArrayInitializer = (ArrayInitializerExpression) expression;
                        }
                        else if (this.StartOf(5))
                        {
                            this.Expr(out expression);
                            if (expression != null)
                            {
                                parameters.Add(expression);
                            }
                            while (this.la.kind == 14)
                            {
                                base.lexer.NextToken();
                                item++;
                                this.Expr(out expression);
                                if (expression != null)
                                {
                                    parameters.Add(expression);
                                }
                            }
                            base.Expect(0x13);
                            list4.Add(item);
                            expression4.Arguments = parameters;
                            for (item = 0; this.la.kind == 0x12; item = 0)
                            {
                                base.lexer.NextToken();
                                while (this.la.kind == 14)
                                {
                                    base.lexer.NextToken();
                                    item++;
                                }
                                base.Expect(0x13);
                                list4.Add(item);
                            }
                            expression4.CreateType.RankSpecifier = list4.ToArray();
                            if (this.la.kind == 0x10)
                            {
                                this.ArrayInitializer(out expression);
                                expression4.ArrayInitializer = (ArrayInitializerExpression) expression;
                            }
                        }
                        else
                        {
                            base.SynErr(180);
                        }
                    }
                    else
                    {
                        base.SynErr(0xb5);
                    }
                }
                else if (this.la.kind == 0x72)
                {
                    base.lexer.NextToken();
                    base.Expect(20);
                    if (this.NotVoidPointer())
                    {
                        base.Expect(0x7a);
                        typeReference = new TypeReference("void");
                    }
                    else if (this.StartOf(9))
                    {
                        this.TypeWithRestriction(out typeReference, true, true);
                    }
                    else
                    {
                        base.SynErr(0xb6);
                    }
                    base.Expect(0x15);
                    pexpr = new TypeOfExpression(typeReference);
                }
                else if ((this.la.kind == 0x3e) && (this.Peek(1).kind == 20))
                {
                    base.Expect(0x3e);
                    base.Expect(20);
                    this.Type(out typeReference);
                    base.Expect(0x15);
                    pexpr = new DefaultValueExpression(typeReference);
                }
                else if (this.la.kind == 0x68)
                {
                    base.lexer.NextToken();
                    base.Expect(20);
                    this.Type(out typeReference);
                    base.Expect(0x15);
                    pexpr = new SizeOfExpression(typeReference);
                }
                else if (this.la.kind == 0x39)
                {
                    base.lexer.NextToken();
                    base.Expect(20);
                    this.Expr(out expression);
                    base.Expect(0x15);
                    pexpr = new CheckedExpression(expression);
                }
                else if (this.la.kind == 0x75)
                {
                    base.lexer.NextToken();
                    base.Expect(20);
                    this.Expr(out expression);
                    base.Expect(0x15);
                    pexpr = new UncheckedExpression(expression);
                }
                else if (this.la.kind == 0x3f)
                {
                    base.lexer.NextToken();
                    this.AnonymousMethodExpr(out expression);
                    pexpr = expression;
                }
                else
                {
                    base.SynErr(0xb7);
                }
            }
            else
            {
                string typeName = null;
                switch (this.la.kind)
                {
                    case 0x3d:
                        base.lexer.NextToken();
                        typeName = "decimal";
                        break;

                    case 0x41:
                        base.lexer.NextToken();
                        typeName = "double";
                        break;

                    case 0x4a:
                        base.lexer.NextToken();
                        typeName = "float";
                        break;

                    case 0x33:
                        base.lexer.NextToken();
                        typeName = "bool";
                        break;

                    case 0x35:
                        base.lexer.NextToken();
                        typeName = "byte";
                        break;

                    case 0x38:
                        base.lexer.NextToken();
                        typeName = "char";
                        break;

                    case 0x51:
                        base.lexer.NextToken();
                        typeName = "int";
                        break;

                    case 0x56:
                        base.lexer.NextToken();
                        typeName = "long";
                        break;

                    case 90:
                        base.lexer.NextToken();
                        typeName = "object";
                        break;

                    case 0x65:
                        base.lexer.NextToken();
                        typeName = "sbyte";
                        break;

                    case 0x67:
                        base.lexer.NextToken();
                        typeName = "short";
                        break;

                    case 0x6b:
                        base.lexer.NextToken();
                        typeName = "string";
                        break;

                    case 0x73:
                        base.lexer.NextToken();
                        typeName = "uint";
                        break;

                    case 0x74:
                        base.lexer.NextToken();
                        typeName = "ulong";
                        break;

                    case 0x77:
                        base.lexer.NextToken();
                        typeName = "ushort";
                        break;
                }
                this.t.val = "";
                base.Expect(15);
                base.Expect(1);
                pexpr = new FieldReferenceExpression(new TypeReferenceExpression(typeName), this.t.val);
            }
            while ((this.StartOf(0x1b) || (this.IsGenericFollowedBy(15) && this.IsTypeReferenceExpression(pexpr))) || this.IsGenericFollowedBy(20))
            {
                if ((this.la.kind == 0x1f) || (this.la.kind == 0x20))
                {
                    if (this.la.kind == 0x1f)
                    {
                        base.lexer.NextToken();
                        pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostIncrement);
                    }
                    else if (this.la.kind == 0x20)
                    {
                        base.lexer.NextToken();
                        pexpr = new UnaryOperatorExpression(pexpr, UnaryOperatorType.PostDecrement);
                    }
                    else
                    {
                        base.SynErr(0xb8);
                    }
                }
                else
                {
                    if (this.la.kind == 0x2f)
                    {
                        base.lexer.NextToken();
                        base.Expect(1);
                        pexpr = new PointerReferenceExpression(pexpr, this.t.val);
                        continue;
                    }
                    if (this.la.kind == 15)
                    {
                        base.lexer.NextToken();
                        base.Expect(1);
                        pexpr = new FieldReferenceExpression(pexpr, this.t.val);
                        continue;
                    }
                    if (this.IsGenericFollowedBy(15) && this.IsTypeReferenceExpression(pexpr))
                    {
                        this.TypeArgumentList(out types, false);
                        base.Expect(15);
                        base.Expect(1);
                        pexpr = new FieldReferenceExpression(this.GetTypeReferenceExpression(pexpr, types), this.t.val);
                        continue;
                    }
                    if (this.la.kind == 20)
                    {
                        base.lexer.NextToken();
                        List<Expression> arguments = new List<Expression>();
                        if (this.StartOf(0x15))
                        {
                            this.Argument(out expression);
                            if (expression != null)
                            {
                                arguments.Add(expression);
                            }
                            while (this.la.kind == 14)
                            {
                                base.lexer.NextToken();
                                this.Argument(out expression);
                                if (expression != null)
                                {
                                    arguments.Add(expression);
                                }
                            }
                        }
                        base.Expect(0x15);
                        pexpr = new InvocationExpression(pexpr, arguments);
                        continue;
                    }
                    if (this.IsGenericFollowedBy(20))
                    {
                        this.TypeArgumentList(out types, false);
                        base.Expect(20);
                        List<Expression> list6 = new List<Expression>();
                        if (this.StartOf(0x15))
                        {
                            this.Argument(out expression);
                            if (expression != null)
                            {
                                list6.Add(expression);
                            }
                            while (this.la.kind == 14)
                            {
                                base.lexer.NextToken();
                                this.Argument(out expression);
                                if (expression != null)
                                {
                                    list6.Add(expression);
                                }
                            }
                        }
                        base.Expect(0x15);
                        pexpr = new InvocationExpression(pexpr, list6, types);
                        continue;
                    }
                    if (flag)
                    {
                        this.Error("element access not allow on array creation");
                    }
                    List<Expression> list7 = new List<Expression>();
                    base.lexer.NextToken();
                    this.Expr(out expression);
                    if (expression != null)
                    {
                        list7.Add(expression);
                    }
                    while (this.la.kind == 14)
                    {
                        base.lexer.NextToken();
                        this.Expr(out expression);
                        if (expression != null)
                        {
                            list7.Add(expression);
                        }
                    }
                    base.Expect(0x13);
                    pexpr = new IndexerExpression(pexpr, list7);
                }
            }
        }
コード例 #33
0
 public override object Visit(BaseReferenceExpression baseReferenceExpression, object data)
 {
     //			Console.WriteLine("Visiting base");
     if (resolver.CallingClass == null) {
         return null;
     }
     IClass baseClass = resolver.BaseClass(resolver.CallingClass);
     if (baseClass == null) {
     //				Console.WriteLine("Base Class not found");
         return null;
     }
     //			Console.WriteLine("Base Class: " + baseClass.FullyQualifiedName);
     return new ReturnType(baseClass.FullyQualifiedName);
 }
コード例 #34
0
        protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
        {
            BaseReferenceExpression o = other as BaseReferenceExpression;

            return(o != null);
        }
コード例 #35
0
 public BaseReferenceBlock(IEmitter emitter, BaseReferenceExpression baseReferenceExpression)
     : base(emitter, baseReferenceExpression)
 {
     this.Emitter = emitter;
     this.BaseReferenceExpression = baseReferenceExpression;
 }
コード例 #36
0
 public RedILNode VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, State data)
 {
     throw new System.NotImplementedException();
 }
コード例 #37
0
        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);
        }
コード例 #38
0
 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.GetElementType() != 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));
 }
コード例 #39
0
 public virtual void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
 {
     if (this.ThrowException)
     {
         throw (Exception)this.CreateException(baseReferenceExpression);
     }
 }
コード例 #40
0
        public override void Complete(CompletionContext context)
        {
            if (declarationBegin > context.StartOffset)
            {
                base.Complete(context);
                return;
            }

            TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);

            b.ShowTypeParameterConstraints = false;
            b.GenerateBody = true;

            var entityDeclaration = b.ConvertEntity(this.Entity);

            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            var       body = entityDeclaration.GetChildByRole(Roles.Body);
            Statement baseCallStatement = body.Children.OfType <Statement>().FirstOrDefault();

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.SymbolKind == SymbolKind.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, new Expression[] { });
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                    {
                        baseCallStatement = new ExpressionStatement(baseCall);
                    }
                    else
                    {
                        baseCallStatement = new ReturnStatement(baseCall);
                    }

                    // Clear body of inserted method
                    entityDeclaration.GetChildByRole(Roles.Body).Statements.Clear();
                }
            }

            var          document          = context.Editor.Document;
            StringWriter w                 = new StringWriter();
            var          formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(contextAtCaret.Compilation.GetProject());
            var          segmentDict       = SegmentTrackingOutputFormatter.WriteNode(
                w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options);

            using (document.OpenUndoGroup()) {
                InsertionContext insertionContext = new InsertionContext(context.Editor.GetService(typeof(TextArea)) as TextArea, declarationBegin);
                insertionContext.InsertionPosition = context.Editor.Caret.Offset;

                string newText = w.ToString().TrimEnd();
                document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
                var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
                if (throwStatement != null)
                {
                    var segment = segmentDict[throwStatement];
                    context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
                }
                CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer);

                var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None);
                var typeResolveContext = refactoringContext.GetTypeResolveContext();
                if (typeResolveContext == null)
                {
                    return;
                }
                var resolvedCurrent = typeResolveContext.CurrentTypeDefinition;
                var entities        = FindFieldsAndProperties(resolvedCurrent).ToList();
                if (entities.Any())
                {
                    IEditorUIService uiService = context.Editor.GetService(typeof(IEditorUIService)) as IEditorUIService;

                    ITextAnchor endAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
                    endAnchor.MovementType = AnchorMovementType.AfterInsertion;

                    ITextAnchor startAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
                    startAnchor.MovementType = AnchorMovementType.BeforeInsertion;

                    ITextAnchor insertionPos = context.Editor.Document.CreateAnchor(endAnchor.Offset);
                    insertionPos.MovementType = AnchorMovementType.BeforeInsertion;

                    AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, context.Editor, insertionPos, entities, baseCallStatement);
                    dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);

                    insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
                }
                else
                {
                    if (baseCallStatement != null)
                    {
                        // Add default base call
                        MethodDeclaration insertedOverrideMethod = refactoringContext.GetNode().PrevSibling as MethodDeclaration;
                        if (insertedOverrideMethod == null)
                        {
                            // We are not inside of a method declaration
                            return;
                        }
                        using (Script script = refactoringContext.StartScript()) {
                            script.AddTo(insertedOverrideMethod.Body, baseCallStatement);
                        }
                    }
                }

                insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }
コード例 #41
0
		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);
		}
コード例 #42
0
		public virtual object VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data) {
			throw new global::System.NotImplementedException("BaseReferenceExpression");
		}
コード例 #43
0
ファイル: CSharpParser.cs プロジェクト: pgoron/monodevelop
			public override object Visit (BaseThis baseAccessExpression)
			{
				var result = new BaseReferenceExpression ();
				result.Location = Convert (baseAccessExpression.Location);
				return result;
			}
コード例 #44
0
ファイル: Lower.cs プロジェクト: evanw/minisharp
 public void VisitBaseReferenceExpression(BaseReferenceExpression node)
 {
     VisitChildren(node);
 }
コード例 #45
0
		public override void Complete(CompletionContext context)
		{
			if (declarationBegin > context.StartOffset) {
				base.Complete(context);
				return;
			}
			
			TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
			b.ShowTypeParameterConstraints = false;
			b.GenerateBody = true;
			
			var entityDeclaration = b.ConvertEntity(this.Entity);
			entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
			entityDeclaration.Modifiers |= Modifiers.Override;
			
			var body = entityDeclaration.GetChildByRole(Roles.Body);
			Statement baseCallStatement = body.Children.OfType<Statement>().FirstOrDefault();
			
			if (!this.Entity.IsAbstract) {
				// modify body to call the base method
				if (this.Entity.SymbolKind == SymbolKind.Method) {
					var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, new Expression[] { });
					if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
						baseCallStatement = new ExpressionStatement(baseCall);
					else
						baseCallStatement = new ReturnStatement(baseCall);
					
					// Clear body of inserted method
					entityDeclaration.GetChildByRole(Roles.Body).Statements.Clear();
				}
			}
			
			var document = context.Editor.Document;
			StringWriter w = new StringWriter();
			var formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
			var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, context.Editor.Options);
			
			using (document.OpenUndoGroup()) {
				InsertionContext insertionContext = new InsertionContext(context.Editor.GetService(typeof(TextArea)) as TextArea, declarationBegin);
				insertionContext.InsertionPosition = context.Editor.Caret.Offset;
				
				string newText = w.ToString().TrimEnd();
				document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
				var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
				if (throwStatement != null) {
					var segment = segmentDict[throwStatement];
					context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
				}
				CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions);
				
				var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None);
				var typeResolveContext = refactoringContext.GetTypeResolveContext();
				if (typeResolveContext == null) {
					return;
				}
				var resolvedCurrent = typeResolveContext.CurrentTypeDefinition;
				var entities = FindFieldsAndProperties(resolvedCurrent).ToList();
				if (entities.Any()) {
					IEditorUIService uiService = context.Editor.GetService(typeof(IEditorUIService)) as IEditorUIService;
					
					ITextAnchor endAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
					endAnchor.MovementType = AnchorMovementType.AfterInsertion;
					
					ITextAnchor startAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
					startAnchor.MovementType = AnchorMovementType.BeforeInsertion;
					
					ITextAnchor insertionPos = context.Editor.Document.CreateAnchor(endAnchor.Offset);
					insertionPos.MovementType = AnchorMovementType.BeforeInsertion;

					AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, context.Editor, insertionPos, entities, baseCallStatement);
					dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);
					
					insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
				} else {
					if (baseCallStatement != null) {
						// Add default base call
						MethodDeclaration insertedOverrideMethod = refactoringContext.GetNode().PrevSibling as MethodDeclaration;
						if (insertedOverrideMethod == null)
						{
							// We are not inside of a method declaration
							return;
						}
						using (Script script = refactoringContext.StartScript()) {
							script.AddTo(insertedOverrideMethod.Body, baseCallStatement);
						}
					}
				}
				
				insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
			}
		}
コード例 #46
0
        public override void Complete(ICSharpCode.AvalonEdit.Editing.TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
        {
            if (declarationBegin > completionSegment.Offset)
            {
                base.Complete(textArea, completionSegment, insertionRequestEventArgs);
                return;
            }
            TypeSystemAstBuilder b = new TypeSystemAstBuilder(new CSharpResolver(contextAtCaret));

            b.ShowTypeParameterConstraints = false;
            b.GenerateBody = true;

            var entityDeclaration = b.ConvertEntity(this.Entity);

            entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
            entityDeclaration.Modifiers |= Modifiers.Override;

            if (!this.Entity.IsAbstract)
            {
                // modify body to call the base method
                if (this.Entity.EntityType == EntityType.Method)
                {
                    var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
                    var body     = entityDeclaration.GetChildByRole(Roles.Body);
                    body.Statements.Clear();
                    if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
                    {
                        body.Statements.Add(new ExpressionStatement(baseCall));
                    }
                    else
                    {
                        body.Statements.Add(new ReturnStatement(baseCall));
                    }
                }
                else if (this.Entity.EntityType == EntityType.Indexer || this.Entity.EntityType == EntityType.Property)
                {
                    Expression baseCall;
                    if (this.Entity.EntityType == EntityType.Indexer)
                    {
                        baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
                    }
                    else
                    {
                        baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
                    }
                    var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
                    if (!getterBody.IsNull)
                    {
                        getterBody.Statements.Clear();
                        getterBody.Add(new ReturnStatement(baseCall.Clone()));
                    }
                    var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
                    if (!setterBody.IsNull)
                    {
                        setterBody.Statements.Clear();
                        setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
                    }
                }
            }

            var          document          = textArea.Document;
            StringWriter w                 = new StringWriter();
            var          formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
            var          segmentDict       = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, textArea.Options);

            string newText = w.ToString().TrimEnd();

            document.Replace(declarationBegin, completionSegment.EndOffset - declarationBegin, newText);
            var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);

            if (throwStatement != null)
            {
                var segment = segmentDict[throwStatement];
                textArea.Selection = new RectangleSelection(textArea, new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset)), new TextViewPosition(textArea.Document.GetLocation(declarationBegin + segment.Offset + segment.Length)));
            }

            //format the inserted code nicely
            var formatter = new CSharpFormatter(formattingOptions);

            formatter.AddFormattingRegion(new DomRegion(document.GetLocation(declarationBegin), document.GetLocation(declarationBegin + newText.Length)));
            var syntaxTree = new CSharpParser().Parse(document);

            formatter.AnalyzeFormatting(document, syntaxTree).ApplyChanges();
        }