-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathMetadataHelpers.cs
More file actions
242 lines (190 loc) · 9.86 KB
/
MetadataHelpers.cs
File metadata and controls
242 lines (190 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// MetadataHelpers.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using ScriptSharp.Importer.IL;
using ScriptSharp.ScriptModel;
using ICustomAttributeProvider = ScriptSharp.Importer.IL.ICustomAttributeProvider;
namespace ScriptSharp.Importer {
internal static class MetadataHelpers {
private static CustomAttribute GetAttribute(ICustomAttributeProvider attributeProvider, string attributeTypeName) {
foreach (CustomAttribute attribute in attributeProvider.CustomAttributes) {
if (String.CompareOrdinal(attribute.Constructor.DeclaringType.FullName, attributeTypeName) == 0) {
return attribute;
}
}
return null;
}
private static string GetAttributeArgument(CustomAttribute attribute) {
return attribute.ConstructorArguments[0].Value as string;
}
private static bool GetBoolAttributeArgument(CustomAttribute attribute) {
return (bool)attribute.ConstructorArguments[0].Value;
}
public static string GetScriptAlias(ICustomAttributeProvider attributeProvider) {
CustomAttribute scriptAliasAttribute = GetAttribute(attributeProvider, "System.Runtime.CompilerServices.ScriptAliasAttribute");
if (scriptAliasAttribute != null) {
return GetAttributeArgument(scriptAliasAttribute);
}
return null;
}
public static string GetScriptAssemblyName(ICustomAttributeProvider attributeProvider, out string assemblyIdentifier) {
assemblyIdentifier = null;
CustomAttribute scriptAssemblyAttribute = GetAttribute(attributeProvider, "System.ScriptAssemblyAttribute");
if (scriptAssemblyAttribute != null) {
string name = GetAttributeArgument(scriptAssemblyAttribute);
if (scriptAssemblyAttribute.Properties.Count != 0) {
assemblyIdentifier = (string)scriptAssemblyAttribute.Properties[0].Argument.Value;
}
return name;
}
return null;
}
public static bool GetScriptDefaultMemberCasePreservation(ICustomAttributeProvider attributeProvider) {
CustomAttribute memberCasePreservationAttribute = GetAttribute(attributeProvider, "System.ScriptDefaultMemberCasePreservation");
if (memberCasePreservationAttribute != null) {
return GetBoolAttributeArgument(memberCasePreservationAttribute);
}
return false;
}
public static string GetScriptDependencyName(ICustomAttributeProvider attributeProvider, out string dependencyIdentifier) {
dependencyIdentifier = null;
CustomAttribute scriptDependencyAttribute = GetAttribute(attributeProvider, "System.Runtime.CompilerServices.ScriptDependencyAttribute");
if (scriptDependencyAttribute != null) {
string name = GetAttributeArgument(scriptDependencyAttribute);
if (scriptDependencyAttribute.Properties.Count != 0) {
dependencyIdentifier = (string)scriptDependencyAttribute.Properties[0].Argument.Value;
}
return name;
}
return null;
}
public static bool GetScriptEventAccessors(EventDefinition eventDefinition, out string addAccessor, out string removeAccessor) {
addAccessor = null;
removeAccessor = null;
CustomAttribute eventAttribute = GetAttribute(eventDefinition, "System.Runtime.CompilerServices.ScriptEventAttribute");
if (eventAttribute != null) {
addAccessor = eventAttribute.ConstructorArguments[0].Value as string;
removeAccessor = eventAttribute.ConstructorArguments[1].Value as string;
return true;
}
return false;
}
public static string GetScriptMethodSelector(MethodDefinition method) {
CustomAttribute selectorAttribute = GetAttribute(method, "System.Runtime.CompilerServices.ScriptMethodAttribute");
if (selectorAttribute != null) {
return GetAttributeArgument(selectorAttribute);
}
return null;
}
public static string GetScriptName(ICustomAttributeProvider attributeProvider, bool defaultPreserveCaseValue, out bool preserveName, out bool preserveCase) {
string name = null;
preserveName = defaultPreserveCaseValue;
preserveCase = false;
CustomAttribute nameAttribute = GetAttribute(attributeProvider, "System.ScriptNameAttribute");
if (nameAttribute != null) {
if (nameAttribute.HasConstructorArguments) {
name = GetAttributeArgument(nameAttribute);
}
if (nameAttribute.HasProperties) {
for (int i = 0; i < nameAttribute.Properties.Count; i++) {
if (String.CompareOrdinal(nameAttribute.Properties[i].Name, "PreserveName") == 0) {
preserveName = (bool)nameAttribute.Properties[i].Argument.Value;
}
else {
preserveCase = (bool)nameAttribute.Properties[i].Argument.Value;
}
}
}
}
return name;
}
public static bool IsCompilerGeneratedType(TypeDefinition type) {
return GetAttribute(type, "System.Runtime.CompilerServices.CompilerGeneratedAttribute") != null;
}
public static bool IsDelegate(TypeDefinition type) {
TypeReference baseType = type.BaseType;
if (baseType == null) {
return false;
}
string baseTypeName = type.BaseType.FullName;
return (String.CompareOrdinal(baseTypeName, "System.MulticastDelegate") == 0) ||
(String.CompareOrdinal(baseTypeName, "System.Delegate") == 0);
}
public static bool IsEnum(TypeDefinition type) {
TypeReference baseType = type.BaseType;
if (baseType == null) {
return false;
}
return (String.CompareOrdinal(type.BaseType.FullName, "System.Enum") == 0);
}
public static bool IsScriptExtension(TypeDefinition type, out string extendee) {
extendee = null;
CustomAttribute extensionAttribute = GetAttribute(type, "System.ScriptExtensionAttribute");
if (extensionAttribute != null) {
extendee = GetAttributeArgument(extensionAttribute);
if (String.IsNullOrEmpty(extendee) == false) {
return true;
}
}
return false;
}
public static bool ShouldIgnoreNamespace(TypeDefinition type) {
return GetAttribute(type, "System.Runtime.CompilerServices.ScriptIgnoreNamespaceAttribute") != null;
}
public static bool ShouldImportScriptCoreType(TypeDefinition type) {
return GetAttribute(type, "System.Runtime.CompilerServices.ScriptIgnoreAttribute") == null;
}
public static bool ShouldSkipFromScript(ICustomAttributeProvider attributeProvider) {
return GetAttribute(attributeProvider, "System.Runtime.CompilerServices.ScriptSkipAttribute") != null;
}
public static bool ShouldTreatAsScriptField(PropertyDefinition property) {
return GetAttribute(property, "System.Runtime.CompilerServices.ScriptFieldAttribute") != null;
}
public static bool ShouldTreatAsConditionalMethod(MethodDefinition method, out ICollection<string> conditions) {
conditions = null;
foreach (CustomAttribute attribute in method.CustomAttributes) {
string typeName = attribute.Constructor.DeclaringType.FullName;
if (String.CompareOrdinal(typeName, "System.Diagnostics.ConditionalAttribute") == 0) {
if (conditions == null) {
conditions = new List<string>();
}
conditions.Add(GetAttributeArgument(attribute));
}
}
return (conditions != null);
}
public static bool ShouldTreatAsRecordType(TypeDefinition type) {
return GetAttribute(type, "System.ScriptObjectAttribute") != null;
}
public static bool ShouldUseEnumNames(TypeDefinition type) {
CustomAttribute attribute = GetAttribute(type, "System.ScriptConstantsAttribute");
if (attribute != null) {
if (attribute.HasProperties) {
Debug.Assert(attribute.Properties.Count == 1);
Debug.Assert(String.CompareOrdinal(attribute.Properties[0].Name, "UseNames") == 0);
Debug.Assert(attribute.Properties[0].Argument.Value is bool);
return (bool)attribute.Properties[0].Argument.Value;
}
}
return false;
}
public static bool ShouldUseEnumValues(TypeDefinition type) {
CustomAttribute attribute = GetAttribute(type, "System.ScriptConstantsAttribute");
if (attribute != null) {
if (attribute.HasProperties) {
Debug.Assert(attribute.Properties.Count == 1);
Debug.Assert(String.CompareOrdinal(attribute.Properties[0].Name, "UseNames") == 0);
Debug.Assert(attribute.Properties[0].Argument.Value is bool);
return (bool)attribute.Properties[0].Argument.Value == false;
}
return true;
}
return false;
}
}
}