예제 #1
0
        protected override void Populate(TextWriter trapFile)
        {
            var info = cx.GetModel(Node).GetSymbolInfo(Node.Name);

            if (Node.StaticKeyword.Kind() == SyntaxKind.None)
            {
                // A normal using
                var namespaceSymbol = info.Symbol as INamespaceSymbol;

                if (namespaceSymbol == null)
                {
                    cx.Extractor.MissingNamespace(Node.Name.ToFullString());
                    cx.ModelError(Node, "Namespace not found");
                    return;
                }
                else
                {
                    var ns = Namespace.Create(cx, namespaceSymbol);
                    trapFile.using_namespace_directives(this, ns);
                    trapFile.using_directive_location(this, cx.Create(ReportingLocation));
                }
            }
            else
            {
                // A "using static"
                Type m = Type.Create(cx, (ITypeSymbol)info.Symbol);
                trapFile.using_static_directives(this, m.TypeRef);
                trapFile.using_directive_location(this, cx.Create(ReportingLocation));
            }

            if (Parent != null)
            {
                trapFile.parent_namespace_declaration(this, Parent);
            }
        }
예제 #2
0
        protected override void Populate(TextWriter trapFile)
        {
            var info = Context.GetModel(node).GetSymbolInfo(node.Name);

            if (node.StaticKeyword.Kind() == SyntaxKind.None)
            {
                // A normal using
                if (info.Symbol is INamespaceSymbol namespaceSymbol)
                {
                    var ns = Namespace.Create(Context, namespaceSymbol);
                    trapFile.using_namespace_directives(this, ns);
                    trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
                }
                else
                {
                    Context.Extractor.MissingNamespace(node.Name.ToFullString(), Context.FromSource);
                    Context.ModelError(node, "Namespace not found");
                    return;
                }
            }
            else
            {
                // A "using static"
                var m = Type.Create(Context, (ITypeSymbol?)info.Symbol);
                trapFile.using_static_directives(this, m.TypeRef);
                trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
            }

            if (parent is not null)
            {
                trapFile.parent_namespace_declaration(this, parent);
            }
        }
예제 #3
0
        public override void Populate()
        {
            Context.Emit(Tuples.types(this, Kinds.TypeKind.DYNAMIC, "dynamic"));
            Context.Emit(Tuples.type_location(this, Location));

            Context.Emit(Tuples.has_modifiers(this, Modifier.Create(Context, "public")));
            Context.Emit(Tuples.parent_namespace(this, Namespace.Create(Context, Context.Compilation.GlobalNamespace)));
        }
예제 #4
0
        public override void Populate(TextWriter trapFile)
        {
            trapFile.types(this, Kinds.TypeKind.DYNAMIC, "dynamic");
            trapFile.type_location(this, Location);

            trapFile.has_modifiers(this, Modifier.Create(Context, "public"));
            trapFile.parent_namespace(this, Namespace.Create(Context, Context.Compilation.GlobalNamespace));
        }
        protected override void Populate(TextWriter trapFile)
        {
            var ns = Namespace.Create(cx, (INamespaceSymbol)cx.GetModel(Node).GetSymbolInfo(Node.Name).Symbol);

            trapFile.namespace_declarations(this, ns);
            trapFile.namespace_declaration_location(this, cx.Create(Node.Name.GetLocation()));

            var visitor = new Populators.TypeOrNamespaceVisitor(cx, trapFile, this);

            foreach (var member in Node.Members.Cast <CSharpSyntaxNode>().Concat(Node.Usings))
            {
                member.Accept(visitor);
            }

            if (Parent != null)
            {
                trapFile.parent_namespace_declaration(this, Parent);
            }
        }
예제 #6
0
        public override void Populate(TextWriter trapFile)
        {
            var @namespace = (INamespaceSymbol)Context.GetModel(node).GetSymbolInfo(node.Name).Symbol;
            var ns         = Namespace.Create(Context, @namespace);

            trapFile.namespace_declarations(this, ns);
            trapFile.namespace_declaration_location(this, Context.CreateLocation(node.Name.GetLocation()));

            var visitor = new Populators.TypeOrNamespaceVisitor(Context, trapFile, this);

            foreach (var member in node.Members.Cast <CSharpSyntaxNode>().Concat(node.Usings))
            {
                member.Accept(visitor);
            }

            if (parent != null)
            {
                trapFile.parent_namespace_declaration(this, parent);
            }
        }
예제 #7
0
        public NamespaceDeclaration(Context cx, NamespaceDeclarationSyntax node, NamespaceDeclaration parent)
            : base(cx)
        {
            var ns = Namespace.Create(cx, (INamespaceSymbol)cx.Model(node).GetSymbolInfo(node.Name).Symbol);

            cx.Emit(Tuples.namespace_declarations(this, ns));
            cx.Emit(Tuples.namespace_declaration_location(this, cx.Create(node.Name.GetLocation())));

            var visitor = new Populators.TypeOrNamespaceVisitor(cx, this);

            foreach (var member in node.Members.Cast <CSharpSyntaxNode>().Concat(node.Usings))
            {
                member.Accept(visitor);
            }

            if (parent != null)
            {
                cx.Emit(Tuples.parent_namespace_declaration(this, parent));
            }
        }
예제 #8
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);
                    }
                }
            }
        }
예제 #9
0
        public UsingDirective(Context cx, UsingDirectiveSyntax usingDirective, NamespaceDeclaration parent)
            : base(cx)
        {
            node = usingDirective;
            var info = cx.Model(node).GetSymbolInfo(usingDirective.Name);

            if (usingDirective.StaticKeyword.Kind() == SyntaxKind.None)
            {
                // A normal using
                var namespaceSymbol = info.Symbol as INamespaceSymbol;

                if (namespaceSymbol == null)
                {
                    cx.Extractor.MissingNamespace(usingDirective.Name.ToFullString());
                    cx.ModelError(usingDirective, "Namespace not found");
                    return;
                }
                else
                {
                    var ns = Namespace.Create(cx, namespaceSymbol);
                    cx.Emit(Tuples.using_namespace_directives(this, ns));
                    cx.Emit(Tuples.using_directive_location(this, cx.Create(ReportingLocation)));
                }
            }
            else
            {
                // A "using static"
                Type m = Type.Create(cx, (ITypeSymbol)info.Symbol);
                cx.Emit(Tuples.using_static_directives(this, m.TypeRef));
                cx.Emit(Tuples.using_directive_location(this, cx.Create(ReportingLocation)));
            }

            if (parent != null)
            {
                cx.Emit(Tuples.parent_namespace_declaration(this, parent));
            }
        }
예제 #10
0
파일: Type.cs 프로젝트: ksg97031/ql
        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();
            }
        }
예제 #11
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);
                    }
                }
            }
        }
예제 #12
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);
                    }
                }
            }
        }
예제 #13
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();
            }
        }