Ejemplo n.º 1
0
        public static PrimitiveTypeCode GetPrimitiveTypeCode(R.ITypeSymbol type)
        {
            Contract.Requires(type != null);
            Contract.Ensures(Contract.Result <PrimitiveTypeCode>() != PrimitiveTypeCode.Pointer &&
                             Contract.Result <PrimitiveTypeCode>() != PrimitiveTypeCode.Reference &&
                             Contract.Result <PrimitiveTypeCode>() != PrimitiveTypeCode.Invalid,
                             "These types aren't checked for; all others are.");
            //if (type.Name == null || String.IsNullOrEmpty(type.Name.Text)) throw new IllFormedSemanticModelException("A CSharpType was found with a null or empty 'Name' field.", type);
            switch (type.SpecialType)
            {
            case Microsoft.CodeAnalysis.SpecialType.System_Boolean: return(PrimitiveTypeCode.Boolean);

            case Microsoft.CodeAnalysis.SpecialType.System_Char: return(PrimitiveTypeCode.Char);

            case Microsoft.CodeAnalysis.SpecialType.System_SByte: return(PrimitiveTypeCode.Int8);

            case Microsoft.CodeAnalysis.SpecialType.System_Single: return(PrimitiveTypeCode.Float32);

            case Microsoft.CodeAnalysis.SpecialType.System_Double: return(PrimitiveTypeCode.Float64);

            case Microsoft.CodeAnalysis.SpecialType.System_Int16: return(PrimitiveTypeCode.Int16);

            case Microsoft.CodeAnalysis.SpecialType.System_Int32: return(PrimitiveTypeCode.Int32);

            case Microsoft.CodeAnalysis.SpecialType.System_Int64: return(PrimitiveTypeCode.Int64);

            case Microsoft.CodeAnalysis.SpecialType.System_IntPtr: return(PrimitiveTypeCode.IntPtr);

            case Microsoft.CodeAnalysis.SpecialType.System_String: return(PrimitiveTypeCode.String);

            case Microsoft.CodeAnalysis.SpecialType.System_Byte: return(PrimitiveTypeCode.UInt8);

            case Microsoft.CodeAnalysis.SpecialType.System_UInt16: return(PrimitiveTypeCode.UInt16);

            case Microsoft.CodeAnalysis.SpecialType.System_UInt32: return(PrimitiveTypeCode.UInt32);

            case Microsoft.CodeAnalysis.SpecialType.System_UInt64: return(PrimitiveTypeCode.UInt64);

            case Microsoft.CodeAnalysis.SpecialType.System_UIntPtr: return(PrimitiveTypeCode.UIntPtr);

            case Microsoft.CodeAnalysis.SpecialType.System_Void: return(PrimitiveTypeCode.Void);

            default: return(PrimitiveTypeCode.NotPrimitive);
            }
        }
Ejemplo n.º 2
0
        private ITypeDefinition CreateTypeDefinition(R.ITypeSymbol typeSymbol)
        {
            Contract.Requires(typeSymbol != null);

            var nts = typeSymbol as R.INamedTypeSymbol;

            if (nts != null)
            {
                return(this.CreateTypeDefinition(nts));
            }

            var gts = typeSymbol as R.ITypeParameterSymbol;

            if (gts != null)
            {
                return(this.CreateTypeDefinition(gts));
            }

            throw new InvalidDataException("CreateTypeDefinition: type case is supposed to be exhaustive.");
        }
Ejemplo n.º 3
0
        public ITypeReference Map(R.ITypeSymbol typeSymbol)
        {
            Contract.Requires(typeSymbol != null);
            Contract.Ensures(Contract.Result <ITypeReference>() != null);

            ITypeReference itr       = null;
            var            arrayType = typeSymbol as R.IArrayTypeSymbol;

            if (arrayType != null)
            {
                typeSymbol = arrayType.ElementType;
            }

            TypeReference tr = null;

            if (!typeSymbolCache.TryGetValue(typeSymbol, out itr))
            {
                if (this.assemblyBeingTranslated.Equals(typeSymbol.ContainingAssembly))
                {
                    // then we have reached this type symbol from a place where it is being referenced,
                    // before its definition has been visited
                    var t = this.CreateTypeDefinition(typeSymbol);
                    return(t);
                }

                var genericTypeSymbol = typeSymbol as R.ITypeParameterSymbol;
                if (genericTypeSymbol != null)
                {
                    var containingSymbol = typeSymbol.ContainingSymbol;
                    if (containingSymbol is R.IMethodSymbol)
                    {
                        tr = new GenericMethodParameterReference()
                        {
                            DefiningMethod = this.Map((R.IMethodSymbol)containingSymbol),
                            Name           = this.host.NameTable.GetNameFor(typeSymbol.Name),
                        };
                    }
                    else
                    {
                        // assume it is a class parameter?
                        tr = new GenericTypeParameterReference()
                        {
                            DefiningType = this.Map((R.ITypeSymbol)containingSymbol),
                            Name         = this.host.NameTable.GetNameFor(typeSymbol.Name),
                        };
                    }
                }

                var namedTypeSymbol = typeSymbol as R.INamedTypeSymbol;
                // if the symbol and its ConstructedFrom are the same then it is the template
                if (namedTypeSymbol != null)
                {
                    if (namedTypeSymbol.IsGenericType && namedTypeSymbol != namedTypeSymbol.ConstructedFrom)
                    {
                        var gas = new List <ITypeReference>();
                        foreach (var a in namedTypeSymbol.TypeArguments)
                        {
                            gas.Add(this.Map(a));
                        }
                        var gtr = new Microsoft.Cci.MutableCodeModel.GenericTypeInstanceReference()
                        {
                            GenericArguments = gas,
                            GenericType      = (INamedTypeReference)this.Map(namedTypeSymbol.ConstructedFrom),
                        };
                        tr = gtr;
                    }
                    else
                    {
                        if (typeSymbol.ContainingType == null)
                        {
                            var ntr = new Microsoft.Cci.MutableCodeModel.NamespaceTypeReference()
                            {
                                ContainingUnitNamespace = Map(typeSymbol.ContainingNamespace),
                                GenericParameterCount   = (ushort)(namedTypeSymbol == null ? 0 : namedTypeSymbol.TypeParameters.Count),
                                IsValueType             = typeSymbol.IsValueType,
                                Name = this.nameTable.GetNameFor(typeSymbol.Name),
                            };
                            tr = ntr;
                        }
                        else
                        {
                            var nestedTr = new Microsoft.Cci.MutableCodeModel.NestedTypeReference()
                            {
                                ContainingType        = Map(typeSymbol.ContainingType),
                                GenericParameterCount = (ushort)namedTypeSymbol.TypeParameters.Count,
                                Name = this.nameTable.GetNameFor(typeSymbol.Name),
                            };
                            tr = nestedTr;
                        }
                    }
                }
                Contract.Assume(tr != null, "Above type tests meant to be exhaustive");
                tr.InternFactory = this.host.InternFactory;
                tr.PlatformType  = this.host.PlatformType;
                tr.TypeCode      = GetPrimitiveTypeCode(typeSymbol);
                this.typeSymbolCache[typeSymbol] = tr;
                itr = tr;
            }
            if (arrayType != null)
            {
                itr = (IArrayTypeReference)Microsoft.Cci.Immutable.Vector.GetVector(itr, this.host.InternFactory);
            }
            return(itr);
        }