示例#1
0
        public override void Populate()
        {
            var constraints = new TypeParameterConstraints(Context);

            Context.Emit(Tuples.type_parameter_constraints(constraints, this));

            if (symbol.HasReferenceTypeConstraint)
            {
                Context.Emit(Tuples.general_type_parameter_constraints(constraints, 1));
            }

            if (symbol.HasValueTypeConstraint)
            {
                Context.Emit(Tuples.general_type_parameter_constraints(constraints, 2));
            }

            if (symbol.HasConstructorConstraint)
            {
                Context.Emit(Tuples.general_type_parameter_constraints(constraints, 3));
            }

            ITypeSymbol baseType = symbol.HasValueTypeConstraint ?
                                   Context.Compilation.GetTypeByMetadataName(valueTypeName) :
                                   Context.Compilation.ObjectType;

            foreach (var abase in symbol.ConstraintTypes)
            {
                if (abase.TypeKind != TypeKind.Interface)
                {
                    baseType = abase;
                }
                var t = Create(Context, abase);
                Context.Emit(Tuples.specific_type_parameter_constraints(constraints, t.TypeRef));
            }

            Context.Emit(Tuples.types(this, Semmle.Extraction.Kinds.TypeKind.TYPE_PARAMETER, symbol.Name));
            Context.Emit(Tuples.extend(this, Create(Context, baseType).TypeRef));

            Namespace parentNs = Namespace.Create(Context, symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : symbol.ContainingNamespace);

            Context.Emit(Tuples.parent_namespace(this, parentNs));

            foreach (var l in symbol.Locations)
            {
                Context.Emit(Tuples.type_location(this, Context.Create(l)));
            }

            if (this.IsSourceDeclaration)
            {
                var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).
                                           Select(s => s.Parent).Where(p => p != null).Select(p => p.Parent).ToArray();
                var clauses = declSyntaxReferences.OfType <MethodDeclarationSyntax>().SelectMany(m => m.ConstraintClauses);
                clauses = clauses.Concat(declSyntaxReferences.OfType <ClassDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                clauses = clauses.Concat(declSyntaxReferences.OfType <InterfaceDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                clauses = clauses.Concat(declSyntaxReferences.OfType <StructDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                foreach (var clause in clauses.Where(c => c.Name.Identifier.Text == symbol.Name))
                {
                    TypeMention.Create(Context, clause.Name, this, this);
                    foreach (var constraint in clause.Constraints.OfType <TypeConstraintSyntax>())
                    {
                        var ti     = Context.Model(constraint).GetTypeInfo(constraint.Type);
                        var target = Type.Create(Context, ti.Type);
                        TypeMention.Create(Context, constraint.Type, this, target);
                    }
                }
            }
        }
示例#2
0
        protected void PopulateType(TextWriter trapFile, bool constructUnderlyingTupleType = false)
        {
            PopulateMetadataHandle(trapFile);
            PopulateAttributes();

            trapFile.Write("types(");
            trapFile.WriteColumn(this);
            trapFile.Write(',');
            trapFile.WriteColumn((int)GetClassType(Context, symbol, constructUnderlyingTupleType));
            trapFile.Write(",\"");
            symbol.BuildDisplayName(Context, trapFile, constructUnderlyingTupleType);
            trapFile.WriteLine("\")");

            // Visit base types
            var baseTypes = new List <Type>();

            if (symbol.GetNonObjectBaseType(Context) is INamedTypeSymbol @base)
            {
                var baseKey = Create(Context, @base);
                trapFile.extend(this, baseKey.TypeRef);
                if (symbol.TypeKind != TypeKind.Struct)
                {
                    baseTypes.Add(baseKey);
                }
            }

            if (!(base.symbol is IArrayTypeSymbol))
            {
                foreach (var t in base.symbol.Interfaces.Select(i => Create(Context, i)))
                {
                    trapFile.implement(this, t.TypeRef);
                    baseTypes.Add(t);
                }
            }

            var containingType = ContainingType;

            if (containingType != null && symbol.Kind != SymbolKind.TypeParameter)
            {
                var originalDefinition = symbol.TypeKind == TypeKind.Error ? this : Create(Context, symbol.OriginalDefinition);
                trapFile.nested_types(this, containingType, originalDefinition);
            }
            else if (symbol.ContainingNamespace != null)
            {
                trapFile.parent_namespace(this, Namespace.Create(Context, symbol.ContainingNamespace));
            }

            if (symbol is IArrayTypeSymbol array)
            {
                // They are in the namespace of the original object
                var elementType = array.ElementType;
                var ns          = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;
                if (ns != null)
                {
                    trapFile.parent_namespace(this, Namespace.Create(Context, ns));
                }
            }

            if (symbol is IPointerTypeSymbol pointer)
            {
                var elementType = pointer.PointedAtType;
                var ns          = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;

                if (ns != null)
                {
                    trapFile.parent_namespace(this, Namespace.Create(Context, ns));
                }
            }

            if (symbol.BaseType != null && symbol.BaseType.SpecialType == SpecialType.System_MulticastDelegate)
            {
                // This is a delegate.
                // The method "Invoke" has the return type.
                var invokeMethod = ((INamedTypeSymbol)symbol).DelegateInvokeMethod;
                ExtractParametersForDelegateLikeType(trapFile, invokeMethod,
                                                     t => trapFile.delegate_return_type(this, t));
            }

            if (symbol is IFunctionPointerTypeSymbol functionPointer)
            {
                ExtractParametersForDelegateLikeType(trapFile, functionPointer.Signature,
                                                     t => trapFile.function_pointer_return_type(this, t));
            }

            Modifier.ExtractModifiers(Context, trapFile, this, symbol);

            if (IsSourceDeclaration && symbol.FromSource())
            {
                var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray();

                var baseLists = declSyntaxReferences.OfType <ClassDeclarationSyntax>().Select(c => c.BaseList);
                baseLists = baseLists.Concat(declSyntaxReferences.OfType <InterfaceDeclarationSyntax>().Select(c => c.BaseList));
                baseLists = baseLists.Concat(declSyntaxReferences.OfType <StructDeclarationSyntax>().Select(c => c.BaseList));

                baseLists
                .Where(bl => bl != null)
                .SelectMany(bl => bl.Types)
                .Zip(
                    baseTypes.Where(bt => bt.symbol.SpecialType != SpecialType.System_Object),
                    (s, t) => TypeMention.Create(Context, s.Type, this, t))
                .Enumerate();
            }
        }
示例#3
0
        public override void Populate(TextWriter trapFile)
        {
            var constraints = new TypeParameterConstraints(Context);

            trapFile.type_parameter_constraints(constraints, this);

            if (Symbol.HasReferenceTypeConstraint)
            {
                trapFile.general_type_parameter_constraints(constraints, 1);
            }

            if (Symbol.HasValueTypeConstraint)
            {
                trapFile.general_type_parameter_constraints(constraints, 2);
            }

            if (Symbol.HasConstructorConstraint)
            {
                trapFile.general_type_parameter_constraints(constraints, 3);
            }

            if (Symbol.HasUnmanagedTypeConstraint)
            {
                trapFile.general_type_parameter_constraints(constraints, 4);
            }

            if (Symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated)
            {
                trapFile.general_type_parameter_constraints(constraints, 5);
            }

            foreach (var abase in Symbol.GetAnnotatedTypeConstraints())
            {
                var t = Create(Context, abase.Symbol);
                trapFile.specific_type_parameter_constraints(constraints, t.TypeRef);
                if (!abase.HasObliviousNullability())
                {
                    trapFile.specific_type_parameter_nullability(constraints, t.TypeRef, NullabilityEntity.Create(Context, Nullability.Create(abase)));
                }
            }

            trapFile.types(this, Kinds.TypeKind.TYPE_PARAMETER, Symbol.Name);

            var parentNs = Namespace.Create(Context, Symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : Symbol.ContainingNamespace);

            trapFile.parent_namespace(this, parentNs);

            foreach (var l in Symbol.Locations)
            {
                trapFile.type_location(this, Context.CreateLocation(l));
            }

            if (IsSourceDeclaration)
            {
                var declSyntaxReferences = Symbol.DeclaringSyntaxReferences
                                           .Select(d => d.GetSyntax())
                                           .Select(s => s.Parent)
                                           .Where(p => p is not null)
                                           .Select(p => p !.Parent)
                                           .ToArray();
                var clauses = declSyntaxReferences.OfType <MethodDeclarationSyntax>().SelectMany(m => m.ConstraintClauses);
                clauses = clauses.Concat(declSyntaxReferences.OfType <ClassDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                clauses = clauses.Concat(declSyntaxReferences.OfType <InterfaceDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                clauses = clauses.Concat(declSyntaxReferences.OfType <StructDeclarationSyntax>().SelectMany(c => c.ConstraintClauses));
                foreach (var clause in clauses.Where(c => c.Name.Identifier.Text == Symbol.Name))
                {
                    TypeMention.Create(Context, clause.Name, this, this);
                    foreach (var constraint in clause.Constraints.OfType <TypeConstraintSyntax>())
                    {
                        var ti     = Context.GetModel(constraint).GetTypeInfo(constraint.Type);
                        var target = Type.Create(Context, ti.Type);
                        TypeMention.Create(Context, constraint.Type, this, target);
                    }
                }
            }
        }
示例#4
0
        protected void PopulateType(TextWriter trapFile)
        {
            PopulateMetadataHandle(trapFile);
            PopulateAttributes();

            trapFile.Write("types(");
            trapFile.WriteColumn(this);
            trapFile.Write(',');
            trapFile.WriteColumn((int)GetClassType(Context, symbol));
            trapFile.Write(",\"");
            symbol.BuildDisplayName(Context, trapFile);
            trapFile.WriteLine("\")");

            // Visit base types
            var baseTypes = new List <Type>();

            if (symbol.BaseType != null)
            {
                Type baseKey = Create(Context, symbol.BaseType);
                trapFile.extend(this, baseKey.TypeRef);
                if (symbol.TypeKind != TypeKind.Struct)
                {
                    baseTypes.Add(baseKey);
                }
            }

            if (symbol.TypeKind == TypeKind.Interface)
            {
                trapFile.extend(this, Create(Context, Context.Compilation.ObjectType));
            }

            if (!(base.symbol is IArrayTypeSymbol))
            {
                foreach (var t in base.symbol.Interfaces.Select(i => Create(Context, i)))
                {
                    trapFile.implement(this, t.TypeRef);
                    baseTypes.Add(t);
                }
            }

            var containingType = ContainingType;

            if (containingType != null && symbol.Kind != SymbolKind.TypeParameter)
            {
                Type originalDefinition = symbol.TypeKind == TypeKind.Error ? this : Create(Context, symbol.OriginalDefinition);
                trapFile.nested_types(this, containingType, originalDefinition);
            }
            else if (symbol.ContainingNamespace != null)
            {
                trapFile.parent_namespace(this, Namespace.Create(Context, symbol.ContainingNamespace));
            }

            if (symbol is IArrayTypeSymbol)
            {
                // They are in the namespace of the original object
                ITypeSymbol      elementType = ((IArrayTypeSymbol)symbol).ElementType;
                INamespaceSymbol ns          = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;
                if (ns != null)
                {
                    trapFile.parent_namespace(this, Namespace.Create(Context, ns));
                }
            }

            if (symbol is IPointerTypeSymbol)
            {
                ITypeSymbol      elementType = ((IPointerTypeSymbol)symbol).PointedAtType;
                INamespaceSymbol ns          = elementType.TypeKind == TypeKind.TypeParameter ? Context.Compilation.GlobalNamespace : elementType.ContainingNamespace;

                if (ns != null)
                {
                    trapFile.parent_namespace(this, Namespace.Create(Context, ns));
                }
            }

            if (symbol.BaseType != null && symbol.BaseType.SpecialType == SpecialType.System_MulticastDelegate)
            {
                // This is a delegate.
                // The method "Invoke" has the return type.
                var invokeMethod = ((INamedTypeSymbol)symbol).DelegateInvokeMethod;

                // Copy the parameters from the "Invoke" method to the delegate type
                for (var i = 0; i < invokeMethod.Parameters.Length; ++i)
                {
                    var param               = invokeMethod.Parameters[i];
                    var originalParam       = invokeMethod.OriginalDefinition.Parameters[i];
                    var originalParamEntity = SymbolEqualityComparer.Default.Equals(param, originalParam) ? null :
                                              DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)symbol).OriginalDefinition));
                    DelegateTypeParameter.Create(Context, param, this, originalParamEntity);
                }

                var returnKey = Create(Context, invokeMethod.ReturnType);
                trapFile.delegate_return_type(this, returnKey.TypeRef);
                if (invokeMethod.ReturnsByRef)
                {
                    trapFile.type_annotation(this, Kinds.TypeAnnotation.Ref);
                }
                if (invokeMethod.ReturnsByRefReadonly)
                {
                    trapFile.type_annotation(this, Kinds.TypeAnnotation.ReadonlyRef);
                }
            }

            Modifier.ExtractModifiers(Context, trapFile, this, symbol);

            if (IsSourceDeclaration && symbol.FromSource())
            {
                var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray();

                var baseLists = declSyntaxReferences.OfType <ClassDeclarationSyntax>().Select(c => c.BaseList);
                baseLists = baseLists.Concat(declSyntaxReferences.OfType <InterfaceDeclarationSyntax>().Select(c => c.BaseList));
                baseLists = baseLists.Concat(declSyntaxReferences.OfType <StructDeclarationSyntax>().Select(c => c.BaseList));

                baseLists.
                Where(bl => bl != null).
                SelectMany(bl => bl.Types).
                Zip(baseTypes.Where(bt => bt.symbol.SpecialType != SpecialType.System_Object),
                    (s, t) => TypeMention.Create(Context, s.Type, this, t)).
                Enumerate();
            }
        }