public MethodDeclaration(ObjectType objectType, MethodDeclarationSyntax declaration, SemanticModel semanticModel) : base(declaration.Modifiers, declaration.AttributeLists) { this.objectType = objectType; this.declaration = declaration; // get name name = declaration.Identifier.ValueText; var symbol = semanticModel.GetDeclaredSymbol((BaseTypeDeclarationSyntax)declaration.Parent); fullName = Tools.GetFullTypeName(symbol) + '.' + name; fullNameFlat = Tools.GetFullTypeNameFlat(symbol) + '_' + name; // parse method parameters parameters = new List <MethodParameter>(); foreach (var parameter in declaration.ParameterList.Parameters) { parameters.Add(new MethodParameter(this, parameter, semanticModel)); } // parse return type returnType = new MethodReturn(declaration.ReturnType, semanticModel); // parse method body body = new LogicalBody(this, declaration.Body, semanticModel); }
public PropertyDeclaration(ObjectType objectType, PropertyDeclarationSyntax declaration, SemanticModel semanticModel) : base((semanticModel.GetDeclaredSymbol(declaration)).Type, declaration.Modifiers, declaration.AttributeLists) { this.objectType = objectType; this.declaration = declaration; // get name name = declaration.Identifier.ValueText; var symbol = semanticModel.GetDeclaredSymbol((BaseTypeDeclarationSyntax)declaration.Parent); fullName = Tools.GetFullTypeName(symbol) + '.' + name; fullNameFlat = Tools.GetFullTypeNameFlat(symbol) + '_' + name; // get initialized value (Not supported in C# 3) /*if (declaration.Initializer != null && declaration.Initializer.Value != null) * { * var value = semanticModel.GetConstantValue(declaration.Initializer.Value); * initializedValue = value.HasValue ? value.Value : null; * }*/ // parse method set/get foreach (var accessor in declaration.AccessorList.Accessors) { if (accessor.Keyword.IsKind(SyntaxKind.GetKeyword)) { if (accessor.Body != null) { getBody = new LogicalBody(this, accessor.Body, semanticModel); } } else if (accessor.Keyword.IsKind(SyntaxKind.SetKeyword)) { if (accessor.Body != null) { setBody = new LogicalBody(this, accessor.Body, semanticModel); } } else { throw new Exception("Unsuported property accessor: " + accessor.Keyword); } } }
private static void WriteLogicalBody(LogicalBody body, StreamWriter writer) { StreamWriterEx.prefix = "\t"; foreach (var statement in body.statements) { if (statement is ExpressionStatement) { WriteExpressionStatement((ExpressionStatement)statement, writer); } else if (statement is ReturnStatement) { WriteReturnStatement((ReturnStatement)statement, writer); } else if (statement is LocalDeclarationStatement) { WriteLocalDeclarationStatement((LocalDeclarationStatement)statement, writer); } else { throw new NotImplementedException("Unsuported statement type: " + statement.GetType()); } } }