Ejemplo n.º 1
0
        public override void Populate(TextWriter trapFile)
        {
            trapFile.types(this, Kinds.TypeKind.TYPE_PARAMETER, Symbol.Name);

            TypeParameterConstraints.Create(Context, this);

            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);
                    }
                }
            }
        }
Ejemplo n.º 2
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);
                    }
                }
            }
        }
Ejemplo n.º 3
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);
                    }
                }
            }
        }