private void GenerateOrdinaryClass(ClassDeclarationSyntax classDeclaration, SemanticModel semanticModel, string outputPath) { INamedTypeSymbol typeInfo = semanticModel.GetDeclaredSymbol(classDeclaration); var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo); var genericTypeParameters = typeInfo.TypeParameters; var typeReferences = new HashSet <string>(); var propertyDeclarations = classDeclaration.DescendantNodes().OfType <PropertyDeclarationSyntax>(); var generatedProperties = GetProperties(semanticModel, typeReferences, propertyDeclarations, false); var fieldsDeclarations = classDeclaration.DescendantNodes().OfType <FieldDeclarationSyntax>(); var generatedFields = GetFields(semanticModel, typeReferences, fieldsDeclarations); var constructorDeclarations = classDeclaration.DescendantNodes().OfType <ConstructorDeclarationSyntax>(); var generatedConstructors = GetConstructors(semanticModel, typeReferences, constructorDeclarations); var methodDeclarations = classDeclaration.DescendantNodes().OfType <MethodDeclarationSyntax>(); var generatedMethods = GetMethods(semanticModel, typeReferences, methodDeclarations); var classSourceCode = TypeBuilder.BuildType(semanticModel, fullyQualifiedNameParts, genericTypeParameters, typeInfo.BaseType, typeInfo.AllInterfaces, generatedConstructors, generatedFields, generatedProperties, generatedMethods); WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, classSourceCode.MainPart); }
private void Generate(EnumDeclarationSyntax enumDeclaration, SemanticModel semanticModel, string outputPath) { INamedTypeSymbol typeInfo = semanticModel.GetDeclaredSymbol(enumDeclaration); var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo); var sourceCode = TypeBuilder.BuildEnum(semanticModel, fullyQualifiedNameParts, enumDeclaration.Members); WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, sourceCode.MainPart); }
private void Generate(InterfaceDeclarationSyntax interfaceDeclaration, SemanticModel semanticModel, string outputPath) { INamedTypeSymbol typeInfo = semanticModel.GetDeclaredSymbol(interfaceDeclaration); var fullyQualifiedNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(typeInfo); var genericTypeParameters = typeInfo.TypeParameters; var typeReferences = new HashSet <string>(); var propertyDeclarations = interfaceDeclaration.DescendantNodes().OfType <PropertyDeclarationSyntax>(); var generatedProperties = GetProperties(semanticModel, typeReferences, propertyDeclarations, true); var methodDeclarations = interfaceDeclaration.DescendantNodes().OfType <MethodDeclarationSyntax>(); var generatedMethods = GetMethods(semanticModel, typeReferences, methodDeclarations); var interfaceSourceCode = TypeBuilder.BuildInterface(semanticModel, fullyQualifiedNameParts, genericTypeParameters, generatedProperties, generatedMethods); WriteToOutputIfPathPresent(outputPath, fullyQualifiedNameParts, interfaceSourceCode.MainPart); }
public TypeReference BuildDelegateReference(INamedTypeSymbol namedTypeSymbol, SemanticModel semanticModel) { var fullyQualifiedPropertyTypeNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(namedTypeSymbol); string name = namedTypeSymbol.Name; bool isAction = name == "Action"; bool isFunc = name == "Func"; if (namedTypeSymbol.IsGenericType) { var typeParameters = new List <TypeReference>(); var args = namedTypeSymbol.TypeArguments.ToList(); for (int i = 0; i < args.Count; i++) { var typeArgument = args[i]; var typeReference = GenerateTypeReference(typeArgument, semanticModel, isInGenericContext: true); typeParameters.Add(typeReference); if (!(isFunc && i == 0)) { name += "T"; } } string typeReferenceText = TypeReferenceBuilder .BuildTypeReference(new[] { "by.besmart.cross.delegates", name }, typeParameters); return(new TypeReference { Text = typeReferenceText, IsReferenceType = namedTypeSymbol.IsReferenceType, IsGeneric = true }); } string typeReferenceTextNonGeneric = TypeReferenceBuilder .BuildTypeReference(new[] { "by.besmart.cross.delegates", name }, new List <TypeReference>()); return(new TypeReference { Text = typeReferenceTextNonGeneric, IsReferenceType = namedTypeSymbol.IsReferenceType }); }
public TypeReference GenerateTypeReference(ITypeSymbol typeSymbol, SemanticModel semanticModel, bool isInGenericContext = false) { if (typeSymbol is ITypeParameterSymbol) { return(new TypeReference { Text = GenericTypeReferenceBuilder.BuildReference(typeSymbol as ITypeParameterSymbol), IsGeneric = true, IsReferenceType = typeSymbol.IsReferenceType }); } var namedTypeSymbol = (INamedTypeSymbol)typeSymbol; string fullyQualifiedName = SyntaxTreeHelper.GetFullyQualifiedName(namedTypeSymbol); var fullyQualifiedPropertyTypeNameParts = SyntaxTreeHelper.GetFullyQualifiedNameParts(namedTypeSymbol); var typeParameters = new List <TypeReference>(); if (namedTypeSymbol.IsGenericType) { foreach (var typeArgument in namedTypeSymbol.TypeArguments) { var typeReference = GenerateTypeReference(typeArgument, semanticModel, isInGenericContext: true); typeParameters.Add(typeReference); } } if (PredefinedTypes.IsPredefined(fullyQualifiedName)) { return(new TypeReference { Text = isInGenericContext ? PredefinedTypes.GetInGenericContext(fullyQualifiedName) : PredefinedTypes.Get(fullyQualifiedName), IsPredefined = true, IsReferenceType = typeSymbol.IsReferenceType }); } if (IsCustomImplementedType(fullyQualifiedName)) { if (namedTypeSymbol.IsGenericType) { } string typeReferenceText = "by.misharp." + (namedTypeSymbol.IsGenericType ? TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts, typeParameters) : TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts)); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } if (IsDelegateType(namedTypeSymbol)) { return(BuildDelegateReference(namedTypeSymbol, semanticModel)); } if (namedTypeSymbol.IsGenericType) { string typeReferenceText = TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts, typeParameters); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } else { string typeReferenceText = TypeReferenceBuilder.BuildTypeReference(fullyQualifiedPropertyTypeNameParts); return(new TypeReference { Text = typeReferenceText, IsReferenceType = typeSymbol.IsReferenceType }); } }