Beispiel #1
0
        public static TypeReference ReadTypeReference(XmlReader reader)
        {
            if (reader == null || reader.NodeType != XmlNodeType.Element)
            {
                return(null);
            }

            switch (reader.Name)
            {
            // For the TypeReference(s)...
            case "SimpleTypeReference":
                SimpleTypeReference simpleTypeReference = new SimpleTypeReference();
                simpleTypeReference.ReadXml(reader);
                return(simpleTypeReference);

            case "SpecializedTypeReference":
                SpecializedTypeReference specializedTypeReference = new SpecializedTypeReference();
                specializedTypeReference.ReadXml(reader);
                return(specializedTypeReference);

            case "ArrayTypeReference":
                ArrayTypeReference arrayTypeReference = new ArrayTypeReference();
                arrayTypeReference.ReadXml(reader);
                return(arrayTypeReference);

            case "ReferenceTypeReference":
                ReferenceTypeReference referenceTypeReference = new ReferenceTypeReference();
                referenceTypeReference.ReadXml(reader);
                return(referenceTypeReference);

            case "PointerTypeReference":
                PointerTypeReference pointerTypeReference = new PointerTypeReference();
                pointerTypeReference.ReadXml(reader);
                return(pointerTypeReference);

            // For the TemplateTypeReference(s)...
            case "IndexedTemplateTypeReference":
                IndexedTemplateTypeReference indexedTemplateTypeReference =
                    new IndexedTemplateTypeReference();
                indexedTemplateTypeReference.ReadXml(reader);
                return(indexedTemplateTypeReference);

            case "NamedTemplateTypeReference":
                NamedTemplateTypeReference namedTemplateTypeReference = new NamedTemplateTypeReference();
                namedTemplateTypeReference.ReadXml(reader);
                return(namedTemplateTypeReference);

            case "TypeTemplateTypeReference":
                TypeTemplateTypeReference typeTemplateTypeReference = new TypeTemplateTypeReference();
                typeTemplateTypeReference.ReadXml(reader);
                return(typeTemplateTypeReference);

            case "MethodTemplateTypeReference":
                MethodTemplateTypeReference methodTemplateTypeReference = new MethodTemplateTypeReference();
                methodTemplateTypeReference.ReadXml(reader);
                return(methodTemplateTypeReference);
            }

            return(null);
        }
        public override IEntity VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data)
        {
            bool         isSingleField = fieldDeclaration.Variables.Count() == 1;
            Modifiers    modifiers     = fieldDeclaration.Modifiers;
            DefaultField field         = null;

            foreach (VariableInitializer vi in fieldDeclaration.Variables)
            {
                field = new DefaultField(currentTypeDefinition, vi.Name);

                field.Region     = isSingleField ? MakeRegion(fieldDeclaration) : MakeRegion(vi);
                field.BodyRegion = MakeRegion(vi);
                ConvertAttributes(field.Attributes, fieldDeclaration.Attributes);

                ApplyModifiers(field, modifiers);
                field.IsVolatile = (modifiers & Modifiers.Volatile) != 0;
                field.IsReadOnly = (modifiers & Modifiers.Readonly) != 0;

                field.ReturnType = ConvertType(fieldDeclaration.ReturnType);
                if ((modifiers & Modifiers.Fixed) != 0)
                {
                    field.ReturnType = PointerTypeReference.Create(field.ReturnType);
                }

                if ((modifiers & Modifiers.Const) != 0)
                {
                    field.ConstantValue = ConvertConstantValue(field.ReturnType, vi.Initializer);
                }

                currentTypeDefinition.Fields.Add(field);
            }
            return(isSingleField ? field : null);
        }
Beispiel #3
0
        public override ITypeReference ToTypeReference(NameLookupMode lookupMode = NameLookupMode.Type)
        {
            ITypeReference t = this.BaseType.ToTypeReference(lookupMode);

            if (this.HasNullableSpecifier)
            {
                t = NullableType.Create(t);
            }
            int pointerRank = this.PointerRank;

            for (int i = 0; i < pointerRank; i++)
            {
                t = new PointerTypeReference(t);
            }
            foreach (var a in this.ArraySpecifiers.Reverse())
            {
                t = new ArrayTypeReference(t, a.Dimensions);
            }
            return(t);
        }
Beispiel #4
0
        public static string GetTypeName(this PointerTypeReference obj, string[] usedNamespaces)
        {
            string retval = obj.ElementType.GetTypeName(usedNamespaces) + "*";

            return(retval.NormaliseClrName(usedNamespaces));
        }
        static ITypeReference ParseTypeName(string typeName, ref int pos)
        {
            string reflectionTypeName = typeName;

            if (pos == typeName.Length)
            {
                throw new ReflectionNameParseException(pos, "Unexpected end");
            }
            ITypeReference result;

            if (reflectionTypeName[pos] == '`')
            {
                // type parameter reference
                pos++;
                if (pos == reflectionTypeName.Length)
                {
                    throw new ReflectionNameParseException(pos, "Unexpected end");
                }
                if (reflectionTypeName[pos] == '`')
                {
                    // method type parameter reference
                    pos++;
                    int index = ReflectionHelper.ReadTypeParameterCount(reflectionTypeName, ref pos);
                    result = TypeParameterReference.Create(SymbolKind.Method, index);
                }
                else
                {
                    // class type parameter reference
                    int index = ReflectionHelper.ReadTypeParameterCount(reflectionTypeName, ref pos);
                    result = TypeParameterReference.Create(SymbolKind.TypeDefinition, index);
                }
            }
            else
            {
                // not a type parameter reference: read the actual type name
                List <ITypeReference> typeArguments = new List <ITypeReference>();
                int    typeParameterCount;
                string typeNameWithoutSuffix = ReadTypeName(typeName, ref pos, true, out typeParameterCount, typeArguments);
                result = new GetPotentiallyNestedClassTypeReference(typeNameWithoutSuffix, typeParameterCount);
                while (pos < typeName.Length && typeName[pos] == '.')
                {
                    pos++;
                    string nestedTypeName = ReadTypeName(typeName, ref pos, false, out typeParameterCount, typeArguments);
                    result = new NestedTypeReference(result, nestedTypeName, typeParameterCount);
                }
                if (typeArguments.Count > 0)
                {
                    result = new ParameterizedTypeReference(result, typeArguments);
                }
            }
            while (pos < typeName.Length)
            {
                switch (typeName[pos])
                {
                case '[':
                    int dimensions = 1;
                    do
                    {
                        pos++;
                        if (pos == typeName.Length)
                        {
                            throw new ReflectionNameParseException(pos, "Unexpected end");
                        }
                        if (typeName[pos] == ',')
                        {
                            dimensions++;
                        }
                    } while (typeName[pos] != ']');
                    result = new ArrayTypeReference(result, dimensions);
                    break;

                case '*':
                    result = new PointerTypeReference(result);
                    break;

                case '@':
                    result = new ByReferenceTypeReference(result);
                    break;

                default:
                    return(result);
                }
                pos++;
            }
            return(result);
        }
Beispiel #6
0
        public AstType ConvertTypeReference(ITypeReference typeRef)
        {
            ArrayTypeReference array = typeRef as ArrayTypeReference;

            if (array != null)
            {
                return(ConvertTypeReference(array.ElementType).MakeArrayType(array.Dimensions));
            }
            PointerTypeReference pointer = typeRef as PointerTypeReference;

            if (pointer != null)
            {
                return(ConvertTypeReference(pointer.ElementType).MakePointerType());
            }
            ByReferenceType brt = typeRef as ByReferenceType;

            if (brt != null)
            {
                return(ConvertTypeReference(brt.ElementType));
            }

            IType type = typeRef.Resolve(context);

            if (type.Kind != TypeKind.Unknown)
            {
                return(ConvertType(type));
            }
            // Unknown type, let's try if we can find an appropriate type
            // (anything is better than displaying a question mark)
            KnownTypeReference knownType = typeRef as KnownTypeReference;

            if (knownType != null)
            {
                string keyword = ReflectionHelper.GetCSharpNameByTypeCode(knownType.TypeCode);
                if (keyword != null)
                {
                    return(new PrimitiveType(keyword));
                }
            }
            SimpleTypeOrNamespaceReference str = typeRef as SimpleTypeOrNamespaceReference;

            if (str != null)
            {
                return(new SimpleType(str.Identifier, str.TypeArguments.Select(ConvertTypeReference)));
            }
            MemberTypeOrNamespaceReference mtr = typeRef as MemberTypeOrNamespaceReference;

            if (mtr != null)
            {
                return(new MemberType(ConvertTypeReference(mtr.Target), mtr.Identifier, mtr.TypeArguments.Select(ConvertTypeReference))
                {
                    IsDoubleColon = mtr.Target is AliasNamespaceReference
                });
            }
            AliasNamespaceReference alias = typeRef as AliasNamespaceReference;

            if (alias != null)
            {
                return(new SimpleType(alias.Identifier));
            }
            // Unknown type reference that couldn't be resolved
            return(new SimpleType("?"));
        }
        static void AppendTypeName(StringBuilder b, ITypeReference type, ITypeResolveContext context)
        {
            IType resolvedType = type as IType;

            if (resolvedType != null)
            {
                AppendTypeName(b, resolvedType);
                return;
            }
            GetClassTypeReference gctr = type as GetClassTypeReference;

            if (gctr != null)
            {
                if (!string.IsNullOrEmpty(gctr.Namespace))
                {
                    b.Append(gctr.Namespace);
                    b.Append('.');
                }
                b.Append(gctr.Name);
                if (gctr.TypeParameterCount > 0)
                {
                    b.Append('`');
                    b.Append(gctr.TypeParameterCount);
                }
                return;
            }
            NestedTypeReference ntr = type as NestedTypeReference;

            if (ntr != null)
            {
                AppendTypeName(b, ntr.DeclaringTypeReference, context);
                b.Append('.');
                b.Append(ntr.Name);
                if (ntr.AdditionalTypeParameterCount > 0)
                {
                    b.Append('`');
                    b.Append(ntr.AdditionalTypeParameterCount);
                }
                return;
            }
            ParameterizedTypeReference pt = type as ParameterizedTypeReference;

            if (pt != null && IsGetClassTypeReference(pt.GenericType))
            {
                AppendParameterizedTypeName(b, pt.GenericType, pt.TypeArguments, context);
                return;
            }
            ArrayTypeReference array = type as ArrayTypeReference;

            if (array != null)
            {
                AppendTypeName(b, array.ElementType, context);
                b.Append('[');
                if (array.Dimensions > 1)
                {
                    for (int i = 0; i < array.Dimensions; i++)
                    {
                        if (i > 0)
                        {
                            b.Append(',');
                        }
                        b.Append("0:");
                    }
                }
                b.Append(']');
                return;
            }
            PointerTypeReference ptr = type as PointerTypeReference;

            if (ptr != null)
            {
                AppendTypeName(b, ptr.ElementType, context);
                b.Append('*');
                return;
            }
            ByReferenceTypeReference brtr = type as ByReferenceTypeReference;

            if (brtr != null)
            {
                AppendTypeName(b, brtr.ElementType, context);
                b.Append('@');
                return;
            }
            if (context == null)
            {
                b.Append('?');
            }
            else
            {
                AppendTypeName(b, type.Resolve(context));
            }
        }
Beispiel #8
0
        public void VisitPointerTypeReference(PointerTypeReference typeReference)
        {
            Formatter.StartNode(typeReference);

            typeReference.BaseType.AcceptVisitor(this);
            Formatter.WriteToken("*");

            Formatter.EndNode();
        }
 private void WritePointerType(PointerTypeReference pointer, ReferenceLinkDisplayOptions options,
                               XmlWriter writer, IDictionary <IndexedTemplateTypeReference, TypeReference> dictionary)
 {
     WriteType(pointer.PointedToType, options, writer, dictionary);
     writer.WriteString("*");
 }
Beispiel #10
0
 public ImmutableNode <string> Visit(PointerTypeReference pointerTypeReference)
 {
     return(new ImmutableNode <string>(pointerTypeReference.ElementType.Accept(this), "*", null));
 }
        internal static ITypeReference ConvertType(AstType type, ITypeDefinition parentTypeDefinition, IMethod parentMethodDefinition, UsingScope parentUsingScope, bool isInUsingDeclaration)
        {
            SimpleType s = type as SimpleType;

            if (s != null)
            {
                List <ITypeReference> typeArguments = new List <ITypeReference>();
                foreach (var ta in s.TypeArguments)
                {
                    typeArguments.Add(ConvertType(ta, parentTypeDefinition, parentMethodDefinition, parentUsingScope, isInUsingDeclaration));
                }
                if (typeArguments.Count == 0 && parentMethodDefinition != null)
                {
                    // SimpleTypeOrNamespaceReference doesn't support method type parameters,
                    // so we directly handle them here.
                    foreach (ITypeParameter tp in parentMethodDefinition.TypeParameters)
                    {
                        if (tp.Name == s.Identifier)
                        {
                            return(tp);
                        }
                    }
                }
                return(new SimpleTypeOrNamespaceReference(s.Identifier, typeArguments, parentTypeDefinition, parentUsingScope, isInUsingDeclaration));
            }

            PrimitiveType p = type as PrimitiveType;

            if (p != null)
            {
                switch (p.Keyword)
                {
                case "string":
                    return(KnownTypeReference.String);

                case "int":
                    return(KnownTypeReference.Int32);

                case "uint":
                    return(KnownTypeReference.UInt32);

                case "object":
                    return(KnownTypeReference.Object);

                case "bool":
                    return(KnownTypeReference.Boolean);

                case "sbyte":
                    return(KnownTypeReference.SByte);

                case "byte":
                    return(KnownTypeReference.Byte);

                case "short":
                    return(KnownTypeReference.Int16);

                case "ushort":
                    return(KnownTypeReference.UInt16);

                case "long":
                    return(KnownTypeReference.Int64);

                case "ulong":
                    return(KnownTypeReference.UInt64);

                case "float":
                    return(KnownTypeReference.Single);

                case "double":
                    return(KnownTypeReference.Double);

                case "decimal":
                    return(ReflectionHelper.ToTypeReference(TypeCode.Decimal));

                case "char":
                    return(KnownTypeReference.Char);

                case "void":
                    return(KnownTypeReference.Void);

                default:
                    return(SharedTypes.UnknownType);
                }
            }
            MemberType m = type as MemberType;

            if (m != null)
            {
                ITypeOrNamespaceReference t;
                if (m.IsDoubleColon)
                {
                    SimpleType st = m.Target as SimpleType;
                    if (st != null)
                    {
                        t = new AliasNamespaceReference(st.Identifier, parentUsingScope);
                    }
                    else
                    {
                        t = null;
                    }
                }
                else
                {
                    t = ConvertType(m.Target, parentTypeDefinition, parentMethodDefinition, parentUsingScope, isInUsingDeclaration) as ITypeOrNamespaceReference;
                }
                if (t == null)
                {
                    return(SharedTypes.UnknownType);
                }
                List <ITypeReference> typeArguments = new List <ITypeReference>();
                foreach (var ta in m.TypeArguments)
                {
                    typeArguments.Add(ConvertType(ta, parentTypeDefinition, parentMethodDefinition, parentUsingScope, isInUsingDeclaration));
                }
                return(new MemberTypeOrNamespaceReference(t, m.MemberName, typeArguments, parentTypeDefinition, parentUsingScope));
            }
            ComposedType c = type as ComposedType;

            if (c != null)
            {
                ITypeReference t = ConvertType(c.BaseType, parentTypeDefinition, parentMethodDefinition, parentUsingScope, isInUsingDeclaration);
                if (c.HasNullableSpecifier)
                {
                    t = NullableType.Create(t);
                }
                for (int i = 0; i < c.PointerRank; i++)
                {
                    t = PointerTypeReference.Create(t);
                }
                foreach (var a in c.ArraySpecifiers.Reverse())
                {
                    t = ArrayTypeReference.Create(t, a.Dimensions);
                }
                return(t);
            }
            Debug.WriteLine("Unknown node used as type: " + type);
            return(SharedTypes.UnknownType);
        }
 public void PointerType()
 {
     var type = new PointerTypeReference(new PrimitiveTypeReference(PrimitiveType.Byte));
     var result = type.Resolve(_compilationUnit.GetScope());
     Assert.IsInstanceOfType(result, typeof(MemberResolveResult));
     Assert.IsInstanceOfType(result.ScopeProvider, typeof(PointerTypeDefinition));
     var pointerType = (PointerTypeDefinition)result.ScopeProvider;
     Assert.AreEqual("System.Byte", pointerType.BaseType.FullName);
 }
Beispiel #13
0
    public InheritanceViaGenericAdapter(TranslatedLibrary library, Adapter targetAdapter, Adapter replacedAdapter)
        : base(targetAdapter)
    {
        // Save and validate our output type
        // (We mostly only validate the replaced adapter since we assume it's well-formed to adapt to this output.)
        OutputType = targetAdapter.InputType;

        if (OutputType is not PointerTypeReference)
        {
            throw new ArgumentException("The target adapter must take a pointer.", nameof(targetAdapter));
        }

        // Determine the actual record type
        TypeReference recordTypeReference = replacedAdapter.InputType;

        if (recordTypeReference is ByRefTypeReference byRefType)
        {
            ByRefKind           = byRefType.Kind;
            recordTypeReference = byRefType.Inner;
        }

        int pointerArity = 0;

        while (recordTypeReference is PointerTypeReference pointerType)
        {
            pointerArity++;
            recordTypeReference = pointerType.Inner;
        }

        if (ByRefKind is null && pointerArity == 0)
        {
            throw new ArgumentException("The replaced adapter must be passed by reference.", nameof(replacedAdapter));
        }

        if (recordTypeReference is not TranslatedTypeReference translatedType)
        {
            throw new ArgumentException("The replaced adapter does not adapt a translated type reference or uses an unrecognized method to do so.", nameof(replacedAdapter));
        }

        if (translatedType.TryResolve(library) is not TranslatedRecord targetRecord)
        {
            throw new ArgumentException("The replaced adapter does not adapt a record type reference.", nameof(replacedAdapter));
        }

        // Determine the constraint info
        ConstraintInterfaceNamespace = $"{targetRecord.Namespace}.{PhysXMarkerInterfacesDeclaration.InfrastructureNamespaceName}";
        ConstraintInterfaceTypeName  = $"I{targetRecord.Name}";

        // Name the generic parameter based on the parameter name
        GenericParameterName = $"T{Char.ToUpperInvariant(replacedAdapter.Name[0])}{replacedAdapter.Name.AsSpan().Slice(1)}";

        // Name our temporary (only used when we're byref)
        TemporaryName = $"__{Name}P";

        // Create the input type
        InputType = new ExternallyDefinedTypeReference(GenericParameterName);

        for (int i = 0; i < pointerArity; i++)
        {
            InputType = new PointerTypeReference(InputType);
        }

        TemporaryType = InputType;

        if (ByRefKind is ByRefKind byRefKind)
        {
            InputType     = new ByRefTypeReference(byRefKind, InputType);
            TemporaryType = new PointerTypeReference(TemporaryType);
        }
    }