Example #1
0
        /// <summary>
        /// Creates a code type reference for the given type
        /// </summary>
        /// <param name="type">The given system type for which to generate a type reference</param>
        /// <param name="parameters">The type arguments</param>
        /// <returns>A code reference with namespace set accordingly</returns>
        public static CodeTypeReference ToTypeReference(this Type type, params CodeTypeReference[] parameters)
        {
            var reference = new CodeTypeReference(type.Name, parameters);

            reference.SetNamespace(type.Namespace);
            return(reference);
        }
Example #2
0
        /// <summary>
        /// Creates a code type reference for the given type
        /// </summary>
        /// <param name="type">The given system type for which to generate a type reference</param>
        /// <returns>A code reference with namespace set accordingly</returns>
        public static CodeTypeReference ToTypeReference(this Type type)
        {
            var reference = new CodeTypeReference(type.Name);

            reference.SetNamespace(type.Namespace);
            return(reference);
        }
Example #3
0
        protected virtual void ResolveMultipleInheritanceMembers(CodeTypeDeclaration generatedType, HashSet <CodeTypeMember> shadows, CodeConstructor constructor)
        {
            Func <CodeTypeDeclaration, IEnumerable <CodeTypeDeclaration> > getBaseTypes =
                type => {
                var interfaceType = CodeDomHelper.GetOrCreateUserItem <CodeTypeDeclaration>(type, CodeDomHelper.InterfaceKey);
                if (interfaceType == null)
                {
                    interfaceType = type;
                }
                return(interfaceType.BaseTypes.Cast <CodeTypeReference>().Select(r => r.GetTypeForReference()).Where(c => c != null));
            };
            var layering = Layering <CodeTypeDeclaration> .CreateLayers(generatedType, getBaseTypes);

            CodeTypeDeclaration implBaseType = FindBaseClassAndCreateShadows(generatedType, shadows, layering);
            IEnumerable <CodeTypeDeclaration> inheritedBaseClasses;

            if (implBaseType != null)
            {
                inheritedBaseClasses = implBaseType.Closure(getBaseTypes);
                var implementationRef = new CodeTypeReference();
                implementationRef.BaseType = implBaseType.Name;
                var n = implBaseType.GetReferenceForType().Namespace();
                if (n != null && n.EndsWith(implBaseType.Name))
                {
                    implementationRef.BaseType = n + "." + implBaseType.Name;
                }
                else
                {
                    implementationRef.SetNamespace(n);
                }
                generatedType.BaseTypes.Insert(0, implementationRef);
            }
            else
            {
                inheritedBaseClasses = Enumerable.Empty <CodeTypeDeclaration>();
                AddImplementationBaseClass(generatedType);
            }
            for (int i = layering.Count - 1; i >= 0; i--)
            {
                foreach (var baseType in layering[i])
                {
                    if (!inheritedBaseClasses.Contains(baseType) &&
                        baseType != generatedType &&
                        ShouldContainMembers(generatedType, baseType.GetReferenceForType()))
                    {
                        var dependent = baseType.DependentMembers(false);
                        if (dependent != null)
                        {
                            foreach (var inheritedMember in dependent)
                            {
                                RecursivelyAddDependentMembers(generatedType.Members, constructor.Statements, inheritedMember, shadows);
                            }
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Creates a reference to the given NMeta type
        /// </summary>
        /// <param name="type">The NMeta type</param>
        /// <param name="isReference">A value indicating whether to default to IModelElement or object</param>
        /// <param name="context">The transformation context</param>
        /// <returns>A code type reference</returns>
        protected static CodeTypeReference CreateReference(IType type, bool isReference, ITransformationContext context, bool implementation = false)
        {
            if (type != null)
            {
                var mappedType = type.GetExtension <MappedType>();
                if (mappedType != null)
                {
                    var reference = new CodeTypeReference();
                    if (mappedType.SystemType.IsValueType || mappedType.SystemType == typeof(string))
                    {
                        reference.BaseType = mappedType.SystemType.FullName;
                    }
                    else
                    {
                        reference.BaseType = mappedType.SystemType.Name;
                        if (typeof(IModelElement).IsAssignableFrom(mappedType.SystemType) && implementation)
                        {
                            reference.BaseType = reference.BaseType.Substring(1);
                        }
                        reference.SetNamespace(mappedType.SystemType.Namespace);
                    }
                    return(reference);
                }
                var declaration = context.Trace.ResolveIn((Type2Type)context.Transformation.GetRuleForRuleType(typeof(Type2Type)), type);
                if (declaration != null)
                {
                    if (!implementation)
                    {
                        return(CodeDomHelper.GetReferenceForType(declaration));
                    }
                    else
                    {
                        var reference = new CodeTypeReference();
                        reference.SetNamespace(CodeDomHelper.GetReferenceForType(declaration).Namespace());
                        reference.BaseType = declaration.Name;
                        return(reference);
                    }
                }
            }
            var primitiveType = type as IPrimitiveType;

            if (primitiveType != null)
            {
                return(new CodeTypeReference(primitiveType.SystemType));
            }
            if (isReference)
            {
                return(typeof(IModelElement).ToTypeReference());
            }
            else
            {
                return(new CodeTypeReference(typeof(object)));
            }
        }
Example #5
0
        /// <summary>
        /// Creates a code type reference for the given type
        /// </summary>
        /// <param name="type">The given system type for which to generate a type reference</param>
        /// <returns>A code reference with namespace set accordingly</returns>
        public static CodeTypeReference ToTypeReference(this Type type)
        {
            var reference = new CodeTypeReference(type.Name);

            reference.SetNamespace(type.Namespace);
            if (type.IsGenericType)
            {
                foreach (var typePar in type.GetGenericArguments())
                {
                    reference.TypeArguments.Add(typePar.ToTypeReference());
                }
            }
            return(reference);
        }
Example #6
0
        protected virtual void ResolveMultipleInheritanceMembers(CodeTypeDeclaration generatedType, HashSet <CodeTypeMember> shadows, CodeConstructor constructor)
        {
            var allClasses = generatedType.Closure(GetBaseClasses);
            var layering   = Layering <CodeTypeDeclaration> .CreateLayers(generatedType, c => Edges(c, allClasses));

            CodeTypeDeclaration implBaseType = null;
            int layerIndex;

            for (layerIndex = layering.Count - 1; layerIndex >= 0; layerIndex--)
            {
                var layer = layering[layerIndex];
                if (layer.Count == 1 && layer.First() != generatedType && !shadows.IntersectsWith(AllFeatures(layer.First())))
                {
                    implBaseType = layer.First();
                    break;
                }
                foreach (var cl in layer)
                {
                    shadows.UnionWith(Refinements(cl));
                }
            }
            IEnumerable <CodeTypeDeclaration> inheritedBaseClasses;

            if (implBaseType != null)
            {
                inheritedBaseClasses = layering.Take(layerIndex + 1).SelectMany(s => s);
                var implementationRef = new CodeTypeReference();
                implementationRef.BaseType = implBaseType.Name;
                var n = implBaseType.GetReferenceForType().Namespace();
                if (n != null && n.EndsWith(implBaseType.Name))
                {
                    implementationRef.BaseType = n + "." + implBaseType.Name;
                }
                else
                {
                    implementationRef.SetNamespace(n);
                }
                generatedType.BaseTypes.Insert(0, implementationRef);
            }
            else
            {
                inheritedBaseClasses = Enumerable.Empty <CodeTypeDeclaration>();
                AddImplementationBaseClass(generatedType);
            }
            CodeDomHelper.SetUserItem(generatedType, CodeDomHelper.BaseClassesKey, inheritedBaseClasses);
            for (int i = layerIndex + 1; i < layering.Count; i++)
            {
                foreach (var baseType in layering[i])
                {
                    if (baseType != generatedType)
                    {
                        var dependent = baseType.DependentMembers(false);
                        if (dependent != null)
                        {
                            foreach (var inheritedMember in dependent)
                            {
                                RecursivelyAddDependentMembers(generatedType.Members, constructor.Statements, inheritedMember, shadows);
                            }
                        }
                    }
                }
            }
        }