BadScript 2
Loading...
Searching...
No Matches
BadInteropStaticCode.cs
Go to the documentation of this file.
1using System.Linq;
2using System.Text;
3
4using Microsoft.CodeAnalysis;
5using Microsoft.CodeAnalysis.Text;
6
8
9public static class BadInteropStaticCode
10{
11 public const string INTEROP_API_ATTRIBUTE = "BadScript2.Interop.BadInteropApiAttribute";
12 public const string INTEROP_OBJECT_ATTRIBUTE = "BadScript2.Interop.BadInteropObjectAttribute";
13 public const string INTEROP_PROPERTY_ATTRIBUTE = "BadScript2.Interop.BadPropertyAttribute";
14 public const string INTEROP_METHOD_ATTRIBUTE = "BadScript2.Interop.BadMethodAttribute";
15 public const string INTEROP_METHOD_PARAMETER_ATTRIBUTE = "BadScript2.Interop.BadParameterAttribute";
16 public const string INTEROP_METHOD_RETURN_ATTRIBUTE = "BadScript2.Interop.BadReturnAttribute";
17
18 public static DiagnosticDescriptor CreateDescriptor(string id,
19 string title,
20 string messageFormat,
21 string category,
22 DiagnosticSeverity severity)
23 {
24 return new DiagnosticDescriptor(id, title, messageFormat, category, severity, true);
25 }
26
27 public static Diagnostic CreateDiagnostic(this ISymbol symbol,
28 string id,
29 string title,
30 string messageFormat,
31 string category,
32 DiagnosticSeverity severity,
33 params object?[]? args)
34 {
35 return CreateDescriptor(id, title, messageFormat, category, severity)
36 .CreateDiagnostic(symbol, args);
37 }
38
39 public static Diagnostic CreateDiagnostic(this DiagnosticDescriptor descriptor,
40 ISymbol symbol,
41 params object?[]? args)
42 {
43 return Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args);
44 }
45
46 public static Diagnostic CreateDiagnostic(this ISymbol symbol,
47 DiagnosticDescriptor descriptor,
48 params object?[]? args)
49 {
50 return Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args);
51 }
52
53 public static AttributeData? GetInteropApiAttribute(this ITypeSymbol symbol)
54 {
55 return symbol.GetAttribute(INTEROP_API_ATTRIBUTE);
56 }
57 public static AttributeData? GetInteropObjectAttribute(this ITypeSymbol symbol)
58 {
59 return symbol.GetAttribute(INTEROP_OBJECT_ATTRIBUTE);
60 }
61
62 public static AttributeData? GetInteropMethodAttribute(this IMethodSymbol symbol)
63 {
64 return symbol.GetAttribute(INTEROP_METHOD_ATTRIBUTE);
65 }
66 public static AttributeData? GetInteropPropertyAttribute(this IPropertySymbol symbol)
67 {
68 return symbol.GetAttribute(INTEROP_PROPERTY_ATTRIBUTE);
69 }
70
71 public static AttributeData? GetReturnTypeAttribute(this IMethodSymbol symbol)
72 {
73 return symbol.GetReturnTypeAttribute(INTEROP_METHOD_RETURN_ATTRIBUTE);
74 }
75
76 public static AttributeData? GetParameterAttribute(this IParameterSymbol symbol)
77 {
78 return symbol.GetAttribute(INTEROP_METHOD_PARAMETER_ATTRIBUTE);
79 }
80
81 public static AttributeData? GetAttribute(this ISymbol symbol, string name)
82 {
83 return symbol.GetAttributes()
84 .FirstOrDefault(x => x.AttributeClass?.ToDisplayString() == name);
85 }
86
87 public static AttributeData? GetReturnTypeAttribute(this IMethodSymbol symbol, string name)
88 {
89 return symbol.GetReturnTypeAttributes()
90 .FirstOrDefault(x => x.AttributeClass?.ToDisplayString() == name);
91 }
92
93 public static void RegisterAttributeSource(IncrementalGeneratorPostInitializationContext context)
94 {
95 context.AddSource("BadInteropAttributes.cs",
96 SourceText.From(@"using System;
97namespace BadScript2.Interop
98{
99 [AttributeUsage(AttributeTargets.Class)]
100 internal sealed class BadInteropApiAttribute : Attribute
101 {
102 public string? Name { get; }
103 public bool ConstructorPrivate { get; }
104 public BadInteropApiAttribute(string? name = null, bool constructorPrivate = false)
105 {
106 Name = name;
107 ConstructorPrivate = constructorPrivate;
108 }
109 }
110 [AttributeUsage(AttributeTargets.Class)]
111 internal sealed class BadInteropObjectAttribute : Attribute
112 {
113 public string? TypeName { get; }
114 public Type? BaseType { get; }
115 public BadInteropObjectAttribute(string? typeName = null, Type? baseType = null)
116 {
117 TypeName = typeName;
118 BaseType = baseType;
119 }
120 }
121
122 [AttributeUsage(AttributeTargets.Method)]
123 internal sealed class BadMethodAttribute : Attribute
124 {
125 public string? Name { get; }
126 public string? Description { get; }
127 public BadMethodAttribute(string? name = null, string? description = null)
128 {
129 Name = name;
130 Description = description;
131 }
132 }
133
134 [AttributeUsage(AttributeTargets.Property)]
135 internal sealed class BadPropertyAttribute : Attribute
136 {
137 public string? Name { get; }
138 public string? Description { get; }
139 public bool ReadOnly { get; }
140 public bool AllowNativeTypes { get; }
141 public BadPropertyAttribute(string? name = null, string? description = null, bool readOnly = false, bool allowNativeTypes = false)
142 {
143 Name = name;
144 Description = description;
145 ReadOnly = readOnly;
146 AllowNativeTypes = allowNativeTypes;
147 }
148 }
149
150 [AttributeUsage(AttributeTargets.Parameter)]
151 internal sealed class BadParameterAttribute : Attribute
152 {
153 public string? Name { get; }
154 public string? Description { get; }
155 public string? NativeType { get; }
156 public BadParameterAttribute(string? name = null, string? description = null, string? nativeType = null)
157 {
158 Name = name;
159 Description = description;
160 NativeType = nativeType;
161 }
162 }
163
164 [AttributeUsage(AttributeTargets.ReturnValue)]
165 internal sealed class BadReturnAttribute : Attribute
166 {
167 public string? Description { get; }
168 public bool AllowNativeTypes { get; }
169 public BadReturnAttribute(string? description = null, bool allowNativeTypes = false)
170 {
171 Description = description;
172 AllowNativeTypes = allowNativeTypes;
173 }
174 }
175
176 internal abstract class BadAutoGeneratedInteropApi : BadScript2.Runtime.Interop.BadInteropApi
177 {
178 protected BadAutoGeneratedInteropApi(string apiName) : base(apiName) { }
179
180 protected virtual void AdditionalData(BadScript2.Runtime.Objects.BadTable target) { }
181 }
182
183}",
184 Encoding.UTF8
185 )
186 );
187 }
188}
static ? AttributeData GetInteropObjectAttribute(this ITypeSymbol symbol)
static ? AttributeData GetInteropMethodAttribute(this IMethodSymbol symbol)
static ? AttributeData GetReturnTypeAttribute(this IMethodSymbol symbol, string name)
static ? AttributeData GetParameterAttribute(this IParameterSymbol symbol)
static DiagnosticDescriptor CreateDescriptor(string id, string title, string messageFormat, string category, DiagnosticSeverity severity)
static Diagnostic CreateDiagnostic(this ISymbol symbol, DiagnosticDescriptor descriptor, params object?[]? args)
static ? AttributeData GetReturnTypeAttribute(this IMethodSymbol symbol)
static Diagnostic CreateDiagnostic(this ISymbol symbol, string id, string title, string messageFormat, string category, DiagnosticSeverity severity, params object?[]? args)
static ? AttributeData GetInteropPropertyAttribute(this IPropertySymbol symbol)
static void RegisterAttributeSource(IncrementalGeneratorPostInitializationContext context)
static Diagnostic CreateDiagnostic(this DiagnosticDescriptor descriptor, ISymbol symbol, params object?[]? args)
static ? AttributeData GetInteropApiAttribute(this ITypeSymbol symbol)
static ? AttributeData GetAttribute(this ISymbol symbol, string name)