public override void Populate(TextWriter trapFile) { PopulateMethod(trapFile); // There is a "bug" in Roslyn whereby the IMethodSymbol associated with the local function symbol // is always static, so we need to go to the syntax reference of the local function to see whether // the "static" modifier is present. if (symbol.DeclaringSyntaxReferences.SingleOrDefault().GetSyntax() is LocalFunctionStatementSyntax fn) { foreach (var modifier in fn.Modifiers) { Modifier.HasModifier(Context, trapFile, this, modifier.Text); } } var originalDefinition = IsSourceDeclaration ? this : Create(Context, symbol.OriginalDefinition); var returnType = Type.Create(Context, symbol.ReturnType); trapFile.local_functions(this, symbol.Name, returnType, originalDefinition); ExtractRefReturn(trapFile); }
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); } } }
public static void ExtractModifiers(Context cx, IEntity key, ISymbol symbol) { bool interfaceDefinition = symbol.ContainingType != null && symbol.ContainingType.Kind == SymbolKind.NamedType && symbol.ContainingType.TypeKind == TypeKind.Interface; Modifier.HasAccessibility(cx, key, symbol.DeclaredAccessibility); if (symbol.Kind == SymbolKind.ErrorType) { cx.Emit(Tuples.has_modifiers(key, Modifier.Create(cx, Accessibility.Public))); } if (symbol.IsAbstract && (symbol.Kind != SymbolKind.NamedType || ((INamedTypeSymbol)symbol).TypeKind != TypeKind.Interface) && !interfaceDefinition) { Modifier.HasModifier(cx, key, "abstract"); } if (symbol.IsSealed) { HasModifier(cx, key, "sealed"); } bool fromSource = symbol.DeclaringSyntaxReferences.Length > 0; if (symbol.IsStatic && !(symbol.Kind == SymbolKind.Field && ((IFieldSymbol)symbol).IsConst && !fromSource)) { HasModifier(cx, key, "static"); } if (symbol.IsVirtual) { HasModifier(cx, key, "virtual"); } // For some reason, method in interfaces are "virtual", not "abstract" if (symbol.IsAbstract && interfaceDefinition) { HasModifier(cx, key, "virtual"); } if (symbol.Kind == SymbolKind.Field && ((IFieldSymbol)symbol).IsReadOnly) { HasModifier(cx, key, "readonly"); } if (symbol.IsOverride) { HasModifier(cx, key, "override"); } if (symbol.Kind == SymbolKind.Method && ((IMethodSymbol)symbol).IsAsync) { HasModifier(cx, key, "async"); } if (symbol.IsExtern) { HasModifier(cx, key, "extern"); } foreach (var modifier in symbol.GetSourceLevelModifiers()) { HasModifier(cx, key, modifier); } if (symbol.Kind == SymbolKind.NamedType) { INamedTypeSymbol nt = symbol as INamedTypeSymbol; if (nt is null) { throw new InternalError(symbol, "Symbol kind is inconsistent with its type"); } if (nt.TypeKind == TypeKind.Struct) { if (nt.IsReadOnly) { HasModifier(cx, key, "readonly"); } if (nt.IsRefLikeType) { HasModifier(cx, key, "ref"); } } } }
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); } } }
public static void ExtractModifiers(Context cx, IEntity key, ISymbol symbol) { bool interfaceDefinition = symbol.ContainingType != null && symbol.ContainingType.Kind == SymbolKind.NamedType && symbol.ContainingType.TypeKind == TypeKind.Interface; Modifier.HasAccessibility(cx, key, symbol.DeclaredAccessibility); if (symbol.Kind == SymbolKind.ErrorType) { cx.Emit(Tuples.has_modifiers(key, Modifier.Create(cx, Accessibility.Public))); } if (symbol.IsAbstract && (symbol.Kind != SymbolKind.NamedType || ((INamedTypeSymbol)symbol).TypeKind != TypeKind.Interface) && !interfaceDefinition) { Modifier.HasModifier(cx, key, "abstract"); } if (symbol.IsSealed) { HasModifier(cx, key, "sealed"); } bool fromSource = symbol.DeclaringSyntaxReferences.Length > 0; if (symbol.IsStatic && !(symbol.Kind == SymbolKind.Field && ((IFieldSymbol)symbol).IsConst && !fromSource)) { HasModifier(cx, key, "static"); } if (symbol.IsVirtual) { HasModifier(cx, key, "virtual"); } // For some reason, method in interfaces are "virtual", not "abstract" if (symbol.IsAbstract && interfaceDefinition) { HasModifier(cx, key, "virtual"); } if (symbol.Kind == SymbolKind.Field && ((IFieldSymbol)symbol).IsReadOnly) { HasModifier(cx, key, "readonly"); } if (symbol.IsOverride) { HasModifier(cx, key, "override"); } if (symbol.Kind == SymbolKind.Method && ((IMethodSymbol)symbol).IsAsync) { HasModifier(cx, key, "async"); } if (symbol.IsExtern) { HasModifier(cx, key, "extern"); } foreach (var modifier in symbol.GetSourceLevelModifiers()) { HasModifier(cx, key, modifier); } if (symbol.Kind == SymbolKind.NamedType) { INamedTypeSymbol nt = symbol as INamedTypeSymbol; if (nt.TypeKind == TypeKind.Struct) { // Sadly, these properties are internal so cannot be accessed directly. // This seems to be a deficiency in the model. var readonlyProperty = nt.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance); var isByRefProperty = nt.GetType().GetProperty("IsByRefLikeType", BindingFlags.NonPublic | BindingFlags.Instance); bool isReadOnly = (bool)readonlyProperty.GetValue(nt); bool isByRefLikeType = (bool)isByRefProperty.GetValue(nt); if (isReadOnly) { HasModifier(cx, key, "readonly"); } if (isByRefLikeType) { HasModifier(cx, key, "ref"); } } } }
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); } } }