예제 #1
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;
    }
예제 #2
0
        public ITypeReference Map(ITypeSymbol typeSymbol)
        {
            Contract.Requires(typeSymbol != null);
            Contract.Ensures(Contract.Result <ITypeReference>() != null);

            ITypeReference itr       = null;
            var            arrayType = typeSymbol as 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 ITypeParameterSymbol;
                if (genericTypeSymbol != null)
                {
                    var containingSymbol = typeSymbol.ContainingSymbol;
                    if (containingSymbol is IMethodSymbol)
                    {
                        tr = new GenericMethodParameterReference()
                        {
                            DefiningMethod = this.Map((IMethodSymbol)containingSymbol),
                            Name           = this.host.NameTable.GetNameFor(typeSymbol.Name),
                        };
                    }
                    else
                    {
                        // assume it is a class parameter?
                        tr = new GenericTypeParameterReference()
                        {
                            DefiningType = this.Map((ITypeSymbol)containingSymbol),
                            Name         = this.host.NameTable.GetNameFor(typeSymbol.Name),
                        };
                    }
                }

                var namedTypeSymbol = typeSymbol as 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.Length),
                                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.Length,
                                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);
        }