codegenerator.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,490 行 · 第 1/3 页

CS
1,490
字号
/* * CodeGenerator.cs - Implementation of the *		System.CodeDom.Compiler.CodeGenerator class. * * Copyright (C) 2002, 2003  Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */namespace System.CodeDom.Compiler{#if CONFIG_CODEDOMusing System.IO;using System.Reflection;using System.Globalization;public abstract class CodeGenerator : ICodeGenerator{	// Internal state.	private CodeTypeMember currentMember;	private CodeTypeDeclaration currentType;	private IndentedTextWriter writer;	private CodeGeneratorOptions options;	// Constructor.	protected CodeGenerator()			{			}	// Determine if an identifier is valid for language-independent use.	public static bool IsValidLanguageIndependentIdentifier(String value)			{				if(value == null || value.Length == 0)				{					return false;				}				int posn = 0;				if(Char.GetUnicodeCategory(value[0]) ==						UnicodeCategory.DecimalDigitNumber)				{					return false;				}				for(posn = 0; posn < value.Length; ++posn)				{					switch(Char.GetUnicodeCategory(value[posn]))					{						case UnicodeCategory.UppercaseLetter:						case UnicodeCategory.LowercaseLetter:						case UnicodeCategory.TitlecaseLetter:						case UnicodeCategory.ModifierLetter:						case UnicodeCategory.OtherLetter:						case UnicodeCategory.NonSpacingMark:						case UnicodeCategory.SpacingCombiningMark:						case UnicodeCategory.DecimalDigitNumber:						case UnicodeCategory.ConnectorPunctuation: break;						default: return false;					}				}				return true;			}	// Get the current class member.	protected CodeTypeMember CurrentMember			{				get				{					return currentMember;				}			}	// Get the current class member's name.	protected String CurrentMemberName			{				get				{					if(currentMember != null)					{						return currentMember.Name;					}					else					{						return "<% unknown %>";					}				}			}	// Get the current class type's name.	protected String CurrentTypeName			{				get				{					if(currentType != null)					{						return currentType.Name;					}					else					{						return "<% unknown %>";					}				}			}	// Get or set the current indent level.	protected int Indent			{				get				{					return writer.Indent;				}				set				{					writer.Indent = value;				}			}	// Determine if the current type is a class.	protected bool IsCurrentClass			{				get				{					return (currentType != null &&							!(currentType is CodeTypeDelegate) &&							currentType.IsClass);				}			}	// Determine if the current type is a delegate.	protected bool IsCurrentDelegate			{				get				{					return (currentType != null &&							currentType is CodeTypeDelegate);				}			}	// Determine if the current type is an enumeration.	protected bool IsCurrentEnum			{				get				{					return (currentType != null &&							!(currentType is CodeTypeDelegate) &&							currentType.IsEnum);				}			}	// Determine if the current type is an interface.	protected bool IsCurrentInterface			{				get				{					return (currentType != null &&							!(currentType is CodeTypeDelegate) &&							currentType.IsInterface);				}			}	// Determine if the current type is a struct.	protected bool IsCurrentStruct			{				get				{					return (currentType != null &&							!(currentType is CodeTypeDelegate) &&							currentType.IsStruct);				}			}	// Get the token for "null".	protected abstract String NullToken { get; }	// Get the current code generation options.	protected CodeGeneratorOptions Options			{				get				{					if(options == null)					{						options = new CodeGeneratorOptions();					}					return options;				}			}	// Get the output text writer.	protected TextWriter Output			{				get				{					return writer;				}			}	// Output a continuation on a new line if the language requires it.	protected virtual void ContinueOnNewLine(String st)			{				writer.WriteLine(st);			}	// Create an escaped identifier if "value" is a language keyword.	protected abstract String CreateEscapedIdentifier(String value);	// Create a valid identifier if "value" is a language keyword.	protected abstract String CreateValidIdentifier(String value);	// Generate various expression categories.	protected abstract void GenerateArgumentReferenceExpression				(CodeArgumentReferenceExpression e);	protected abstract void GenerateArrayCreateExpression				(CodeArrayCreateExpression e);	protected abstract void GenerateArrayIndexerExpression				(CodeArrayIndexerExpression e);	protected abstract void GenerateBaseReferenceExpression				(CodeBaseReferenceExpression e);	protected abstract void GenerateCastExpression				(CodeCastExpression e);	protected abstract void GenerateDelegateCreateExpression				(CodeDelegateCreateExpression e);	protected abstract void GenerateDelegateInvokeExpression				(CodeDelegateInvokeExpression e);	protected abstract void GenerateEventReferenceExpression				(CodeEventReferenceExpression e);	protected abstract void GenerateFieldReferenceExpression				(CodeFieldReferenceExpression e);	protected abstract void GenerateIndexerExpression				(CodeIndexerExpression e);	protected abstract void GenerateMethodInvokeExpression				(CodeMethodInvokeExpression e);	protected abstract void GenerateMethodReferenceExpression				(CodeMethodReferenceExpression e);	protected abstract void GenerateObjectCreateExpression				(CodeObjectCreateExpression e);	protected abstract void GeneratePropertyReferenceExpression				(CodePropertyReferenceExpression e);	protected abstract void GeneratePropertySetValueReferenceExpression				(CodePropertySetValueReferenceExpression e);	protected abstract void GenerateSnippetExpression				(CodeSnippetExpression e);	protected abstract void GenerateThisReferenceExpression				(CodeThisReferenceExpression e);	protected abstract void GenerateVariableReferenceExpression				(CodeVariableReferenceExpression e);	// Generate various statement categories.	protected abstract void GenerateAssignStatement				(CodeAssignStatement e);	protected abstract void GenerateAttachEventStatement				(CodeAttachEventStatement e);	protected abstract void GenerateConditionStatement				(CodeConditionStatement e);	protected abstract void GenerateExpressionStatement				(CodeExpressionStatement e);	protected abstract void GenerateGotoStatement				(CodeGotoStatement e);	protected abstract void GenerateIterationStatement				(CodeIterationStatement e);	protected abstract void GenerateLabeledStatement				(CodeLabeledStatement e);	protected abstract void GenerateMethodReturnStatement				(CodeMethodReturnStatement e);	protected abstract void GenerateRemoveEventStatement				(CodeRemoveEventStatement e);	protected abstract void GenerateThrowExceptionStatement				(CodeThrowExceptionStatement e);	protected abstract void GenerateTryCatchFinallyStatement				(CodeTryCatchFinallyStatement e);	protected abstract void GenerateVariableDeclarationStatement				(CodeVariableDeclarationStatement e);	// Generate various declaration categories.	protected abstract void GenerateAttributeDeclarationsStart				(CodeAttributeDeclarationCollection attributes);	protected abstract void GenerateAttributeDeclarationsEnd				(CodeAttributeDeclarationCollection attributes);	protected abstract void GenerateConstructor				(CodeConstructor e, CodeTypeDeclaration c);	protected abstract void GenerateEntryPointMethod				(CodeEntryPointMethod e, CodeTypeDeclaration c);	protected abstract void GenerateEvent				(CodeMemberEvent e, CodeTypeDeclaration c);	protected abstract void GenerateField(CodeMemberField e);	protected abstract void GenerateMethod				(CodeMemberMethod e, CodeTypeDeclaration c);	protected abstract void GenerateProperty				(CodeMemberProperty e, CodeTypeDeclaration c);	protected abstract void GenerateNamespaceStart(CodeNamespace e);	protected abstract void GenerateNamespaceEnd(CodeNamespace e);	protected abstract void GenerateNamespaceImport(CodeNamespaceImport e);	protected abstract void GenerateSnippetMember				(CodeSnippetTypeMember e);	protected abstract void GenerateTypeConstructor				(CodeTypeConstructor e);	protected abstract void GenerateTypeStart(CodeTypeDeclaration e);	protected abstract void GenerateTypeEnd(CodeTypeDeclaration e);	// Generate various misc categories.	protected abstract void GenerateComment(CodeComment e);	protected abstract void GenerateLinePragmaStart(CodeLinePragma e);	protected abstract void GenerateLinePragmaEnd(CodeLinePragma e);	protected abstract String GetTypeOutput(CodeTypeReference value);	// Generate code for a binary operator expression.	protected virtual void GenerateBinaryOperatorExpression				(CodeBinaryOperatorExpression e)			{				Output.Write("(");				GenerateExpression(e.Left);				Output.Write(" ");				OutputOperator(e.Operator);				Output.Write(" ");				GenerateExpression(e.Right);				Output.Write(")");			}	// Generate code for comment statements.	protected virtual void GenerateCommentStatement				(CodeCommentStatement e)			{				GenerateComment(e.Comment);			}	protected virtual void GenerateCommentStatements				(CodeCommentStatementCollection e)			{				foreach(CodeCommentStatement comment in e)				{					GenerateCommentStatement(comment);				}			}	// Generate code for a compilation unit.	protected virtual void GenerateCompileUnit(CodeCompileUnit e)			{				GenerateCompileUnitStart(e);				GenerateNamespaces(e);				GenerateCompileUnitEnd(e);			}	protected virtual void GenerateCompileUnitStart(CodeCompileUnit e)			{				// Nothing to do here.			}	protected virtual void GenerateCompileUnitEnd(CodeCompileUnit e)			{				// Nothing to do here.			}	// Generate code for constants.	protected virtual void GenerateDecimalValue(Decimal d)			{				writer.Write(d.ToString(CultureInfo.InvariantCulture));			}	protected virtual void GenerateDoubleValue(double d)			{				writer.Write(d.ToString("R", CultureInfo.InvariantCulture));			}	protected virtual void GenerateSingleFloatValue(float s)			{				writer.Write(s.ToString(CultureInfo.InvariantCulture));			}	// Generate code for an expression.	protected void GenerateExpression(CodeExpression e)			{				if(e is CodeArrayCreateExpression)				{					GenerateArrayCreateExpression						((CodeArrayCreateExpression)e);				}				else if(e is CodeCastExpression)				{					GenerateCastExpression((CodeCastExpression)e);				}				else if(e is CodeMethodInvokeExpression)				{					GenerateMethodInvokeExpression						((CodeMethodInvokeExpression)e);				}				else if(e is CodeObjectCreateExpression)				{					GenerateObjectCreateExpression						((CodeObjectCreateExpression)e);				}				else if(e is CodeParameterDeclarationExpression)				{					GenerateParameterDeclarationExpression						((CodeParameterDeclarationExpression)e);				}				else if(e is CodeTypeOfExpression)				{					GenerateTypeOfExpression						((CodeTypeOfExpression)e);				}				else if(e is CodeTypeReferenceExpression)				{					GenerateTypeReferenceExpression						((CodeTypeReferenceExpression)e);				}				else if(e is CodeArgumentReferenceExpression)				{					GenerateArgumentReferenceExpression						((CodeArgumentReferenceExpression)e);				}				else if(e is CodeArrayIndexerExpression)				{					GenerateArrayIndexerExpression						((CodeArrayIndexerExpression)e);				}				else if(e is CodeBaseReferenceExpression)				{					GenerateBaseReferenceExpression						((CodeBaseReferenceExpression)e);				}				else if(e is CodeBinaryOperatorExpression)				{					GenerateBinaryOperatorExpression						((CodeBinaryOperatorExpression)e);				}				else if(e is CodeDelegateCreateExpression)				{					GenerateDelegateCreateExpression						((CodeDelegateCreateExpression)e);				}				else if(e is CodeDelegateInvokeExpression)				{					GenerateDelegateInvokeExpression						((CodeDelegateInvokeExpression)e);				}				else if(e is CodeDirectionExpression)				{					GenerateDirectionExpression						((CodeDirectionExpression)e);				}				else if(e is CodeEventReferenceExpression)				{					GenerateEventReferenceExpression						((CodeEventReferenceExpression)e);				}				else if(e is CodeFieldReferenceExpression)				{					GenerateFieldReferenceExpression						((CodeFieldReferenceExpression)e);				}				else if(e is CodeIndexerExpression)				{					GenerateIndexerExpression						((CodeIndexerExpression)e);				}				else if(e is CodeMethodReferenceExpression)				{					GenerateMethodReferenceExpression						((CodeMethodReferenceExpression)e);				}				else if(e is CodePrimitiveExpression)				{					GeneratePrimitiveExpression						((CodePrimitiveExpression)e);				}				else if(e is CodePropertyReferenceExpression)				{					GeneratePropertyReferenceExpression						((CodePropertyReferenceExpression)e);				}				else if(e is CodePropertySetValueReferenceExpression)				{					GeneratePropertySetValueReferenceExpression						((CodePropertySetValueReferenceExpression)e);				}				else if(e is CodeSnippetExpression)				{					GenerateSnippetExpression						((CodeSnippetExpression)e);				}				else if(e is CodeThisReferenceExpression)				{					GenerateThisReferenceExpression						((CodeThisReferenceExpression)e);				}				else if(e is CodeVariableReferenceExpression)				{					GenerateVariableReferenceExpression						((CodeVariableReferenceExpression)e);				}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?