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