codegenerator.cs

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

CS
1,490
字号
					{						ContinueOnNewLine(", ");					}					else					{						first = false;					}					if(prefix != null)					{						Output.Write(prefix);					}					Output.Write(attr.Name);					args = attr.Arguments;					if(args.Count != 0)					{						Output.Write("(");						first2 = true;						foreach(CodeAttributeArgument arg in args)						{							if(!first2)							{								Output.Write(", ");							}							else							{								first2 = false;							}							OutputAttributeArgument(arg);						}						Output.Write(")");					}				}				GenerateAttributeDeclarationsEnd(attributes);			}	// Generate a direction expression.	protected virtual void GenerateDirectionExpression				(CodeDirectionExpression e)			{				OutputDirection(e.Direction);				GenerateExpression(e.Expression);			}	// Output a field direction value.	protected virtual void OutputDirection(FieldDirection dir)			{				if(dir == FieldDirection.Out)				{					Output.Write("out ");				}				else if(dir == FieldDirection.Ref)				{					Output.Write("ref ");				}			}	// Output an expression list.	protected virtual void OutputExpressionList				(CodeExpressionCollection expressions)			{				OutputExpressionList(expressions, false);			}	protected virtual void OutputExpressionList				(CodeExpressionCollection expressions,				 bool newlineBetweenItems)			{				writer.Indent += 1;				bool first = true;				foreach(CodeExpression expr in expressions)				{					if(!first)					{						if(newlineBetweenItems)						{							ContinueOnNewLine(",");						}						else						{							Output.Write(", ");						}					}					else					{						first = false;					}					((ICodeGenerator)this).GenerateCodeFromExpression						(expr, writer.InnerWriter, Options);				}				writer.Indent -= 1;			}	// Output a field scope modifier.	protected virtual void OutputFieldScopeModifier				(MemberAttributes attributes)			{				if((attributes & MemberAttributes.VTableMask) ==						MemberAttributes.New)				{					Output.Write("new ");				}				if((attributes & MemberAttributes.ScopeMask) ==						MemberAttributes.Static)				{					Output.Write("static ");				}				else if((attributes & MemberAttributes.ScopeMask) ==							MemberAttributes.Const)				{					Output.Write("const ");				}			}	// Output an identifier.	protected virtual void OutputIdentifier(String ident)			{				Output.Write(ident);			}	// Output a member access modifier.	protected virtual void OutputMemberAccessModifier				(MemberAttributes attributes)			{				String access;				switch(attributes & MemberAttributes.AccessMask)				{					case MemberAttributes.Assembly:						access = "internal "; break;					case MemberAttributes.FamilyAndAssembly:						access = "/*FamANDAssem*/ internal "; break;					case MemberAttributes.Family:						access = "protected "; break;					case MemberAttributes.FamilyOrAssembly:						access = "protected internal "; break;					case MemberAttributes.Private:						access = "private "; break;					case MemberAttributes.Public:						access = "public "; break;					default: return;				}				Output.Write(access);			}	// Output a member scope modifier.	protected virtual void OutputMemberScopeModifier				(MemberAttributes attributes)			{				if((attributes & MemberAttributes.VTableMask) ==						MemberAttributes.New)				{					Output.Write("new ");				}				switch(attributes & MemberAttributes.ScopeMask)				{					case MemberAttributes.Abstract:						Output.Write("abstract "); break;;					case MemberAttributes.Final:						break;;					case MemberAttributes.Static:						Output.Write("static "); break;;					case MemberAttributes.Override:						Output.Write("override "); break;;					default:					{						if((attributes & MemberAttributes.AccessMask) ==								MemberAttributes.Public ||						   (attributes & MemberAttributes.AccessMask) ==								MemberAttributes.Family)						{							Output.Write("virtual ");						}					}					break;				}			}	// Output a binary operator.	protected virtual void OutputOperator(CodeBinaryOperatorType op)			{				String oper;				switch(op)				{					case CodeBinaryOperatorType.Add:						oper = "+"; break;					case CodeBinaryOperatorType.Subtract:						oper = "-"; break;					case CodeBinaryOperatorType.Multiply:						oper = "*"; break;					case CodeBinaryOperatorType.Divide:						oper = "/"; break;					case CodeBinaryOperatorType.Modulus:						oper = "%"; break;					case CodeBinaryOperatorType.Assign:						oper = "="; break;					case CodeBinaryOperatorType.IdentityInequality:						oper = "!="; break;					case CodeBinaryOperatorType.IdentityEquality:						oper = "=="; break;					case CodeBinaryOperatorType.ValueEquality:						oper = "=="; break;					case CodeBinaryOperatorType.BitwiseOr:						oper = "|"; break;					case CodeBinaryOperatorType.BitwiseAnd:						oper = "&"; break;					case CodeBinaryOperatorType.BooleanOr:						oper = "&&"; break;					case CodeBinaryOperatorType.BooleanAnd:						oper = "||"; break;					case CodeBinaryOperatorType.LessThan:						oper = "<"; break;					case CodeBinaryOperatorType.LessThanOrEqual:						oper = "<="; break;					case CodeBinaryOperatorType.GreaterThan:						oper = ">"; break;					case CodeBinaryOperatorType.GreaterThanOrEqual:						oper = ">="; break;					default: return;				}				Output.Write(oper);			}	// Output a list of parameters.	protected virtual void OutputParameters				(CodeParameterDeclarationExpressionCollection parameters)			{				bool splitLines = (parameters.Count >= 16);				bool first = true;				if(splitLines)				{					writer.Indent += 1;				}				foreach(CodeParameterDeclarationExpression expr in parameters)				{					if(!first)					{						Output.Write(", ");						if(splitLines)						{							ContinueOnNewLine("");						}					}					else					{						first = false;					}					GenerateParameterDeclarationExpression(expr);				}				if(splitLines)				{					writer.Indent -= 1;				}			}	// Output a type.	protected abstract void OutputType(CodeTypeReference typeRef);	// Output the attributes for a type.	protected virtual void OutputTypeAttributes				(TypeAttributes attributes, bool isStruct, bool isEnum)			{				switch(attributes & TypeAttributes.VisibilityMask)				{					case TypeAttributes.NestedPrivate:						Output.Write("private "); break;					case TypeAttributes.Public:					case TypeAttributes.NestedPublic:						Output.Write("public "); break;				}				if(isStruct)				{					Output.Write("struct ");				}				else if(isEnum)				{					Output.Write("enum ");				}				else				{					if((attributes & TypeAttributes.ClassSemanticsMask) ==							TypeAttributes.Interface)					{						Output.Write("interface ");					}					else					{						if((attributes & TypeAttributes.Sealed) != 0)						{							Output.Write("sealed ");						}						if((attributes & TypeAttributes.Abstract) != 0)						{							Output.Write("abstract ");						}						Output.Write("class ");					}				}			}	// Output a type name pair.	protected virtual void OutputTypeNamePair				(CodeTypeReference typeRef, String name)			{				OutputType(typeRef);				Output.Write(" ");				OutputIdentifier(name);			}	// Quote a snippet string.	protected abstract String QuoteSnippetString(String value);	// Determine if this code generator supports a particular	// set of generator options.	protected abstract bool Supports(GeneratorSupport supports);	// Validate an identifier and throw an exception if not valid.	protected virtual void ValidateIdentifier(String value)			{				if(!IsValidIdentifier(value))				{					throw new ArgumentException(S._("Arg_InvalidIdentifier"));				}			}	// Validate all identifiers in a CodeDom tree.	public static void ValidateIdentifiers(CodeObject e)			{				Validator.Validate(e);			}	// Create an escaped identifier if "value" is a language keyword.	String ICodeGenerator.CreateEscapedIdentifier(String value)			{				return CreateEscapedIdentifier(value);			}	// Create a valid identifier if "value" is a language keyword.	String ICodeGenerator.CreateValidIdentifier(String value)			{				return CreateValidIdentifier(value);			}	// Set up this object for code generation.	private bool SetupForCodeGeneration(TextWriter w, CodeGeneratorOptions o)			{				if(writer != null)				{					if(writer.InnerWriter != w)					{						throw new InvalidOperationException							(S._("Invalid_CGWriterExists"));					}					return false;				}				else				{					if(o != null)					{						options = o;					}					else					{						options = new CodeGeneratorOptions();					}					writer = new IndentedTextWriter(w, options.IndentString);					return true;				}			}	// Finalize code generation.	private void FinalizeCodeGeneration(bool initialized)			{				if(initialized)				{					writer = null;					options = null;				}			}	// Generate code from a CodeDom compile unit.	void ICodeGenerator.GenerateCodeFromCompileUnit(CodeCompileUnit e,									                TextWriter w,									                CodeGeneratorOptions o)			{				bool initialized = SetupForCodeGeneration(w, o);				try				{					if(e is CodeSnippetCompileUnit)					{						GenerateSnippetCompileUnit((CodeSnippetCompileUnit)e);					}					else					{						GenerateCompileUnit(e);					}				}				finally				{					FinalizeCodeGeneration(initialized);				}			}	// Generate code from a CodeDom expression.	void ICodeGenerator.GenerateCodeFromExpression(CodeExpression e,												   TextWriter w,												   CodeGeneratorOptions o)			{				bool initialized = SetupForCodeGeneration(w, o);				try				{					GenerateExpression(e);				}				finally				{					FinalizeCodeGeneration(initialized);				}			}	// Generate code from a CodeDom namespace.	void ICodeGenerator.GenerateCodeFromNamespace(CodeNamespace e,								   				  TextWriter w,								   				  CodeGeneratorOptions o)			{				bool initialized = SetupForCodeGeneration(w, o);				try				{					GenerateNamespace(e);				}				finally				{					FinalizeCodeGeneration(initialized);				}			}	// Generate code from a CodeDom statement.	void ICodeGenerator.GenerateCodeFromStatement(CodeStatement e,								   				  TextWriter w,								   				  CodeGeneratorOptions o)			{				bool initialized = SetupForCodeGeneration(w, o);				try				{					GenerateStatement(e);				}				finally				{					FinalizeCodeGeneration(initialized);				}			}	// Generate code from a CodeDom type declaration.	void ICodeGenerator.GenerateCodeFromType(CodeTypeDeclaration e,							  				 TextWriter w,							  				 CodeGeneratorOptions o)			{				bool initialized = SetupForCodeGeneration(w, o);				try				{					GenerateType(e);				}				finally				{					FinalizeCodeGeneration(initialized);				}			}	// Get the type indicated by a CodeDom type reference.	String ICodeGenerator.GetTypeOutput(CodeTypeReference type)			{				return GetTypeOutput(type);			}	// Determine if "value" is a valid identifier.	bool ICodeGenerator.IsValidIdentifier(String value)			{				return IsValidIdentifier(value);			}	// Determine if this code generator supports a particular	// set of generator options.	bool ICodeGenerator.Supports(GeneratorSupport supports)			{				return Supports(supports);			}	// Validate an identifier and throw an exception if not valid.	void ICodeGenerator.ValidateIdentifier(String value)			{				ValidateIdentifier(value);			}}; // class CodeGenerator#endif // CONFIG_CODEDOM}; // namespace System.CodeDom.Compiler

⌨️ 快捷键说明

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