Beispiel #1
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);
            ContainingType.PopulateGenerics();

            var returnType = Type.Create(Context, symbol.ReturnType);

            trapFile.methods(this, Name, ContainingType, returnType.TypeRef, OriginalDefinition);

            if (IsSourceDeclaration)
            {
                foreach (var declaration in symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).OfType <MethodDeclarationSyntax>())
                {
                    Context.BindComments(this, declaration.Identifier.GetLocation());
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
            }

            foreach (var l in Locations)
            {
                trapFile.method_location(this, l);
            }

            PopulateGenerics(trapFile);
            Overrides(trapFile);
            ExtractRefReturn(trapFile, symbol, this);
            ExtractCompilerGenerated(trapFile);
        }
Beispiel #2
0
        public static TypeMention Create(Context cx, TypeSyntax syntax, IEntity parent, Type type, Microsoft.CodeAnalysis.Location?loc = null)
        {
            var ret = new TypeMention(cx, syntax, parent, type, loc);

            cx.Try(syntax, null, () => ret.Populate(cx.TrapWriter.Writer));
            return(ret);
        }
Beispiel #3
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);

            var returnType = Type.Create(Context, Symbol.ReturnType);

            trapFile.operators(this,
                               Symbol.Name,
                               OperatorSymbol(Context, Symbol),
                               ContainingType,
                               returnType.TypeRef,
                               (UserOperator)OriginalDefinition);

            foreach (var l in Locations)
            {
                trapFile.operator_location(this, l);
            }

            if (IsSourceDeclaration)
            {
                var declSyntaxReferences = Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray();
                foreach (var declaration in declSyntaxReferences.OfType <OperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
                foreach (var declaration in declSyntaxReferences.OfType <ConversionOperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.Type, this, returnType);
                }
            }

            ContainingType.PopulateGenerics();
        }
Beispiel #4
0
        public override void Populate()
        {
            PopulateMethod();
            ExtractModifiers();

            var returnType = Type.Create(Context, symbol.ReturnType);

            Context.Emit(Tuples.operators(this,
                                          symbol.Name,
                                          OperatorSymbol(Context, symbol.Name),
                                          ContainingType,
                                          returnType.TypeRef,
                                          (UserOperator)OriginalDefinition));

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

            if (IsSourceDeclaration)
            {
                var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray();
                foreach (var declaration in declSyntaxReferences.OfType <OperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
                foreach (var declaration in declSyntaxReferences.OfType <ConversionOperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.Type, this, returnType);
                }
            }

            ContainingType.ExtractGenerics();
        }
        void ExtractAttribute(System.IO.TextWriter trapFile, AttributeSyntax syntax, ITypeSymbol attributeClass, IEntity entity)
        {
            var type = Type.Create(cx, attributeClass);

            trapFile.attributes(this, type.TypeRef, entity);

            trapFile.attribute_location(this, cx.Create(syntax.Name.GetLocation()));

            if (cx.Extractor.OutputPath != null)
            {
                trapFile.attribute_location(this, Assembly.CreateOutputAssembly(cx));
            }

            TypeMention.Create(cx, syntax.Name, this, type);

            if (syntax.ArgumentList != null)
            {
                cx.PopulateLater(() =>
                {
                    int child = 0;
                    foreach (var arg in syntax.ArgumentList.Arguments)
                    {
                        var expr = Expression.Create(cx, arg.Expression, this, child++);
                        if (!(arg.NameEquals is null))
                        {
                            trapFile.expr_argument_name(expr, arg.NameEquals.Name.Identifier.Text);
                        }
                    }
                });
            }
        }
Beispiel #6
0
        void ExtractAttribute(AttributeSyntax syntax, ITypeSymbol attributeClass, IEntity entity)
        {
            var type = Type.Create(cx, attributeClass);

            cx.Emit(Tuples.attributes(this, type.TypeRef, entity));

            cx.Emit(Tuples.attribute_location(this, cx.Create(syntax.Name.GetLocation())));

            if (cx.Extractor.OutputPath != null)
            {
                cx.Emit(Tuples.attribute_location(this, Assembly.CreateOutputAssembly(cx)));
            }

            TypeMention.Create(cx, syntax.Name, this, type);

            if (syntax.ArgumentList != null)
            {
                cx.PopulateLater(() =>
                {
                    int child = 0;
                    foreach (var arg in syntax.ArgumentList.Arguments)
                    {
                        Expression.Create(cx, arg.Expression, this, child++);
                    }
                    // !! Handle named arguments
                });
            }
        }
Beispiel #7
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateAttributes();
            PopulateNullability(trapFile, Symbol.GetAnnotatedType());
            PopulateRefKind(trapFile, Symbol.RefKind);

            if (Symbol.Name != Original.Symbol.Name)
            {
                Context.ModelError(Symbol, "Inconsistent parameter declaration");
            }

            var type = Type.Create(Context, Symbol.Type);

            trapFile.@params(this, Name, type.TypeRef, Ordinal, ParamKind, Parent !, Original);

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

            if (!Symbol.Locations.Any() &&
                Symbol.ContainingSymbol is IMethodSymbol ms &&
                ms.Name == WellKnownMemberNames.TopLevelStatementsEntryPointMethodName &&
                ms.ContainingType.Name == WellKnownMemberNames.TopLevelStatementsEntryPointTypeName)
            {
                trapFile.param_location(this, Context.CreateLocation());
            }

            if (Symbol.HasExplicitDefaultValue && Context.Defines(Symbol))
            {
                var defaultValueSyntax = GetDefaultValueFromSyntax(Symbol);

                Action defaultValueExpressionCreation = defaultValueSyntax is not null
                    ? () => Expression.Create(Context, defaultValueSyntax.Value, this, 0)
                    : () => Expression.CreateGenerated(Context, Symbol, this, 0, Location);

                Context.PopulateLater(defaultValueExpressionCreation);
            }

            if (!IsSourceDeclaration || !Symbol.FromSource())
            {
                return;
            }

            BindComments();

            if (IsSourceDeclaration)
            {
                foreach (var syntax in Symbol.DeclaringSyntaxReferences
                         .Select(d => d.GetSyntax())
                         .OfType <ParameterSyntax>()
                         .Where(s => s.Type is not null))
                {
                    TypeMention.Create(Context, syntax.Type !, this, type);
                }
            }
        }
        public override void Populate(TextWriter trapFile)
        {
            PopulateNullability(trapFile, Symbol.GetAnnotatedType());

            var type = Type.Create(Context, Symbol.Type);

            trapFile.events(this, Symbol.GetName(), ContainingType !, type.TypeRef, Create(Context, Symbol.OriginalDefinition));

            var adder   = Symbol.AddMethod;
            var remover = Symbol.RemoveMethod;

            if (!(adder is null))
            {
                Method.Create(Context, adder);
            }

            if (!(remover is null))
            {
                Method.Create(Context, remover);
            }

            PopulateModifiers(trapFile);
            BindComments();

            var declSyntaxReferences = IsSourceDeclaration
                ? Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray()
                : Enumerable.Empty <SyntaxNode>();

            foreach (var explicitInterface in Symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                trapFile.explicitly_implements(this, explicitInterface.TypeRef);

                foreach (var syntax in declSyntaxReferences.OfType <EventDeclarationSyntax>())
                {
                    TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier !.Name, this, explicitInterface);
                }
            }

            foreach (var l in Locations)
            {
                trapFile.event_location(this, l);
            }

            foreach (var syntaxType in declSyntaxReferences
                     .OfType <VariableDeclaratorSyntax>()
                     .Select(d => d.Parent)
                     .OfType <VariableDeclarationSyntax>()
                     .Select(syntax => syntax.Type))
            {
                TypeMention.Create(Context, syntaxType, this, type);
            }
        }
Beispiel #9
0
Datei: Event.cs Projekt: s0/ql
        public override void Populate()
        {
            var type = Type.Create(Context, symbol.Type);

            Context.Emit(Tuples.events(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)));

            var adder = symbol.AddMethod;

            if (adder != null)
            {
                EventAccessor.Create(Context, adder);
            }

            var remover = symbol.RemoveMethod;

            if (remover != null)
            {
                EventAccessor.Create(Context, remover);
            }

            ExtractModifiers();
            BindComments();

            var declSyntaxReferences = IsSourceDeclaration
                ? symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray()
                : Enumerable.Empty <SyntaxNode>();

            foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                Context.Emit(Tuples.explicitly_implements(this, explicitInterface.TypeRef));

                foreach (var syntax in declSyntaxReferences.OfType <EventDeclarationSyntax>())
                {
                    TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface);
                }
            }

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

            foreach (var syntaxType in declSyntaxReferences.OfType <VariableDeclaratorSyntax>().
                     Select(d => d.Parent).
                     OfType <VariableDeclarationSyntax>().
                     Select(syntax => syntax.Type))
            {
                TypeMention.Create(Context, syntaxType, this, type);
            }
        }
Beispiel #10
0
        public override void Populate(TextWriter trapFile)
        {
            var type = Type.Create(Context, symbol.AttributeClass);

            trapFile.attributes(this, type.TypeRef, entity);
            trapFile.attribute_location(this, Location);

            if (attributeSyntax is object)
            {
                if (Context.Extractor.OutputPath != null)
                {
                    trapFile.attribute_location(this, Assembly.CreateOutputAssembly(Context));
                }

                TypeMention.Create(Context, attributeSyntax.Name, this, type);
            }

            ExtractArguments(trapFile);
        }
Beispiel #11
0
        public override void Populate(TextWriter trapFile)
        {
            var type = Type.Create(Context, Symbol.AttributeClass);

            trapFile.attributes(this, kind, type.TypeRef, entity);
            trapFile.attribute_location(this, Location);

            if (attributeSyntax is not null)
            {
                if (!Context.Extractor.Mode.HasFlag(ExtractorMode.Standalone))
                {
                    trapFile.attribute_location(this, Assembly.CreateOutputAssembly(Context));
                }

                TypeMention.Create(Context, attributeSyntax.Name, this, type);
            }

            ExtractArguments(trapFile);
        }
Beispiel #12
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);
                    }
                }
            }
        }
Beispiel #13
0
        public void Overrides()
        {
            foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.
                     Where(sym => sym.MethodKind == MethodKind.Ordinary).
                     Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                Context.Emit(Tuples.explicitly_implements(this, explicitInterface.TypeRef));

                if (IsSourceDeclaration)
                {
                    foreach (var syntax in symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).OfType <MethodDeclarationSyntax>())
                    {
                        TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface);
                    }
                }
            }

            if (symbol.OverriddenMethod != null)
            {
                Context.Emit(Tuples.overrides(this, Method.Create(Context, symbol.OverriddenMethod)));
            }
        }
Beispiel #14
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);
                    }
                }
            }
        }
Beispiel #15
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();
            }
        }
Beispiel #16
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMetadataHandle(trapFile);
            PopulateAttributes();
            PopulateModifiers(trapFile);
            BindComments();
            PopulateNullability(trapFile, symbol.GetAnnotatedType());
            PopulateRefKind(trapFile, symbol.RefKind);

            var type = Type;

            trapFile.properties(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition));

            var getter = symbol.GetMethod;
            var setter = symbol.SetMethod;

            if (!(getter is null))
            {
                Method.Create(Context, getter);
            }

            if (!(setter is null))
            {
                Method.Create(Context, setter);
            }

            var declSyntaxReferences = IsSourceDeclaration ?
                                       symbol.DeclaringSyntaxReferences.
                                       Select(d => d.GetSyntax()).OfType <PropertyDeclarationSyntax>().ToArray()
                : Enumerable.Empty <PropertyDeclarationSyntax>();

            foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                trapFile.explicitly_implements(this, explicitInterface.TypeRef);

                foreach (var syntax in declSyntaxReferences)
                {
                    TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface);
                }
            }

            foreach (var l in Locations)
            {
                trapFile.property_location(this, l);
            }

            if (IsSourceDeclaration && symbol.FromSource())
            {
                var expressionBody = ExpressionBody;
                if (expressionBody != null)
                {
                    Context.PopulateLater(() => Expression.Create(Context, expressionBody, this, 0));
                }

                var child = 1;
                foreach (var initializer in declSyntaxReferences
                         .Select(n => n.Initializer)
                         .Where(i => i != null))
                {
                    Context.PopulateLater(() =>
                    {
                        var loc              = Context.CreateLocation(initializer.GetLocation());
                        var annotatedType    = AnnotatedTypeSymbol.CreateNotAnnotated(symbol.Type);
                        var simpleAssignExpr = new Expression(new ExpressionInfo(Context, annotatedType, loc, ExprKind.SIMPLE_ASSIGN, this, child++, false, null));
                        Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Value, simpleAssignExpr, 0));
                        var access = new Expression(new ExpressionInfo(Context, annotatedType, Location, ExprKind.PROPERTY_ACCESS, simpleAssignExpr, 1, false, null));
                        trapFile.expr_access(access, this);
                        if (!symbol.IsStatic)
                        {
                            This.CreateImplicit(Context, symbol.ContainingType, Location, access, -1);
                        }
                    });
                }

                foreach (var syntax in declSyntaxReferences)
                {
                    TypeMention.Create(Context, syntax.Type, this, type);
                }
            }
        }
Beispiel #17
0
        public override void Populate()
        {
            ExtractAttributes();
            ExtractModifiers();
            BindComments();

            var type = Type.Create(Context, symbol.Type);

            Context.Emit(Tuples.properties(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)));

            var getter = symbol.GetMethod;

            if (getter != null)
            {
                Getter = Accessor.Create(Context, getter);
            }

            var setter = symbol.SetMethod;

            if (setter != null)
            {
                Setter = Accessor.Create(Context, setter);
            }

            var declSyntaxReferences = IsSourceDeclaration ?
                                       symbol.DeclaringSyntaxReferences.
                                       Select(d => d.GetSyntax()).OfType <PropertyDeclarationSyntax>().ToArray()
                : Enumerable.Empty <PropertyDeclarationSyntax>();

            foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                Context.Emit(Tuples.explicitly_implements(this, explicitInterface.TypeRef));

                foreach (var syntax in declSyntaxReferences)
                {
                    TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface);
                }
            }

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

            if (IsSourceDeclaration && symbol.FromSource())
            {
                var expressionBody = ExpressionBody;
                if (expressionBody != null)
                {
                    Context.PopulateLater(() => Expression.Create(Context, expressionBody, this, 0));
                }

                foreach (var initializer in declSyntaxReferences.
                         Select(n => n.Initializer).
                         Where(i => i != null).
                         Select(i => i.Value))
                {
                    Context.PopulateLater(() => Expression.Create(Context, initializer, this, 1));
                }

                foreach (var syntax in declSyntaxReferences)
                {
                    TypeMention.Create(Context, syntax.Type, this, type);
                }
            }
        }
Beispiel #18
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateAttributes();
            PopulateNullability(trapFile, symbol.GetAnnotatedType());
            PopulateRefKind(trapFile, symbol.RefKind);

            if (symbol.Name != Original.symbol.Name)
            {
                Context.ModelError(symbol, "Inconsistent parameter declaration");
            }

            var type = Type.Create(Context, symbol.Type);

            trapFile.@params(this, Name, type.TypeRef, Ordinal, ParamKind, Parent, Original);

            foreach (var l in symbol.Locations)
            {
                trapFile.param_location(this, Context.Create(l));
            }

            if (!IsSourceDeclaration || !symbol.FromSource())
            {
                return;
            }

            BindComments();

            if (IsSourceDeclaration)
            {
                foreach (var syntax in symbol.DeclaringSyntaxReferences.
                         Select(d => d.GetSyntax()).
                         OfType <ParameterSyntax>().
                         Where(s => s.Type != null))
                {
                    TypeMention.Create(Context, syntax.Type, this, type);
                }
            }

            if (symbol.HasExplicitDefaultValue && Context.Defines(symbol))
            {
                // This is a slight bug in the dbscheme
                // We should really define param_default(param, string)
                // And use parameter child #0 to encode the default expression.
                var defaultValue = GetParameterDefaultValue(symbol);
                if (defaultValue == null)
                {
                    // In case this parameter belongs to an accessor of an indexer, we need
                    // to get the default value from the corresponding parameter belonging
                    // to the indexer itself
                    var method = (IMethodSymbol)symbol.ContainingSymbol;
                    if (method != null)
                    {
                        var i       = method.Parameters.IndexOf(symbol);
                        var indexer = (IPropertySymbol)method.AssociatedSymbol;
                        if (indexer != null)
                        {
                            defaultValue = GetParameterDefaultValue(indexer.Parameters[i]);
                        }
                    }
                }

                if (defaultValue != null)
                {
                    Context.PopulateLater(() =>
                    {
                        Expression.Create(Context, defaultValue.Value, this, 0);
                    });
                }
            }
        }
Beispiel #19
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();
            }
        }
Beispiel #20
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMetadataHandle(trapFile);
            PopulateAttributes();
            ContainingType !.PopulateGenerics();
            PopulateNullability(trapFile, Symbol.GetAnnotatedType());

            var unboundFieldKey = Field.Create(Context, Symbol.OriginalDefinition);

            trapFile.fields(this, (Symbol.IsConst ? 2 : 1), Symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey);

            PopulateModifiers(trapFile);

            if (Symbol.IsVolatile)
            {
                Modifier.HasModifier(Context, trapFile, this, "volatile");
            }

            if (Symbol.IsConst)
            {
                Modifier.HasModifier(Context, trapFile, this, "const");

                if (Symbol.HasConstantValue)
                {
                    trapFile.constant_value(this, Expression.ValueAsString(Symbol.ConstantValue));
                }
            }

            foreach (var l in Locations)
            {
                trapFile.field_location(this, l);
            }

            if (!IsSourceDeclaration || !Symbol.FromSource())
            {
                return;
            }

            Context.BindComments(this, Location.Symbol);

            var child = 0;

            foreach (var initializer in Symbol.DeclaringSyntaxReferences
                     .Select(n => n.GetSyntax())
                     .OfType <VariableDeclaratorSyntax>()
                     .Where(n => n.Initializer is not null))
            {
                Context.PopulateLater(() =>
                {
                    var loc = Context.CreateLocation(initializer.GetLocation());

                    var fieldAccess = AddInitializerAssignment(trapFile, initializer.Initializer !.Value, loc, null, ref child);

                    if (!Symbol.IsStatic)
                    {
                        This.CreateImplicit(Context, Symbol.ContainingType, Location, fieldAccess, -1);
                    }
                });
            }

            foreach (var initializer in Symbol.DeclaringSyntaxReferences
                     .Select(n => n.GetSyntax())
                     .OfType <EnumMemberDeclarationSyntax>()
                     .Where(n => n.EqualsValue is not null))
            {
                // Mark fields that have explicit initializers.
                var constValue = Symbol.HasConstantValue
                    ? Expression.ValueAsString(Symbol.ConstantValue)
                    : null;

                var loc = Context.CreateLocation(initializer.GetLocation());

                AddInitializerAssignment(trapFile, initializer.EqualsValue !.Value, loc, constValue, ref child);
            }

            if (IsSourceDeclaration)
            {
                foreach (var syntax in Symbol.DeclaringSyntaxReferences
                         .Select(d => d.GetSyntax())
                         .OfType <VariableDeclaratorSyntax>()
                         .Select(d => d.Parent)
                         .OfType <VariableDeclarationSyntax>())
                {
                    TypeMention.Create(Context, syntax.Type, this, Type);
                }
            }
        }
Beispiel #21
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);
                    }
                }
            }
        }
Beispiel #22
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMetadataHandle(trapFile);
            PopulateAttributes();
            ContainingType.PopulateGenerics();
            PopulateNullability(trapFile, symbol.NullableAnnotation);

            Field unboundFieldKey = Field.Create(Context, symbol.OriginalDefinition);

            trapFile.fields(this, (symbol.IsConst ? 2 : 1), symbol.Name, ContainingType, Type.Type.TypeRef, unboundFieldKey);

            PopulateModifiers(trapFile);

            if (symbol.IsVolatile)
            {
                Modifier.HasModifier(Context, trapFile, this, "volatile");
            }

            if (symbol.IsConst)
            {
                Modifier.HasModifier(Context, trapFile, this, "const");

                if (symbol.HasConstantValue)
                {
                    trapFile.constant_value(this, Expression.ValueAsString(symbol.ConstantValue));
                }
            }

            foreach (var l in Locations)
            {
                trapFile.field_location(this, l);
            }

            if (!IsSourceDeclaration || !symbol.FromSource())
            {
                return;
            }

            Context.BindComments(this, Location.symbol);

            int child = 0;

            foreach (var initializer in
                     symbol.DeclaringSyntaxReferences.
                     Select(n => n.GetSyntax()).
                     OfType <VariableDeclaratorSyntax>().
                     Where(n => n.Initializer != null))
            {
                Context.PopulateLater(() =>
                {
                    var loc = Context.Create(initializer.GetLocation());
                    var simpleAssignExpr = new Expression(new ExpressionInfo(Context, Type, loc, ExprKind.SIMPLE_ASSIGN, this, child++, false, null));
                    Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Initializer.Value, simpleAssignExpr, 0));
                    var access = new Expression(new ExpressionInfo(Context, Type, Location, ExprKind.FIELD_ACCESS, simpleAssignExpr, 1, false, null));
                    trapFile.expr_access(access, this);
                    if (!symbol.IsStatic)
                    {
                        This.CreateImplicit(Context, Entities.Type.Create(Context, symbol.ContainingType), Location, access, -1);
                    }
                });
            }

            foreach (var initializer in symbol.DeclaringSyntaxReferences.
                     Select(n => n.GetSyntax()).
                     OfType <EnumMemberDeclarationSyntax>().
                     Where(n => n.EqualsValue != null))
            {
                // Mark fields that have explicit initializers.
                var expr = new Expression(new ExpressionInfo(Context, Type, Context.Create(initializer.EqualsValue.Value.FixedLocation()), Kinds.ExprKind.FIELD_ACCESS, this, child++, false, null));
                trapFile.expr_access(expr, this);
            }

            if (IsSourceDeclaration)
            {
                foreach (var syntax in symbol.DeclaringSyntaxReferences.
                         Select(d => d.GetSyntax()).OfType <VariableDeclaratorSyntax>().
                         Select(d => d.Parent).OfType <VariableDeclarationSyntax>())
                {
                    TypeMention.Create(Context, syntax.Type, this, Type);
                }
            }
        }
Beispiel #23
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateNullability(trapFile, symbol.GetAnnotatedType());

            var type = Type.Create(Context, symbol.Type);

            trapFile.indexers(this, symbol.GetName(useMetadataName: true), ContainingType, type.TypeRef, OriginalDefinition);
            foreach (var l in Locations)
            {
                trapFile.indexer_location(this, l);
            }

            var getter = symbol.GetMethod;
            var setter = symbol.SetMethod;

            if (getter is null && setter is null)
            {
                Context.ModelError(symbol, "No indexer accessor defined");
            }

            if (!(getter is null))
            {
                Method.Create(Context, getter);
            }

            if (!(setter is null))
            {
                Method.Create(Context, setter);
            }

            for (var i = 0; i < symbol.Parameters.Length; ++i)
            {
                var original = Parameter.Create(Context, symbol.OriginalDefinition.Parameters[i], OriginalDefinition);
                Parameter.Create(Context, symbol.Parameters[i], this, original);
            }

            if (IsSourceDeclaration)
            {
                var expressionBody = ExpressionBody;
                if (expressionBody != null)
                {
                    // The expression may need to reference parameters in the getter.
                    // So we need to arrange that the expression is populated after the getter.
                    Context.PopulateLater(() => Expression.CreateFromNode(new ExpressionNodeInfo(Context, expressionBody, this, 0)
                    {
                        Type = Type.Create(Context, symbol.GetAnnotatedType())
                    }));
                }
            }

            PopulateModifiers(trapFile);
            BindComments();

            var declSyntaxReferences = IsSourceDeclaration
                ? symbol.DeclaringSyntaxReferences.
                                       Select(d => d.GetSyntax()).OfType <IndexerDeclarationSyntax>().ToArray()
                : Enumerable.Empty <IndexerDeclarationSyntax>();

            foreach (var explicitInterface in symbol.ExplicitInterfaceImplementations.Select(impl => Type.Create(Context, impl.ContainingType)))
            {
                trapFile.explicitly_implements(this, explicitInterface.TypeRef);

                foreach (var syntax in declSyntaxReferences)
                {
                    TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier.Name, this, explicitInterface);
                }
            }


            foreach (var syntax in declSyntaxReferences)
            {
                TypeMention.Create(Context, syntax.Type, this, type);
            }
        }
Beispiel #24
0
        public override void Populate()
        {
            ExtractMetadataHandle();
            ExtractAttributes();
            ContainingType.ExtractGenerics();

            Field unboundFieldKey = Field.Create(Context, symbol.OriginalDefinition);

            Context.Emit(Tuples.fields(this, (symbol.IsConst ? 2 : 1), symbol.Name, ContainingType, Type.TypeRef, unboundFieldKey));

            ExtractModifiers();

            if (symbol.IsVolatile)
            {
                Modifier.HasModifier(Context, this, "volatile");
            }

            if (symbol.IsConst)
            {
                Modifier.HasModifier(Context, this, "const");

                if (symbol.HasConstantValue)
                {
                    Context.Emit(Tuples.constant_value(this, Expression.ValueAsString(symbol.ConstantValue)));
                }
            }

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

            if (!IsSourceDeclaration || !symbol.FromSource())
            {
                return;
            }

            Context.BindComments(this, Location.symbol);

            int child = 0;

            foreach (var initializer in
                     symbol.DeclaringSyntaxReferences.
                     Select(n => n.GetSyntax()).
                     OfType <VariableDeclaratorSyntax>().
                     Where(n => n.Initializer != null))
            {
                Context.PopulateLater(() =>
                {
                    Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Initializer.Value, this, child++));
                });
            }

            foreach (var initializer in symbol.DeclaringSyntaxReferences.
                     Select(n => n.GetSyntax()).
                     OfType <EnumMemberDeclarationSyntax>().
                     Where(n => n.EqualsValue != null))
            {
                // Mark fields that have explicit initializers.
                var expr = new Expression(new ExpressionInfo(Context, Type, Context.Create(initializer.EqualsValue.Value.FixedLocation()), Kinds.ExprKind.FIELD_ACCESS, this, child++, false, null));
                Context.Emit(Tuples.expr_access(expr, this));
            }

            if (IsSourceDeclaration)
            {
                foreach (var syntax in symbol.DeclaringSyntaxReferences.
                         Select(d => d.GetSyntax()).OfType <VariableDeclaratorSyntax>().
                         Select(d => d.Parent).OfType <VariableDeclarationSyntax>())
                {
                    TypeMention.Create(Context, syntax.Type, this, Type);
                }
            }
        }