Ejemplo n.º 1
0
        /// <summary>
        /// Generates the class for the provided grain types.
        /// </summary>
        internal static TypeDeclarationSyntax GenerateClass(WellKnownTypes wellKnownTypes, GrainInterfaceDescription description)
        {
            var generatedTypeName = description.InvokerTypeName;
            var grainType         = description.Type;
            var baseTypes         = new List <BaseTypeSyntax> {
                SimpleBaseType(wellKnownTypes.IGrainMethodInvoker.ToTypeSyntax())
            };

            var genericTypes = grainType.GetHierarchyTypeParameters()
                               .Select(_ => TypeParameter(_.ToString()))
                               .ToArray();

            // Create the special method invoker marker attribute.
            var interfaceId         = description.InterfaceId;
            var interfaceIdArgument = interfaceId.ToHexLiteral();
            var grainTypeArgument   = TypeOfExpression(grainType.WithoutTypeParameters().ToTypeSyntax());
            var attributes          = new List <AttributeSyntax>
            {
                GeneratedCodeAttributeGenerator.GetGeneratedCodeAttributeSyntax(wellKnownTypes),
                Attribute(wellKnownTypes.MethodInvokerAttribute.ToNameSyntax())
                .AddArgumentListArguments(
                    AttributeArgument(grainTypeArgument),
                    AttributeArgument(interfaceIdArgument)),
                Attribute(wellKnownTypes.ExcludeFromCodeCoverageAttribute.ToNameSyntax())
            };

            var genericInvokerFields = GenerateGenericInvokerFields(wellKnownTypes, description.Methods);
            var members = new List <MemberDeclarationSyntax>(genericInvokerFields)
            {
                GenerateInvokeMethod(wellKnownTypes, grainType),
                GrainInterfaceCommon.GenerateInterfaceIdProperty(wellKnownTypes, description),
                GrainInterfaceCommon.GenerateInterfaceVersionProperty(wellKnownTypes, description)
            };

            // If this is an IGrainExtension, make the generated class implement IGrainExtensionMethodInvoker.
            if (grainType.HasInterface(wellKnownTypes.IGrainExtension))
            {
                baseTypes.Add(SimpleBaseType(wellKnownTypes.IGrainExtensionMethodInvoker.ToTypeSyntax()));
                members.Add(GenerateExtensionInvokeMethod(wellKnownTypes, grainType));
            }

            var classDeclaration =
                ClassDeclaration(generatedTypeName)
                .AddModifiers(Token(SyntaxKind.InternalKeyword))
                .AddBaseListTypes(baseTypes.ToArray())
                .AddConstraintClauses(grainType.GetTypeConstraintSyntax())
                .AddMembers(members.ToArray())
                .AddAttributeLists(AttributeList().AddAttributes(attributes.ToArray()));

            if (genericTypes.Length > 0)
            {
                classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes);
            }

            return(classDeclaration);
        }
        /// <summary>
        /// Generates the class for the provided grain types.
        /// </summary>
        internal TypeDeclarationSyntax GenerateClass(GrainInterfaceDescription description)
        {
            var generatedTypeName = description.ReferenceTypeName;
            var grainType         = description.Type;
            var genericTypes      = grainType.GetHierarchyTypeParameters()
                                    .Select(_ => TypeParameter(_.ToString()))
                                    .ToArray();

            // Create the special marker attribute.
            var grainTypeArgument = TypeOfExpression(grainType.WithoutTypeParameters().ToTypeSyntax());

            var attributes = AttributeList()
                             .AddAttributes(
                GeneratedCodeAttributeGenerator.GetGeneratedCodeAttributeSyntax(wellKnownTypes),
                Attribute(wellKnownTypes.SerializableAttribute.ToNameSyntax()),
                Attribute(wellKnownTypes.ExcludeFromCodeCoverageAttribute.ToNameSyntax()),
                Attribute(wellKnownTypes.GrainReferenceAttribute.ToNameSyntax())
                .AddArgumentListArguments(AttributeArgument(grainTypeArgument)));

            var classDeclaration =
                ClassDeclaration(generatedTypeName)
                .AddModifiers(Token(SyntaxKind.InternalKeyword))
                .AddBaseListTypes(
                    SimpleBaseType(wellKnownTypes.GrainReference.ToTypeSyntax()),
                    SimpleBaseType(grainType.ToTypeSyntax()))
                .AddConstraintClauses(grainType.GetTypeConstraintSyntax())
                .AddMembers(GenerateConstructors(generatedTypeName))
                .AddMembers(
                    GrainInterfaceCommon.GenerateInterfaceIdProperty(this.wellKnownTypes, description).AddModifiers(Token(SyntaxKind.OverrideKeyword)),
                    GrainInterfaceCommon.GenerateInterfaceVersionProperty(this.wellKnownTypes, description).AddModifiers(Token(SyntaxKind.OverrideKeyword)),
                    GenerateInterfaceNameProperty(description),
                    GenerateIsCompatibleMethod(description),
                    GenerateGetMethodNameMethod(description))
                .AddMembers(GenerateInvokeMethods(description))
                .AddAttributeLists(attributes);

            if (genericTypes.Length > 0)
            {
                classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes);
            }

            if (this.options.DebuggerStepThrough)
            {
                var debuggerStepThroughAttribute = Attribute(this.wellKnownTypes.DebuggerStepThroughAttribute.ToNameSyntax());
                classDeclaration = classDeclaration.AddAttributeLists(AttributeList().AddAttributes(debuggerStepThroughAttribute));
            }

            return(classDeclaration);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates the non serializer class for the provided grain types.
        /// </summary>
        internal (TypeDeclarationSyntax, TypeSyntax) GenerateClass(SemanticModel model, SerializerTypeDescription description, ILogger logger)
        {
            var className    = GetGeneratedClassName(description.Target);
            var type         = description.Target;
            var genericTypes = type.GetHierarchyTypeParameters().Select(_ => TypeParameter(_.ToString())).ToArray();

            var attributes = new List <AttributeSyntax>
            {
                GeneratedCodeAttributeGenerator.GetGeneratedCodeAttributeSyntax(wellKnownTypes),
                Attribute(wellKnownTypes.ExcludeFromCodeCoverageAttribute.ToNameSyntax()),
                Attribute(wellKnownTypes.SerializerAttribute.ToNameSyntax())
                .AddArgumentListArguments(
                    AttributeArgument(TypeOfExpression(type.WithoutTypeParameters().ToTypeSyntax())))
            };

            var fields = GetFields(model, type, logger);

            var members = new List <MemberDeclarationSyntax>(GenerateFields(fields))
            {
                GenerateConstructor(className, fields),
                GenerateDeepCopierMethod(type, fields, model),
                GenerateSerializerMethod(type, fields, model),
                GenerateDeserializerMethod(type, fields, model),
            };

            var classDeclaration =
                ClassDeclaration(className)
                .AddModifiers(Token(SyntaxKind.InternalKeyword))
                .AddModifiers(Token(SyntaxKind.SealedKeyword))
                .AddAttributeLists(AttributeList().AddAttributes(attributes.ToArray()))
                .AddMembers(members.ToArray())
                .AddConstraintClauses(type.GetTypeConstraintSyntax());

            if (genericTypes.Length > 0)
            {
                classDeclaration = classDeclaration.AddTypeParameterListParameters(genericTypes);
            }

            if (this.options.DebuggerStepThrough)
            {
                var debuggerStepThroughAttribute = Attribute(this.wellKnownTypes.DebuggerStepThroughAttribute.ToNameSyntax());
                classDeclaration = classDeclaration.AddAttributeLists(AttributeList().AddAttributes(debuggerStepThroughAttribute));
            }

            return(classDeclaration, ParseTypeName(type.GetParsableReplacementName(className)));
        }
Ejemplo n.º 4
0
        public static (List <AttributeListSyntax>, List <MemberDeclarationSyntax>) GenerateSyntax(
            WellKnownTypes wellKnownTypes,
            AggregatedModel model,
            Compilation compilation)
        {
            var attributes = new List <AttributeListSyntax>();
            var members    = new List <MemberDeclarationSyntax>();
            var className  = GetFeaturePopulatorClassName(compilation);

            // Generate a class for populating the metadata.
            var classSyntax = ClassDeclaration(className)
                              .AddBaseListTypes(
                SimpleBaseType(wellKnownTypes.IFeaturePopulator_1.Construct(wellKnownTypes.GrainInterfaceFeature).ToTypeSyntax()),
                SimpleBaseType(wellKnownTypes.IFeaturePopulator_1.Construct(wellKnownTypes.GrainClassFeature).ToTypeSyntax()),
                SimpleBaseType(wellKnownTypes.IFeaturePopulator_1.Construct(wellKnownTypes.SerializerFeature).ToTypeSyntax()))
                              .AddModifiers(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword))
                              .AddMembers(
                GeneratePopulateMethod(wellKnownTypes, model.GrainInterfaces),
                GeneratePopulateMethod(wellKnownTypes, model.GrainClasses),
                GeneratePopulateMethod(wellKnownTypes, model.Serializers))
                              .AddAttributeLists(AttributeList(SingletonSeparatedList(GeneratedCodeAttributeGenerator.GetGeneratedCodeAttributeSyntax(wellKnownTypes))));

            var namespaceSyntax = NamespaceDeclaration(NamespaceName.ToIdentifierName()).AddMembers(classSyntax);

            members.Add(namespaceSyntax);

            // Generate an assembly-level attribute with an instance of that class.
            var attribute = AttributeList(
                AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)),
                SingletonSeparatedList(
                    Attribute(wellKnownTypes.FeaturePopulatorAttribute.ToNameSyntax())
                    .AddArgumentListArguments(AttributeArgument(TypeOfExpression(ParseTypeName(NamespaceName + "." + className))))));

            attributes.Add(attribute);

            return(attributes, members);
        }