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_METHOD_ATTRIBUTE = "BadScript2.Interop.BadMethodAttribute";
13 public const string INTEROP_METHOD_PARAMETER_ATTRIBUTE = "BadScript2.Interop.BadParameterAttribute";
14 public const string INTEROP_METHOD_RETURN_ATTRIBUTE = "BadScript2.Interop.BadReturnAttribute";
15
16 public static DiagnosticDescriptor CreateDescriptor(string id,
17 string title,
18 string messageFormat,
19 string category,
20 DiagnosticSeverity severity)
21 {
22 return new DiagnosticDescriptor(id, title, messageFormat, category, severity, true);
23 }
24
25 public static Diagnostic CreateDiagnostic(this ISymbol symbol,
26 string id,
27 string title,
28 string messageFormat,
29 string category,
30 DiagnosticSeverity severity,
31 params object?[]? args)
32 {
33 return CreateDescriptor(id, title, messageFormat, category, severity)
34 .CreateDiagnostic(symbol, args);
35 }
36
37 public static Diagnostic CreateDiagnostic(this DiagnosticDescriptor descriptor,
38 ISymbol symbol,
39 params object?[]? args)
40 {
41 return Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args);
42 }
43
44 public static Diagnostic CreateDiagnostic(this ISymbol symbol,
45 DiagnosticDescriptor descriptor,
46 params object?[]? args)
47 {
48 return Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args);
49 }
50
51 public static AttributeData? GetInteropApiAttribute(this ITypeSymbol symbol)
52 {
53 return symbol.GetAttribute(INTEROP_API_ATTRIBUTE);
54 }
55
56 public static AttributeData? GetInteropMethodAttribute(this IMethodSymbol symbol)
57 {
58 return symbol.GetAttribute(INTEROP_METHOD_ATTRIBUTE);
59 }
60
61 public static AttributeData? GetReturnTypeAttribute(this IMethodSymbol symbol)
62 {
63 return symbol.GetReturnTypeAttribute(INTEROP_METHOD_RETURN_ATTRIBUTE);
64 }
65
66 public static AttributeData? GetParameterAttribute(this IParameterSymbol symbol)
67 {
68 return symbol.GetAttribute(INTEROP_METHOD_PARAMETER_ATTRIBUTE);
69 }
70
71 public static AttributeData? GetAttribute(this ISymbol symbol, string name)
72 {
73 return symbol.GetAttributes()
74 .FirstOrDefault(x => x.AttributeClass?.ToDisplayString() == name);
75 }
76
77 public static AttributeData? GetReturnTypeAttribute(this IMethodSymbol symbol, string name)
78 {
79 return symbol.GetReturnTypeAttributes()
80 .FirstOrDefault(x => x.AttributeClass?.ToDisplayString() == name);
81 }
82
83 public static void RegisterAttributeSource(IncrementalGeneratorPostInitializationContext context)
84 {
85 context.AddSource("BadInteropAttributes.cs",
86 SourceText.From(@"using System;
87namespace BadScript2.Interop
88{
89 [AttributeUsage(AttributeTargets.Class)]
90 internal sealed class BadInteropApiAttribute : Attribute
91 {
92 public string? Name { get; }
93 public bool ConstructorPrivate { get; }
94 public BadInteropApiAttribute(string? name = null, bool constructorPrivate = false)
95 {
96 Name = name;
97 ConstructorPrivate = constructorPrivate;
98 }
99 }
100
101 [AttributeUsage(AttributeTargets.Method)]
102 internal sealed class BadMethodAttribute : Attribute
103 {
104 public string? Name { get; }
105 public string? Description { get; }
106 public BadMethodAttribute(string? name = null, string? description = null)
107 {
108 Name = name;
109 Description = description;
110 }
111 }
112
113 [AttributeUsage(AttributeTargets.Parameter)]
114 internal sealed class BadParameterAttribute : Attribute
115 {
116 public string? Name { get; }
117 public string? Description { get; }
118 public string? NativeType { get; }
119 public BadParameterAttribute(string? name = null, string? description = null, string? nativeType = null)
120 {
121 Name = name;
122 Description = description;
123 NativeType = nativeType;
124 }
125 }
126
127 [AttributeUsage(AttributeTargets.ReturnValue)]
128 internal sealed class BadReturnAttribute : Attribute
129 {
130 public string? Description { get; }
131 public bool AllowNativeTypes { get; }
132 public BadReturnAttribute(string? description = null, bool allowNativeTypes = false)
133 {
134 Description = description;
135 AllowNativeTypes = allowNativeTypes;
136 }
137 }
138
139 internal abstract class BadAutoGeneratedInteropApi : BadScript2.Runtime.Interop.BadInteropApi
140 {
141 protected BadAutoGeneratedInteropApi(string apiName) : base(apiName) { }
142
143 protected virtual void AdditionalData(BadScript2.Runtime.Objects.BadTable target) { }
144 }
145}",
146 Encoding.UTF8
147 )
148 );
149 }
150}
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 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)