📄 cppcodegenerator.cs
字号:
//
// -*- C# -*-
//
// Author: Roman Taranchenko
// Copyright: (c) 2004 Roman Taranchenko
// Copying Policy: GNU General Public License
//
// Some code was borrowed from mono class library ;-)
//
// This implementation of System.CodeDom.Compiler.ICodeGenerator
// has limited functionality. It designed only for use in
// the SharpDevelop's class wizard.
//
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.IO;
using System.Reflection;
namespace NewClassWizard
{
public class CppCodeGenerator: CodeGenerator
{
static CppCodeGenerator()
{
keywordsTable = new Hashtable();
foreach (string keyword in keywords)
{
keywordsTable.Add (keyword,keyword);
}
typesTable = new Hashtable();
foreach (string type in types)
{
typesTable.Add (type,type);
}
}
protected override string NullToken
{
get
{
return "NULL";
}
}
protected override void GenerateArrayCreateExpression(CodeArrayCreateExpression expression)
{
}
protected override void GenerateBaseReferenceExpression(CodeBaseReferenceExpression expression)
{
}
protected override void GenerateCastExpression(CodeCastExpression expression)
{
Output.Write("(__try_cast<");
OutputType(expression.TargetType);
Output.Write(">(");
GenerateExpression(expression.Expression);
Output.Write("))");
}
protected override void GenerateCompileUnitStart(CodeCompileUnit compileUnit)
{
GenerateComment(new CodeComment("------------------------------------------------------------------------------"));
GenerateComment(new CodeComment(" <autogenerated>"));
GenerateComment(new CodeComment(" This code was generated by a tool."));
GenerateComment(new CodeComment(" SharpDevelop Version: " + Assembly.GetEntryAssembly().GetName().Version));
Output.WriteLine();
GenerateComment(new CodeComment(" Changes to this file may cause incorrect behavior and will be lost if "));
GenerateComment(new CodeComment(" the code is regenerated."));
GenerateComment(new CodeComment(" </autogenerated>"));
GenerateComment(new CodeComment("------------------------------------------------------------------------------"));
Output.WriteLine();
Output.WriteLine("#pragma once");
Output.WriteLine();
Output.WriteLine("#using <mscorlib.dll>");
Output.WriteLine();
}
protected override void GenerateDelegateCreateExpression(CodeDelegateCreateExpression expression)
{
}
protected override void GenerateFieldReferenceExpression(CodeFieldReferenceExpression expression)
{
}
protected override void GenerateArgumentReferenceExpression(CodeArgumentReferenceExpression expression)
{
}
protected override void GenerateVariableReferenceExpression(CodeVariableReferenceExpression expression)
{
}
protected override void GenerateIndexerExpression(CodeIndexerExpression expression)
{
}
protected override void GenerateArrayIndexerExpression(CodeArrayIndexerExpression expression)
{
}
protected override void GenerateSnippetExpression(CodeSnippetExpression expression)
{
}
protected override void GenerateMethodInvokeExpression(CodeMethodInvokeExpression expression)
{
}
protected override void GenerateMethodReferenceExpression(CodeMethodReferenceExpression expression)
{
}
protected override void GenerateEventReferenceExpression(CodeEventReferenceExpression expression)
{
}
protected override void GenerateDelegateInvokeExpression(CodeDelegateInvokeExpression expression)
{
}
protected override void GenerateObjectCreateExpression(CodeObjectCreateExpression expression)
{
}
protected override void GeneratePropertyReferenceExpression(CodePropertyReferenceExpression expression)
{
}
protected override void GeneratePropertySetValueReferenceExpression(CodePropertySetValueReferenceExpression expression)
{
}
protected override void GenerateThisReferenceExpression(CodeThisReferenceExpression expression)
{
}
protected override void GenerateExpressionStatement(CodeExpressionStatement statement)
{
GenerateExpression(statement.Expression);
Output.WriteLine(';');
}
protected override void GenerateIterationStatement(CodeIterationStatement statement)
{
}
protected override void GenerateThrowExceptionStatement(CodeThrowExceptionStatement statement)
{
}
protected override void GenerateComment(CodeComment comment)
{
foreach (string line in comment.Text.Split('\n'))
{
Output.Write("// ");
Output.WriteLine(line);
}
}
protected override void GenerateMethodReturnStatement(CodeMethodReturnStatement statement)
{
if (statement.Expression != null)
{
Output.Write ("return ");
GenerateExpression(statement.Expression);
Output.WriteLine(";");
}
else
{
Output.WriteLine("return;");
}
}
protected override void GenerateConditionStatement(CodeConditionStatement statement)
{
}
protected override void GenerateTryCatchFinallyStatement(CodeTryCatchFinallyStatement statement)
{
}
protected override void GenerateAssignStatement(CodeAssignStatement statement)
{
GenerateExpression(statement.Left);
Output.Write(" = ");
GenerateExpression(statement.Right);
Output.WriteLine(';');
}
protected override void GenerateAttachEventStatement(CodeAttachEventStatement statement)
{
}
protected override void GenerateRemoveEventStatement(CodeRemoveEventStatement statement)
{
}
protected override void GenerateGotoStatement(CodeGotoStatement statement)
{
}
protected override void GenerateLabeledStatement(CodeLabeledStatement statement)
{
}
protected override void GenerateVariableDeclarationStatement(CodeVariableDeclarationStatement statement)
{
OutputTypeNamePair(statement.Type, GetSafeName(statement.Name));
CodeExpression initExpression = statement.InitExpression;
if (initExpression != null)
{
Output.Write(" = ");
GenerateExpression(initExpression);
}
Output.WriteLine(';');
}
protected override void GenerateLinePragmaStart(CodeLinePragma linePragma)
{
Output.WriteLine();
Output.Write("#line ");
Output.Write(linePragma.LineNumber);
Output.Write(" \"");
Output.Write(linePragma.FileName);
Output.Write("\"");
Output.WriteLine();
}
protected override void GenerateLinePragmaEnd(CodeLinePragma linePragma)
{
Output.WriteLine();
Output.WriteLine("#line default");
}
protected override void GenerateEvent(CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
{
}
protected override void GenerateField(CodeMemberField field)
{
MemberAttributes attributes = field.Attributes;
OutputMemberAccessModifier(attributes);
if (field.CustomAttributes.Count > 0)
{
OutputAttributeDeclarations(field.CustomAttributes);
}
OutputFieldScopeModifier(attributes);
OutputTypeNamePair(field.Type, GetSafeName (field.Name));
CodeExpression initExpression = field.InitExpression;
if (initExpression != null)
{
Output.Write(" = ");
GenerateExpression(initExpression);
}
if (IsCurrentEnum)
{
Output.WriteLine(',');
}
else
{
Output.WriteLine(';');
}
}
protected override void GenerateSnippetMember(CodeSnippetTypeMember member)
{
}
protected override void GenerateEntryPointMethod(CodeEntryPointMethod method, CodeTypeDeclaration declaration)
{
}
protected override void GenerateMethod(CodeMemberMethod method, CodeTypeDeclaration declaration)
{
MemberAttributes attributes = method.Attributes;
if (method.PrivateImplementationType == null && !declaration.IsInterface)
{
OutputMemberAccessModifier(attributes);
}
if (method.CustomAttributes.Count > 0)
{
OutputAttributeDeclarations(method.CustomAttributes);
}
if (method.ReturnTypeCustomAttributes.Count > 0)
{
OutputAttributeDeclarations(method.ReturnTypeCustomAttributes);
}
if (!declaration.IsInterface)
{
OutputMemberScopeModifier(attributes);
}
OutputType(method.ReturnType);
Output.Write(' ');
CodeTypeReference privateType = method.PrivateImplementationType;
if (privateType != null)
{
OutputType(privateType);
Output.Write("::");
}
Output.Write(GetSafeName(method.Name));
Output.Write('(');
OutputParameters(method.Parameters);
Output.Write(')');
if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract || declaration.IsInterface)
{
Output.WriteLine(" = 0;");
}
else
{
Output.WriteLine(" {");
++Indent;
GenerateStatements(method.Statements);
--Indent;
Output.WriteLine('}');
}
}
private void OutputPropertyModifier(MemberAttributes attributes)
{
OutputMemberAccessModifier(attributes);
Output.Write("__property ");
OutputMemberScopeModifier(attributes);
}
private void OutputPropertyStuff(CodeMemberProperty property, CodeTypeDeclaration declaration)
{
Output.Write('(');
OutputParameters(property.Parameters);
if (property.HasSet)
{
if (property.Parameters.Count > 0)
{
Output.Write(", ");
}
OutputTypeNamePair(property.Type, "value");
}
Output.Write(')');
if (declaration.IsInterface)
{
Output.WriteLine(" = 0");
}
else
{
Output.WriteLine (" {");
++Indent;
GenerateStatements (property.GetStatements);
--Indent;
Output.WriteLine ("}");
}
Output.WriteLine();
}
protected override void GenerateProperty(CodeMemberProperty property, CodeTypeDeclaration declaration)
{
if (property.CustomAttributes.Count > 0)
{
OutputAttributeDeclarations(property.CustomAttributes);
}
MemberAttributes attributes = property.Attributes;
if (property.HasGet)
{
OutputPropertyModifier(attributes);
OutputTypeNamePair(property.Type, GetSafeName("get_" + property.Name));
OutputPropertyStuff(property, declaration);
}
if (property.HasSet)
{
OutputPropertyModifier(attributes);
Output.Write("void ");
Output.Write(GetSafeName("set_" + property.Name));
OutputPropertyStuff(property, declaration);
}
}
protected override void GenerateConstructor(CodeConstructor constructor, CodeTypeDeclaration declaration)
{
OutputMemberAccessModifier(constructor.Attributes);
Output.Write(GetSafeName(CurrentTypeName) + " (");
OutputParameters(constructor.Parameters);
Output.Write (") ");
Output.WriteLine ("{");
Indent++;
GenerateStatements(constructor.Statements);
Indent--;
Output.WriteLine ('}');
}
protected override void GenerateTypeConstructor(CodeTypeConstructor constructor)
{
Output.WriteLine("static " + GetSafeName(CurrentTypeName) + "() {");
Indent++;
GenerateStatements(constructor.Statements);
Indent--;
Output.WriteLine('}');
}
protected override void GenerateTypeStart(CodeTypeDeclaration declaration)
{
CodeTypeDelegate del = declaration as CodeTypeDelegate;
if (declaration.CustomAttributes.Count > 0)
{
OutputAttributeDeclarations(declaration.CustomAttributes);
}
TypeAttributes attributes = declaration.TypeAttributes;
OutputTypeAttributes(attributes, declaration.IsStruct, declaration.IsEnum);
if (del != null)
{
if (del.ReturnType != null)
{
OutputType(del.ReturnType);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -