private static bool IsGenericArgsValid(MethodInfo method, TypeImpl type)
        {
            var methodGenericParameters = method.GetGenericArguments();
            var paramGenericParameters  = type.TypeInfo.GetGenericArguments();

            return(methodGenericParameters.Length == paramGenericParameters.Length);
        }
Exemple #2
0
        public override string GetTypeId(TypeImpl notNormalizedType)
        {
            if (notNormalizedType.TypeInfo.ContainsGenericParameters)
            {
                throw new ArgumentException($"{ Type } conatins generic parameters.");
            }

            var builder = new StringBuilder();

            builder.Append(TypeId);
            builder.Append('[');

            var argumentsCount = notNormalizedType.TypeInfo.GenericTypeArguments.Length;

            for (int i = 0; i < argumentsCount; i++)
            {
                var genericTypeArgument = notNormalizedType.TypeInfo.GenericTypeArguments[i];
                var typeId = SerializerTypes.GetTypeIdImpl(genericTypeArgument);

                builder.Append(typeId);

                var isLast = i == argumentsCount - 1;
                if (!isLast)
                {
                    builder.Append(",");
                }
            }

            builder.Append("]");

            return(builder.ToString());
        }
Exemple #3
0
        public SerializerTypeInfo(BinTypeDescription description, BinTypeVersion version, BinTypeProcess process)
        {
            // Check
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }

            // Set
            Type   = description.Type;
            TypeId = description.TypeId;

            Version             = version.Version;
            MinSupportedVersion = version.MinSipportedVersion;

            if (process != null)
            {
                StreamWriter = process.StreamWriter;
                StreamReader = process.StreamReader;
                StreamSkiper = process.StreamSkiper;

                _typeWriter = process.TypeWriter;
                _typeReader = process.TypeReader;
            }
        }
        public BinTypeDescription(TypeImpl type, string typeId)
        {
            // Type validation
            if (type.TypeInfo.IsGenericType && !type.TypeInfo.IsGenericTypeDefinition)
            {
                throw new ArgumentException("Only opened generic types can be registered.");
            }

            // Type id validation
            if (ReservedIds.Contains(typeId))
            {
                throw new ArgumentException($"This id reserved by serializer { typeId }.");
            }

            if (ReservedTypes.TryGetValue(typeId, out Type reservedType) && reservedType != type)
            {
                throw new ArgumentException($"This id reserved by serializer { typeId }.");
            }

            foreach (var ch in typeId)
            {
                if (ReservedChars.Contains(ch))
                {
                    throw new ArgumentException("Id contains reserved symbols '[',']','(',')','<','>'.");
                }
            }

            // Set
            Type   = type;
            TypeId = typeId;
        }
        private static SerializerTypeInfo GetTypeInfo(TypeImpl type)
        {
            var normalizedType = Normalize(type);

            if (!TypesByType.TryGetValue(normalizedType, out SerializerTypeInfo info))
            {
                throw new ArgumentException($"TypeInfo not found. For type { type }");
            }
            return(info);
        }
 internal static string GetTypeId(TypeImpl type)
 {
     Locker.EnterReadLock();
     try
     {
         return(GetTypeIdImpl(type));
     }
     finally
     {
         Locker.ExitReadLock();
     }
 }
Exemple #7
0
        public CCodeInterfaceMethodAdapterDefinition(ITypeSymbol type, IMethodSymbol interfaceMethod, IMethodSymbol classMethod)
            : base(interfaceMethod)
        {
            this.type = type;

            var receiver = type == classMethod.ContainingType || (classMethod.IsVirtual || classMethod.IsOverride || classMethod.IsAbstract)
                               ? (Expression) new ThisReference {
                Type = classMethod.ContainingType
            }
                               : (Expression) new BaseReference {
                Type = classMethod.ContainingType, ExplicitType = true
            };

            var call = new Call {
                ReceiverOpt = receiver, Method = classMethod
            };

            foreach (var argument in interfaceMethod.Parameters.Select(parameterSymbol => new Parameter {
                ParameterSymbol = parameterSymbol
            }))
            {
                call.Arguments.Add(argument);
            }

            var body = !interfaceMethod.ReturnsVoid ? (Statement) new ReturnStatement {
                ExpressionOpt = call
            } : (Statement) new ExpressionStatement {
                Expression = call
            };

            var methodBodyOpt = new MethodBody(Method);

            if (classMethod.IsGenericMethod)
            {
                // set generic types
                foreach (var typeArgument in classMethod.TypeArguments.Where(t => t.TypeKind == TypeKind.TypeParameter))
                {
                    methodBodyOpt.Statements.Add(
                        new TypeDef {
                        TypeExpressionOpt = new TypeExpression {
                            Type = typeArgument.GetFirstConstraintType() ?? new TypeImpl {
                                SpecialType = SpecialType.System_Object
                            }
                        }, Identifier = new TypeExpression {
                            Type = TypeImpl.Wrap(typeArgument, null)
                        }
                    });
                }
            }

            methodBodyOpt.Statements.Add(body);
            MethodBodyOpt = methodBodyOpt;
        }
Exemple #8
0
 public IsPrimitiveTypeArrayVirtualMethod(INamedTypeSymbol type)
 {
     Name           = "__is_primitive_type_array";
     MetadataName   = Name;
     MethodKind     = MethodKind.Ordinary;
     ContainingType = type;
     IsVirtual      = true;
     IsAbstract     = true;
     ReturnType     = new TypeImpl {
         SpecialType = SpecialType.System_Boolean
     };
     Parameters = ImmutableArray <IParameterSymbol> .Empty;
 }
 public GetArrayElementSizeVirtualMethod(INamedTypeSymbol type)
 {
     Name           = "__array_element_size";
     MetadataName   = Name;
     MethodKind     = MethodKind.Ordinary;
     ContainingType = type;
     IsVirtual      = true;
     IsAbstract     = true;
     ReturnType     = new TypeImpl {
         SpecialType = SpecialType.System_Int32
     };
     Parameters = ImmutableArray <IParameterSymbol> .Empty;
 }
Exemple #10
0
 public SpecialTypeConstructorMethod(INamedTypeSymbol type)
 {
     MethodKind     = MethodKind.Constructor;
     Name           = "_ctor";
     ReceiverType   = type;
     ContainingType = type;
     ReturnType     = new TypeImpl {
         SpecialType = SpecialType.System_Void
     };
     Parameters = ImmutableArray.Create <IParameterSymbol>(new ParameterImpl {
         Name = "value", Type = type
     });
 }
 internal static int GetMinSupported(TypeImpl type)
 {
     Locker.EnterReadLock();
     try
     {
         var info = GetTypeInfo(type);
         return(info.MinSupportedVersion);
     }
     finally
     {
         Locker.ExitReadLock();
     }
 }
 internal static MethodInfo TryGetStreamSkiper(TypeImpl type)
 {
     Locker.EnterReadLock();
     try
     {
         var info = GetTypeInfo(type);
         return(info.StreamSkiper);
     }
     finally
     {
         Locker.ExitReadLock();
     }
 }
Exemple #13
0
        public override string GetTypeId(TypeImpl notNormalizedType)
        {
            var elementType   = notNormalizedType.TypeInfo.GetElementType();
            var elementTypeId = SerializerTypes.GetTypeIdImpl(elementType);

            var builder = new StringBuilder();

            builder.Append(TypeId);
            builder.Append('[');
            builder.Append(elementTypeId);
            builder.Append(']');

            return(builder.ToString());
        }
Exemple #14
0
        private static MethodInfo GetMethod(MethodInfo method, TypeImpl notNormalizedType)
        {
            if (method == null)
            {
                return(null);
            }

            if (method.IsGenericMethodDefinition)
            {
                var genericArgs = notNormalizedType.TypeInfo.GetGenericArguments();
                return(method.MakeGenericMethod(genericArgs));
            }
            return(method);
        }
Exemple #15
0
 public GetTypeMethod(INamedTypeSymbol type, string name = "__new")
 {
     Name         = name;
     MetadataName = Name;
     MethodKind   = MethodKind.Ordinary;
     IsVirtual    = true;
     IsOverride   = true;
     ReturnsVoid  = false;
     ReturnType   = new TypeImpl {
         SpecialType = SpecialType.System_Object
     };
     Parameters     = ImmutableArray <IParameterSymbol> .Empty;
     ContainingType = type;
 }
 internal static MethodInfo TryGetTypeReader(TypeImpl type)
 {
     Locker.EnterReadLock();
     try
     {
         // Build
         var info = GetTypeInfo(type);
         return(info.GetTypeReader(type));
     }
     finally
     {
         Locker.ExitReadLock();
     }
 }
        internal bool IsValid(TypeImpl type)
        {
            if (TypeWriter != null && !IsGenericArgsValid(TypeWriter, type))
            {
                return(false);
            }

            if (TypeReader != null && !IsGenericArgsValid(TypeReader, type))
            {
                return(false);
            }

            return(true);
        }
Exemple #18
0
        private static ITypeSymbol GetTypeForVirtualGenericMethod(IMethodSymbol method, ITypeSymbol type, ISymbol containingSymbol)
        {
            // new, we need template view
            if (type.TypeKind == TypeKind.Array)
            {
                var sourceArrayType = (IArrayTypeSymbol)type;
                var arrayType       = new ArrayTypeImpl(sourceArrayType);
                arrayType.ElementType = GetTypeForVirtualGenericMethod(method, sourceArrayType.ElementType, containingSymbol);
                return(arrayType);
            }

            if (type.TypeKind == TypeKind.Pointer)
            {
                var sourcePointerType = (IPointerTypeSymbol)type;
                var pointerType       = new PointerTypeImpl();
                pointerType.PointedAtType = GetTypeForVirtualGenericMethod(method, sourcePointerType.PointedAtType, containingSymbol);
                return(pointerType);
            }

            if (type.TypeKind == TypeKind.TypeParameter)
            {
                for (var i = 0; i < method.TypeParameters.Length; i++)
                {
                    var typeParameter = method.TypeParameters[i];
                    if (typeParameter.Name == type.Name)
                    {
                        return(method.TypeArguments[i]);
                    }
                }

                return(new TypeImpl {
                    SpecialType = SpecialType.System_Object
                });
            }

            var namedTypeImpl = new NamedTypeImpl((INamedTypeSymbol)type);

            namedTypeImpl.ContainingSymbol = containingSymbol;
            namedTypeImpl.TypeArguments    =
                ImmutableArray.Create(
                    namedTypeImpl.TypeArguments.Select(
                        ta => method.TypeArguments.Contains(ta)
                                ? SetContaningSymbol(TypeImpl.Wrap(ta), containingSymbol)
                                : ta)
                    .OfType <ITypeSymbol>()
                    .ToArray());
            return(namedTypeImpl);
        }
 public IsTypeVirtualMethod(INamedTypeSymbol type)
 {
     Name                = "__is_type";
     MetadataName        = Name;
     MethodKind          = MethodKind.Ordinary;
     ContainingType      = type;
     ReceiverType        = type;
     ContainingNamespace = type.ContainingNamespace;
     IsVirtual           = true;
     IsOverride          = type.BaseType != null;
     ReturnType          = new TypeImpl {
         SpecialType = SpecialType.System_Boolean
     };
     Parameters = ImmutableArray.Create <IParameterSymbol>(new ParameterImpl {
         Name = "value", Type = type.GetBaseType().GetMembers().OfType <IMethodSymbol>().First(m => m.Name == "GetType").ReturnType
     });
 }
Exemple #20
0
        public void ReflectionExtensionsGetProperties()
        {
            tlog.Debug(tag, $"ReflectionExtensionsGetProperties START");

            try
            {
                TypeImpl type = new TypeImpl();
                Assert.IsNotNull(type, "null TypeImpl");
                ReflectionExtensions.GetProperties(type);
            }
            catch (Exception e)
            {
                tlog.Debug(tag, e.Message.ToString());
                Assert.Fail("Caught Exception : Failed!");
            }

            tlog.Debug(tag, $"ReflectionExtensionsGetProperties END");
        }
        private static TypeImpl Normalize(TypeImpl type)
        {
            if (type.TypeInfo.IsEnum)
            {
                return(new TypeImpl(Enum.GetUnderlyingType(type.Type)));
            }

            if (type.TypeInfo.IsArray)
            {
                return(new TypeImpl(typeof(Array)));
            }

            if (type.TypeInfo.IsGenericType && !type.TypeInfo.IsGenericTypeDefinition)
            {
                return(new TypeImpl(type.TypeInfo.GetGenericTypeDefinition()));
            }

            return(type);
        }
        internal static string GetTypeIdImpl(TypeImpl type)
        {
            // Try return from cache
            if (TypeToTypeIdCache.TryGetValue(type, out string cachedTypeId))
            {
                return(cachedTypeId);
            }

            // Resolve type id
            var info   = GetTypeInfo(type);
            var typeId = info.GetTypeId(type);

            // Add to cache
            TypeToTypeIdCache.TryAdd(type, typeId);
            TypeIdToTypeCache.TryAdd(typeId, type);

            // Result
            return(typeId);
        }
        public static void AddTypesFrom(Assembly assembly)
        {
            Locker.EnterWriteLock();
            try
            {
                foreach (var type in assembly.DefinedTypes)
                {
                    var attribute = type.GetCustomAttribute <BinTypeAttribute>(false);
                    if (attribute != null)
                    {
                        var typeImpl    = new TypeImpl(type);
                        var description = new BinTypeDescription(typeImpl, attribute.Id);
                        var version     = new BinTypeVersion(attribute.Version, attribute.MinSupportedVersion);

                        AddTypeImpl(description, version, null);
                    }
                }
            }
            finally
            {
                Locker.ExitWriteLock();
            }
        }
Exemple #24
0
        private IEnumerable <CCodeUnit> BuildUnit(ITypeSymbol type, IAssembliesInfoResolver assembliesInfoResolver)
        {
            var unit = new CCodeUnit(type);

            var isNotModule            = type.Name != "<Module>";
            var isNotInterfaceOrModule = isNotModule && type.TypeKind != TypeKind.Interface;
            var methodSymbols          = type.GetMembers().OfType <IMethodSymbol>().ToList();
            var hasStaticConstructor   = methodSymbols.Any(m => m.MethodKind == MethodKind.StaticConstructor);

            foreach (var field in type.GetMembers().OfType <IFieldSymbol>())
            {
                this.BuildField(field, unit, hasStaticConstructor);
            }

            foreach (var @event in type.GetMembers().OfType <IEventSymbol>())
            {
                this.BuildField(new FieldImpl(@event), unit, hasStaticConstructor);
            }

            if (hasStaticConstructor)
            {
                BuildStaticConstructorVariables(type, unit);
            }

            if (isNotModule)
            {
                ////BuildTypeHolderVariables(type, unit);
                BuildTypeDescriptorVariables(type, unit);
            }

            var constructors = methodSymbols.Where(m => m.MethodKind == MethodKind.Constructor);

            foreach (var method in constructors)
            {
                this.BuildMethod(method, unit);
            }

            var finalizationRequired = type.BaseType != null && type.GetMembers().OfType <IMethodSymbol>().Any(m => m.MethodKind == MethodKind.Destructor);
            var isAtomicType         = type.IsAtomicType();
            var namedTypeSymbol      = (INamedTypeSymbol)type;

            if (isNotInterfaceOrModule)
            {
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, debugVersion: true));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, true));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, true, true));
            }

            if (!isAtomicType && type.TypeKind != TypeKind.Interface)
            {
                unit.Declarations.Add(new CCodeGetTypeDescriptorDeclaration(namedTypeSymbol));
            }

            if (type.IsPrimitiveValueType() || type.TypeKind == TypeKind.Enum)
            {
                unit.Declarations.Add(new CCodeSpecialTypeOrEnumConstructorDeclaration(namedTypeSymbol, false));
            }

            /*
             * if (type.IsIntPtrType())
             * {
             *  unit.Declarations.Add(new CCodeSpecialTypeOrEnumConstructorDeclaration((INamedTypeSymbol)type, true));
             * }
             */

            // to support RuntimeType initialization
            if (type.IsRuntimeType())
            {
                unit.Declarations.Add(new CCodeRuntimeTypeConstructorDeclaration(namedTypeSymbol, true));
            }

            if (type.IsPrimitiveValueType() || type.TypeKind == TypeKind.Enum || type.IsIntPtrType())
            {
                unit.Declarations.Add(new CCodeCastOperatorDeclaration(namedTypeSymbol));
            }

            if (type.TypeKind == TypeKind.Struct)
            {
                unit.Declarations.Add(new CCodeArrowOperatorDeclaration(namedTypeSymbol));
            }

            if (isNotInterfaceOrModule)
            {
                // add internal infrustructure
                unit.Declarations.Add(new CCodeGetTypeVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeGetTypeVirtualMethodDefinition(namedTypeSymbol));
                unit.Declarations.Add(new CCodeIsTypeVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeIsTypeVirtualMethodDefinition(namedTypeSymbol));
                unit.Declarations.Add(new CCodeGetInterfaceVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeGetInterfaceVirtualMethodDefinition(namedTypeSymbol));
                if (!type.IsAbstract)
                {
                    unit.Declarations.Add(new CCodeCloneVirtualMethod(namedTypeSymbol));
                    unit.Declarations.Add(new CCodeGetSizeVirtualMethod(namedTypeSymbol));
                }

                if (type.SpecialType == SpecialType.System_Object)
                {
                    unit.Declarations.Add(new CCodeHashVirtualMethod(namedTypeSymbol));
                    unit.Declarations.Add(new CCodeEqualsVirtualMethod(namedTypeSymbol));
                }
            }

            if (type.TypeKind == TypeKind.Interface)
            {
                unit.Declarations.Add(new CCodeObjectCastOperatorDeclaration(namedTypeSymbol));
            }

            if (type.SpecialType == SpecialType.System_Array)
            {
                unit.Declarations.Add(new CCodeNewOperatorPointerDeclaration(namedTypeSymbol));
                unit.Declarations.Add(new CCodeGetArrayElementSizeVirtualMethod(namedTypeSymbol));
                unit.Declarations.Add(new CCodeIsPrimitiveTypeArrayVirtualMethod(namedTypeSymbol));
            }

            if (type.TypeKind == TypeKind.Interface)
            {
                // add all methods from all interfaces
                foreach (var method in type.EnumerateInterfaceMethods())
                {
                    unit.Declarations.Add(new CCodeMethodDeclaration(type, method));
                }
            }

            foreach (var method in methodSymbols.Where(m => m.MethodKind != MethodKind.Constructor))
            {
                this.BuildMethod(method, unit);
            }

            if (isNotModule)
            {
                BuildMethodTableVariables(type, unit);
                ////BuildRuntimeInfoVariables(type, unit);
            }

            if (isNotInterfaceOrModule)
            {
                // append interface calls
                foreach (var interfaceMethod in type.EnumerateInterfaceMethods())
                {
                    var method = interfaceMethod;
                    var implementationForInterfaceMember = type.FindImplementationForInterfaceMember(interfaceMethod) as IMethodSymbol;
                    if (implementationForInterfaceMember != null &&
                        implementationForInterfaceMember.ExplicitInterfaceImplementations.Any(ei => ei.Equals(method)))
                    {
                        continue;
                    }

                    Debug.Assert(implementationForInterfaceMember != null, "Method for interface can't be found");
                    unit.Declarations.Add(new CCodeInterfaceMethodAdapterDeclaration(type, interfaceMethod, implementationForInterfaceMember));
                    unit.Definitions.Add(new CCodeInterfaceMethodAdapterDefinition(type, interfaceMethod, implementationForInterfaceMember));
                }
            }

            yield return(unit);

            if (!isNotModule)
            {
                yield break;
            }

            // return type holder class
            var typeHolderType = (TypeImpl)TypeImpl.Wrap(type);

            if (!type.IsAnonymousType())
            {
                typeHolderType.Name         = typeHolderType.Name + "__type";
                typeHolderType.MetadataName = typeHolderType.MetadataName + "__type";
            }
            else
            {
                var namedType = (INamedTypeSymbol)type;
                typeHolderType.Name         = namedType.GetAnonymousTypeName() + "__type";
                typeHolderType.MetadataName = namedType.GetAnonymousTypeName() + "__type";
            }

            typeHolderType.BaseType      = null;
            typeHolderType.TypeKind      = TypeKind.Struct;
            typeHolderType.Interfaces    = ImmutableArray <INamedTypeSymbol> .Empty;
            typeHolderType.AllInterfaces = ImmutableArray <INamedTypeSymbol> .Empty;
            typeHolderType.SpecialType   = SpecialType.None;

            var unitTypeHolder = new CCodeUnit(typeHolderType);

            BuildTypeHolderVariables(typeHolderType, unitTypeHolder);
            ////BuildMethodTableVariables(typeHolderType, unitTypeHolder);
            BuildRuntimeInfoVariables(typeHolderType, type, unitTypeHolder);

            yield return(unitTypeHolder);
        }
Exemple #25
0
        internal void Parse(BoundDelegateCreationExpression boundDelegateCreationExpression)
        {
            base.Parse(boundDelegateCreationExpression);
            var argument = Deserialize(boundDelegateCreationExpression.Argument) as Expression;

            Debug.Assert(argument != null);

            if (boundDelegateCreationExpression.MethodOpt != null && !(boundDelegateCreationExpression.Argument is BoundMethodGroup))
            {
                var methodGroup = new MethodGroup
                {
                    ReceiverOpt      = argument,
                    Method           = boundDelegateCreationExpression.MethodOpt,
                    TypeArgumentsOpt = boundDelegateCreationExpression.MethodOpt.TypeArguments.Select(t => TypeImpl.Wrap(t)).ToList()
                };

                Arguments.Add(methodGroup);
            }
            else
            {
                if (argument.Type != null && argument.Type.TypeKind == TypeKind.Delegate)
                {
                    this.clone         = true;
                    this.cloneArgument = argument;
                }
                else
                {
                    var methodGroup = argument as MethodGroup;
                    if (methodGroup != null && boundDelegateCreationExpression.MethodOpt != null)
                    {
                        methodGroup.Method = boundDelegateCreationExpression.MethodOpt;
                    }

                    Arguments.Add(argument);
                }
            }
        }
Exemple #26
0
 public virtual string GetTypeId(TypeImpl notNormalizedType)
 {
     return(TypeId);
 }
Exemple #27
0
 public virtual MethodInfo GetTypeReader(TypeImpl notNormalizedType)
 {
     return(_typeReader);
 }
Exemple #28
0
        public override MethodInfo GetTypeReader(TypeImpl notNormalizedType)
        {
            var reader = base.GetTypeReader(notNormalizedType);

            return(GetMethod(reader, notNormalizedType));
        }
Exemple #29
0
        private IEnumerable <CCodeUnit> BuildUnit(ITypeSymbol type, IAssembliesInfoResolver assembliesInfoResolver)
        {
            var namedTypeSymbol = (INamedTypeSymbol)type;

            var unit = new CCodeUnit(type);

            var isNotModule = type.Name != "<Module>";

            // Class
            var isNotInterfaceOrModule = isNotModule && type.TypeKind != TypeKind.Interface;
            var methodSymbols          = type.GetMembers().OfType <IMethodSymbol>().ToList();
            var hasStaticConstructor   = methodSymbols.Any(m => m.MethodKind == MethodKind.StaticConstructor);

            // to support generic virtual methods
            #region Virtual Generic methods support
            var methodsTableType = "__methods_table".ToType();
            foreach (var typeParameter in namedTypeSymbol.GetTemplateParameters().Where(t => t.HasConstructorConstraint))
            {
                this.BuildField(new FieldImpl {
                    Type = methodsTableType, Name = "construct_" + typeParameter.Name
                }, unit, false);
            }
            #endregion

            foreach (var field in type.GetMembers().OfType <IFieldSymbol>())
            {
                this.BuildField(field, unit, hasStaticConstructor);
            }

            foreach (var @event in type.GetMembers().OfType <IEventSymbol>())
            {
                this.BuildField(new FieldImpl(@event), unit, hasStaticConstructor);
            }

            if (hasStaticConstructor)
            {
                BuildStaticConstructorVariables(type, unit);
            }

            if (isNotModule)
            {
                ////BuildTypeHolderVariables(type, unit);
                BuildTypeDescriptorVariables(type, unit);
            }

            var constructors = methodSymbols.Where(m => m.MethodKind == MethodKind.Constructor);
            foreach (var method in constructors)
            {
                this.BuildMethod(method, unit);
            }

            var finalizationRequired = type.BaseType != null && type.GetMembers().OfType <IMethodSymbol>().Any(m => m.MethodKind == MethodKind.Destructor);
            var isAtomicType         = type.IsAtomicType();
            if (isNotInterfaceOrModule && !type.IsAbstract)
            {
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, debugVersion: true));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, true));
                unit.Declarations.Add(new CCodeNewOperatorDeclaration(namedTypeSymbol, finalizationRequired, true, true));
            }

            if (!isAtomicType && type.TypeKind != TypeKind.Interface)
            {
                unit.Declarations.Add(new CCodeGetTypeDescriptorDeclaration(namedTypeSymbol));
            }

            if (type.IsPrimitiveValueType() || type.TypeKind == TypeKind.Enum)
            {
                unit.Declarations.Add(new CCodeSpecialTypeOrEnumConstructorDeclaration(namedTypeSymbol, false));
            }

            /*
             * if (type.IsIntPtrType())
             * {
             *  unit.Declarations.Add(new CCodeSpecialTypeOrEnumConstructorDeclaration((INamedTypeSymbol)type, true));
             * }
             */

            // to support RuntimeType initialization
            if (type.IsRuntimeType())
            {
                unit.Declarations.Add(new CCodeRuntimeTypeConstructorDeclaration(namedTypeSymbol, true));
            }

            if (type.IsPrimitiveValueType() || type.TypeKind == TypeKind.Enum || type.IsIntPtrType())
            {
                unit.Declarations.Add(new CCodeCastOperatorDeclaration(namedTypeSymbol));
            }

            if (type.TypeKind == TypeKind.Struct)
            {
                unit.Declarations.Add(new CCodeArrowOperatorDeclaration(namedTypeSymbol));
            }

            if (isNotInterfaceOrModule)
            {
                // add internal infrustructure
                unit.Declarations.Add(new CCodeGetTypeVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeGetTypeVirtualMethodDefinition(namedTypeSymbol));
                unit.Declarations.Add(new CCodeIsTypeVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeIsTypeVirtualMethodDefinition(namedTypeSymbol));
                unit.Declarations.Add(new CCodeGetInterfaceVirtualMethodDeclaration(namedTypeSymbol));
                unit.Definitions.Add(new CCodeGetInterfaceVirtualMethodDefinition(namedTypeSymbol));
                if (!type.IsAbstract)
                {
                    unit.Declarations.Add(new CCodeCloneVirtualMethod(namedTypeSymbol));
                    unit.Declarations.Add(new CCodeGetSizeVirtualMethod(namedTypeSymbol));
                }

                if (type.SpecialType == SpecialType.System_Object)
                {
                    unit.Declarations.Add(new CCodeHashVirtualMethod(namedTypeSymbol));
                    unit.Declarations.Add(new CCodeEqualsVirtualMethod(namedTypeSymbol));
                }
            }

            if (type.TypeKind == TypeKind.Interface)
            {
                unit.Declarations.Add(new CCodeObjectCastOperatorDeclaration(namedTypeSymbol));
            }

            if (type.SpecialType == SpecialType.System_Array)
            {
                unit.Declarations.Add(new CCodeNewOperatorPointerDeclaration(namedTypeSymbol));
                unit.Declarations.Add(new CCodeGetArrayElementSizeVirtualMethod(namedTypeSymbol));
                unit.Declarations.Add(new CCodeIsPrimitiveTypeArrayVirtualMethod(namedTypeSymbol));
            }

            if (type.TypeKind == TypeKind.Interface)
            {
                // add all methods from all interfaces
                foreach (var method in type.EnumerateInterfaceMethods())
                {
                    unit.Declarations.Add(new CCodeMethodDeclaration(type, method));
                }
            }

            foreach (var method in methodSymbols.Where(m => m.MethodKind != MethodKind.Constructor))
            {
                this.BuildMethod(method, unit);
            }

            // write interface wrappers
            foreach (var iface in namedTypeSymbol.Interfaces)
            {
                unit.Declarations.Add(new CCodeClassDeclaration(new CCodeInterfaceWrapperClass(namedTypeSymbol, iface)));
                unit.Declarations.Add(new CCodeInterfaceCastOperatorDeclaration(namedTypeSymbol, iface));
            }

            if (isNotModule)
            {
                BuildMethodTableVariables(type, unit);
                ////BuildRuntimeInfoVariables(type, unit);
            }

            // transition all methods which have body into source file
            foreach (var declaration in unit.Declarations.OfType <CCodeMethodDeclaration>().Where(m => m.MethodBodyOpt != null))
            {
                declaration.ToDefinition(unit.Definitions, namedTypeSymbol);
            }

            yield return(unit);

            // TypeHolder
            if (!isNotModule)
            {
                yield break;
            }

            // return type holder class
            var typeHolderType = (TypeImpl)TypeImpl.Wrap(type);

            if (!type.IsAnonymousType())
            {
                typeHolderType.Name         = typeHolderType.Name + "__type";
                typeHolderType.MetadataName = typeHolderType.MetadataName + "__type";
            }
            else
            {
                var namedType = (INamedTypeSymbol)type;
                typeHolderType.Name         = namedType.GetAnonymousTypeName() + "__type";
                typeHolderType.MetadataName = namedType.GetAnonymousTypeName() + "__type";
            }

            typeHolderType.BaseType      = null;
            typeHolderType.TypeKind      = TypeKind.Struct;
            typeHolderType.Interfaces    = ImmutableArray <INamedTypeSymbol> .Empty;
            typeHolderType.AllInterfaces = ImmutableArray <INamedTypeSymbol> .Empty;
            typeHolderType.SpecialType   = SpecialType.None;

            var unitTypeHolder = new CCodeUnit(typeHolderType);
            BuildTypeHolderVariables(typeHolderType, unitTypeHolder);
            ////BuildMethodTableVariables(typeHolderType, unitTypeHolder);
            BuildRuntimeInfoVariables(typeHolderType, type, unitTypeHolder);

            yield return(unitTypeHolder);
        }
Exemple #30
0
 /// <summary>
 /// 用于创建modelBuilder
 /// </summary>
 /// <param name="modelBuilder"></param>
 protected internal virtual void DoRegisterTypes(ModelBuilder modelBuilder)
 {
     //TODO Dmn类型注册
     try
     {
         AllowedAnswersImpl.RegisterType(modelBuilder);
         AllowedValuesImpl.RegisterType(modelBuilder);
         ArtifactImpl.RegisterType(modelBuilder);
         AssociationImpl.RegisterType(modelBuilder);
         AuthorityRequirementImpl.RegisterType(modelBuilder);
         BindingImpl.RegisterType(modelBuilder);
         BusinessContextElementImpl.RegisterType(modelBuilder);
         BusinessKnowledgeModelImpl.RegisterType(modelBuilder);
         ColumnImpl.RegisterType(modelBuilder);
         ContextEntryImpl.RegisterType(modelBuilder);
         ContextImpl.RegisterType(modelBuilder);
         DecisionImpl.RegisterType(modelBuilder);
         DecisionMadeReferenceImpl.RegisterType(modelBuilder);
         DecisionMakerReferenceImpl.RegisterType(modelBuilder);
         DecisionOwnedReferenceImpl.RegisterType(modelBuilder);
         DecisionOwnerReferenceImpl.RegisterType(modelBuilder);
         DecisionRuleImpl.RegisterType(modelBuilder);
         DecisionServiceImpl.RegisterType(modelBuilder);
         DecisionTableImpl.RegisterType(modelBuilder);
         DefaultOutputEntryImpl.RegisterType(modelBuilder);
         DefinitionsImpl.RegisterType(modelBuilder);
         DescriptionImpl.RegisterType(modelBuilder);
         DmnElementImpl.RegisterType(modelBuilder);
         DmnElementReferenceImpl.RegisterType(modelBuilder);
         DrgElementImpl.RegisterType(modelBuilder);
         DrgElementReferenceImpl.RegisterType(modelBuilder);
         ElementCollectionImpl.RegisterType(modelBuilder);
         EncapsulatedDecisionReferenceImpl.RegisterType(modelBuilder);
         EncapsulatedLogicImpl.RegisterType(modelBuilder);
         ExpressionImpl.RegisterType(modelBuilder);
         ExtensionElementsImpl.RegisterType(modelBuilder);
         FormalParameterImpl.RegisterType(modelBuilder);
         FunctionDefinitionImpl.RegisterType(modelBuilder);
         ImpactedPerformanceIndicatorReferenceImpl.RegisterType(modelBuilder);
         ImpactingDecisionReferenceImpl.RegisterType(modelBuilder);
         ImportImpl.RegisterType(modelBuilder);
         ImportedElementImpl.RegisterType(modelBuilder);
         ImportedValuesImpl.RegisterType(modelBuilder);
         InformationItemImpl.RegisterType(modelBuilder);
         InformationRequirementImpl.RegisterType(modelBuilder);
         InputImpl.RegisterType(modelBuilder);
         InputClauseImpl.RegisterType(modelBuilder);
         InputDataImpl.RegisterType(modelBuilder);
         InputDataReferenceImpl.RegisterType(modelBuilder);
         InputDecisionReferenceImpl.RegisterType(modelBuilder);
         InputEntryImpl.RegisterType(modelBuilder);
         InputExpressionImpl.RegisterType(modelBuilder);
         InputValuesImpl.RegisterType(modelBuilder);
         InvocationImpl.RegisterType(modelBuilder);
         ItemComponentImpl.RegisterType(modelBuilder);
         ItemDefinitionImpl.RegisterType(modelBuilder);
         ItemDefinitionReferenceImpl.RegisterType(modelBuilder);
         KnowledgeRequirementImpl.RegisterType(modelBuilder);
         KnowledgeSourceImpl.RegisterType(modelBuilder);
         ListImpl.RegisterType(modelBuilder);
         LiteralExpressionImpl.RegisterType(modelBuilder);
         ModelElementInstanceImpl.RegisterType(modelBuilder);
         NamedElementImpl.RegisterType(modelBuilder);
         OrganizationUnitImpl.RegisterType(modelBuilder);
         OutputImpl.RegisterType(modelBuilder);
         OutputClauseImpl.RegisterType(modelBuilder);
         OutputDecisionReferenceImpl.RegisterType(modelBuilder);
         OutputEntryImpl.RegisterType(modelBuilder);
         OutputValuesImpl.RegisterType(modelBuilder);
         OwnerReferenceImpl.RegisterType(modelBuilder);
         ParameterReferenceImpl.RegisterType(modelBuilder);
         PerformanceIndicatorImpl.RegisterType(modelBuilder);
         QuestionImpl.RegisterType(modelBuilder);
         RelationImpl.RegisterType(modelBuilder);
         RequiredAuthorityReferenceImpl.RegisterType(modelBuilder);
         RequiredDecisionReferenceImpl.RegisterType(modelBuilder);
         RequiredInputReferenceImpl.RegisterType(modelBuilder);
         RequiredKnowledgeReferenceImpl.RegisterType(modelBuilder);
         RowImpl.RegisterType(modelBuilder);
         RuleImpl.RegisterType(modelBuilder);
         SourceRefImpl.RegisterType(modelBuilder);
         SupportedObjectiveReferenceImpl.RegisterType(modelBuilder);
         TargetRefImpl.RegisterType(modelBuilder);
         TextImpl.RegisterType(modelBuilder);
         TextAnnotationImpl.RegisterType(modelBuilder);
         TypeImpl.RegisterType(modelBuilder);
         TypeRefImpl.RegisterType(modelBuilder);
         UnaryTestsImpl.RegisterType(modelBuilder);
         UsingProcessReferenceImpl.RegisterType(modelBuilder);
         UsingTaskReferenceImpl.RegisterType(modelBuilder);
         VariableImpl.RegisterType(modelBuilder);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
     /// <summary>
     /// camunda extensions </summary>
 }