1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | //***************************************************************************** |
5 | // ImportHelper.h |
6 | // |
7 | |
8 | // |
9 | // contains utility code to MD directory |
10 | // |
11 | //***************************************************************************** |
12 | #ifndef __IMPORTHELPER__h__ |
13 | #define __IMPORTHELPER__h__ |
14 | |
15 | class CMiniMdRW; |
16 | class MDTOKENMAP; |
17 | |
18 | //********************************************************************* |
19 | // Class to handle merge |
20 | //********************************************************************* |
21 | class ImportHelper |
22 | { |
23 | public: |
24 | // Options for code:FindMemberRef. |
25 | enum HashSearchOption |
26 | { |
27 | DoNotCreateHash, // Do not create hash if it does not exist (faster for isolated calls) |
28 | CreateHash // Create hash if it does not exist (faster for multiple calls) |
29 | }; |
30 | |
31 | |
32 | static HRESULT FindMethodSpecByMethodAndInstantiation( |
33 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
34 | /*mdMethodDefOrRef*/ mdToken tkMethod, // [IN] MethodSpec method field |
35 | PCCOR_SIGNATURE pInstantiation, // [IN] MethodSpec instantiation (a signature) |
36 | ULONG cbInstantiation, // [IN] Size of instantiation. |
37 | mdMethodSpec *pMethodSpec, // [OUT] Put the MethodSpec token here. |
38 | RID rid = 0); // [IN] Optional rid to be ignored. |
39 | |
40 | |
41 | static HRESULT FindGenericParamConstraintByOwnerAndConstraint( |
42 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
43 | mdGenericParam tkOwner, // [IN] GenericParamConstraint Owner |
44 | mdToken tkConstraint, // [IN] GenericParamConstraint Constraint |
45 | mdGenericParamConstraint *pGenericParamConstraint, // [OUT] Put the GenericParamConstraint token here. |
46 | RID rid = 0); // [IN] Optional rid to be ignored. |
47 | |
48 | |
49 | static HRESULT FindGenericParamByOwner( |
50 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
51 | mdToken tkOwner, // [IN] GenericParam Owner |
52 | LPCUTF8 szUTF8Name, // [IN] GeneriParam Name, may be NULL if not used for search |
53 | ULONG *pNumber, // [IN] GeneriParam Number, may be NULL if not used for search |
54 | mdGenericParam *pGenericParam, // [OUT] Put the GenericParam token here. |
55 | RID rid = 0); // [IN] Optional rid to be ignored. |
56 | |
57 | static HRESULT FindMethod( |
58 | CMiniMdRW * pMiniMd, // [IN] the minimd to lookup |
59 | mdTypeDef td, // [IN] parent. |
60 | LPCUTF8 szName, // [IN] MethodDef name. |
61 | PCCOR_SIGNATURE pSig, // [IN] Signature. |
62 | ULONG cbSig, // [IN] Size of signature. |
63 | mdMethodDef * pmb, // [OUT] Put the MethodDef token here. |
64 | RID rid = 0, // [IN] Optional rid to be ignored. |
65 | PSIGCOMPARE pSignatureCompare = NULL, // [IN] Optional Routine to compare signatures |
66 | void * pCompareContext = NULL); // [IN] Optional context for the compare function |
67 | |
68 | static HRESULT FindField( |
69 | CMiniMdRW * pMiniMd, // [IN] the minimd to lookup |
70 | mdTypeDef td, // [IN] parent. |
71 | LPCUTF8 szName, // [IN] FieldDef name. |
72 | PCCOR_SIGNATURE pSig, // [IN] Signature. |
73 | ULONG cbSig, // [IN] Size of signature. |
74 | mdFieldDef * pfd, // [OUT] Put the FieldDef token here. |
75 | RID rid = 0); // [IN] Optional rid to be ignored. |
76 | |
77 | static HRESULT FindMember( |
78 | CMiniMdRW * pMiniMd, // [IN] the minimd to lookup |
79 | mdTypeDef td, // [IN] parent. |
80 | LPCUTF8 szName, // [IN] Member name. |
81 | PCCOR_SIGNATURE pSig, // [IN] Signature. |
82 | ULONG cbSig, // [IN] Size of signature. |
83 | mdToken * ptk); // [OUT] Put the token here. |
84 | |
85 | static HRESULT FindMemberRef( |
86 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
87 | mdToken tkParent, // [IN] the parent token |
88 | LPCUTF8 szName, // [IN] memberref name |
89 | const COR_SIGNATURE *pSig, // [IN] Signature. |
90 | ULONG cbSig, // [IN] Size of signature. |
91 | mdMemberRef *pmr, // [OUT] Put the MemberRef token found |
92 | RID rid = 0, // [IN] Optional rid to be ignored. |
93 | HashSearchOption fCreateHash = DoNotCreateHash); // [IN] Should we create hash first? (Optimize for multiple calls vs. single isolated call) |
94 | |
95 | static HRESULT FindStandAloneSig( |
96 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
97 | const COR_SIGNATURE *pbSig, // [IN] Signature. |
98 | ULONG cbSig, // [IN] Size of signature. |
99 | mdSignature *psa); // [OUT] Put the StandAloneSig token found |
100 | |
101 | static HRESULT FindTypeSpec( |
102 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
103 | const COR_SIGNATURE *pbSig, // [IN] Signature. |
104 | ULONG cbSig, // [IN] Size of signature. |
105 | mdTypeSpec *ptypespec); // [OUT] Put the TypeSpec token found |
106 | |
107 | static HRESULT FindMethodImpl( |
108 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
109 | mdTypeDef tkClass, // [IN] The parent TypeDef token. |
110 | mdToken tkBody, // [IN] Method body token. |
111 | mdToken tkDecl, // [IN] Method declaration token. |
112 | RID *pRid); // [OUT] Put the MethodImpl rid here |
113 | |
114 | static HRESULT FindCustomAttributeCtorByName( |
115 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
116 | LPCUTF8 szAssemblyName, // [IN] Assembly Name. |
117 | LPCUTF8 szNamespace, // [IN] TypeRef Namespace. |
118 | LPCUTF8 szName, // [IN] TypeRef Name. |
119 | mdTypeDef *ptk, // [OUT] Put the TypeRef token here. |
120 | RID rid = 0); // [IN] Optional rid to be ignored. |
121 | |
122 | static HRESULT FindTypeRefByName( |
123 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
124 | mdToken tkResolutionScope, // [IN] ResolutionScope, mdAssemblyRef or mdModuleRef. |
125 | LPCUTF8 szNamespace, // [IN] TypeRef Namespace. |
126 | LPCUTF8 szName, // [IN] TypeRef Name. |
127 | mdTypeDef *ptk, // [OUT] Put the TypeRef token here. |
128 | RID rid = 0); // [IN] Optional rid to be ignored. |
129 | |
130 | static HRESULT FindModuleRef( |
131 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
132 | LPCUTF8 szUTF8Name, // [IN] ModuleRef name. |
133 | mdModuleRef *pmur, // [OUT] Put the ModuleRef token here. |
134 | RID rid = 0); // [IN] Optional rid to be ignored. |
135 | |
136 | static HRESULT FindTypeDefByName( |
137 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
138 | LPCUTF8 szNamespace, // [IN] Namespace of the TypeDef. |
139 | LPCUTF8 szName, // [IN] Name of the TypeDef. |
140 | mdToken tkEnclosingClass, // [IN] TypeDef/TypeRef enclosing class. |
141 | mdTypeDef *ptk, // [OUT] Put the TypeDef token here. |
142 | RID rid = 0); // [IN] Optional rid to be ignored. |
143 | |
144 | static HRESULT FindInterfaceImpl( |
145 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
146 | mdToken tkClass, // [IN] TypeDef of the type |
147 | mdToken tkInterface, // [IN] could be typedef/typeref |
148 | mdInterfaceImpl *ptk, // [OUT] Put the interface token here. |
149 | RID rid = 0); // [IN] Optional rid to be ignored. |
150 | |
151 | static HRESULT FindPermission( |
152 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
153 | mdToken tkParent, // [IN] Token with the Permission |
154 | USHORT usAction, // [IN] The action of the permission |
155 | mdPermission *ppm); // [OUT] Put permission token here |
156 | |
157 | static HRESULT FindProperty( |
158 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
159 | mdToken tkTypeDef, // [IN] typedef token |
160 | LPCUTF8 szName, // [IN] name of the property |
161 | const COR_SIGNATURE *pbSig, // [IN] Signature. |
162 | ULONG cbSig, // [IN] Size of signature. |
163 | mdProperty *ppr); // [OUT] Property token |
164 | |
165 | static HRESULT FindEvent( |
166 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
167 | mdToken tkTypeDef, // [IN] typedef token |
168 | LPCUTF8 szName, // [IN] name of the event |
169 | mdProperty *pev); // [OUT] Event token |
170 | |
171 | static HRESULT FindCustomAttributeByToken( |
172 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
173 | mdToken tkParent, // [IN] the parent that custom value is associated with |
174 | mdToken tkType, // [IN] type of the CustomAttribute |
175 | const void *pCustBlob, // [IN] custom value blob |
176 | ULONG cbCustBlob, // [IN] size of the blob. |
177 | mdCustomAttribute *pcv); // [OUT] CustomAttribute token |
178 | |
179 | static HRESULT GetCustomAttributeByName(// S_OK or error. |
180 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
181 | mdToken tkObj, // [IN] Object with Custom Attribute. |
182 | LPCUTF8 szName, // [IN] Name of desired Custom Attribute. |
183 | const void **ppData, // [OUT] Put pointer to data here. |
184 | ULONG *pcbData); // [OUT] Put size of data here. |
185 | |
186 | static HRESULT GetCustomAttributeByName(// S_OK or error. |
187 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
188 | mdToken tkObj, // [IN] Object with Custom Attribute. |
189 | LPCUTF8 szName, // [IN] Name of desired Custom Attribute. |
190 | mdCustomAttribute pca); // [OUT] found CA token |
191 | |
192 | static HRESULT MergeUpdateTokenInFieldSig( |
193 | CMiniMdRW *pMiniMdAssemEmit, // [IN] The assembly emit scope. |
194 | CMiniMdRW *pMiniMdEmit, // [IN] The emit scope. |
195 | IMetaModelCommon *pCommonAssemImport, // [IN] Assembly scope where the signature is from. |
196 | const void *pbHashValue, // [IN] Hash value for the import assembly. |
197 | ULONG cbHashValue, // [IN] Size in bytes for the hash value. |
198 | IMetaModelCommon *pCommonImport, // [IN] The scope to merge into the emit scope. |
199 | PCCOR_SIGNATURE pbSigImp, // [IN] signature from the imported scope |
200 | MDTOKENMAP *ptkMap, // [IN] Internal OID mapping structure. |
201 | CQuickBytes *pqkSigEmit, // [OUT] buffer for translated signature |
202 | ULONG cbStartEmit, // [IN] start point of buffer to write to |
203 | ULONG *pcbImp, // [OUT] total number of bytes consumed from pbSigImp |
204 | ULONG *pcbEmit); // [OUT] total number of bytes write to pqkSigEmit |
205 | |
206 | static HRESULT MergeUpdateTokenInSig( // S_OK or error. |
207 | CMiniMdRW *pMiniMdAssemEmit, // [IN] The assembly emit scope. |
208 | CMiniMdRW *pMiniMdEmit, // [IN] The emit scope. |
209 | IMetaModelCommon *pCommonAssemImport, // [IN] Assembly scope where the signature is from. |
210 | const void *pbHashValue, // [IN] Hash value for the import assembly. |
211 | ULONG cbHashValue, // [IN] Size in bytes for the hash value. |
212 | IMetaModelCommon *pCommonImport, // [IN] The scope to merge into the emit scope. |
213 | PCCOR_SIGNATURE pbSigImp, // [IN] signature from the imported scope |
214 | MDTOKENMAP *ptkMap, // [IN] Internal OID mapping structure. |
215 | CQuickBytes *pqkSigEmit, // [OUT] translated signature |
216 | ULONG cbStartEmit, // [IN] start point of buffer to write to |
217 | ULONG *pcbImp, // [OUT] total number of bytes consumed from pbSigImp |
218 | ULONG *pcbEmit); // [OUT] total number of bytes write to pqkSigEmit |
219 | |
220 | // This is implemented in a satellite lib because it is only used in emit and depends on |
221 | // strong name support in mscorwks.dll. |
222 | static HRESULT FindAssemblyRef( |
223 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup. |
224 | LPCUTF8 szName, // [IN] Name. |
225 | LPCUTF8 szLocale, // [IN] Locale. |
226 | const void *pbPublicKeyOrToken, // [IN] Public key or token (based on flags). |
227 | ULONG cbPublicKeyOrToken, // [IN] Byte count of public key or token. |
228 | USHORT usMajorVersion, // [IN] Major version. |
229 | USHORT usMinorVersion, // [IN] Minor version. |
230 | USHORT usBuildNumber, // [IN] Build number. |
231 | USHORT usRevisionNumber, // [IN] Revision number. |
232 | DWORD dwFlags, // [IN] Flags. |
233 | mdAssemblyRef *pmar); // [OUT] returned AssemblyRef token. |
234 | |
235 | static HRESULT FindFile( |
236 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup. |
237 | LPCUTF8 szName, // [IN] name for the File. |
238 | mdFile *pmf, // [OUT] returned File token. |
239 | RID rid = 0); // [IN] Optional rid to be ignored. |
240 | |
241 | static HRESULT FindExportedType( |
242 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup. |
243 | LPCUTF8 szNamespace, // [IN] namespace for the ExportedType. |
244 | LPCUTF8 szName, // [IN] name for the ExportedType. |
245 | mdExportedType tkEnclosingType, // [IN] enclosing ExportedType token. |
246 | mdExportedType *pmct, // [OUT] returned ExportedType token. |
247 | RID rid = 0); // [IN] Optional rid to be ignored. |
248 | |
249 | static HRESULT FindManifestResource( |
250 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup. |
251 | LPCUTF8 szName, // [IN] name for the ManifestResource. |
252 | mdManifestResource *pmmr, // [OUT] returned ManifestResource token. |
253 | RID rid = 0); // [IN] Optional rid to be ignored. |
254 | |
255 | static HRESULT GetNesterHierarchy( |
256 | IMetaModelCommon *pCommon, // Scope in which to find the hierarchy. |
257 | mdTypeDef td, // TypeDef whose hierarchy is needed. |
258 | CQuickArray<mdTypeDef> &cqaTdNesters, // Array of Nesters. |
259 | CQuickArray<LPCUTF8> &cqaNamespaces, // Namespaces of the nesters. |
260 | CQuickArray<LPCUTF8> &cqaNames); // Names of the nesters. |
261 | |
262 | static HRESULT FindNestedTypeRef( |
263 | CMiniMdRW *pMiniMd, // [IN] Scope in which to find the TypeRef. |
264 | CQuickArray<LPCUTF8> &cqaNesterNamespaces, // [IN] Array of Namespaces. |
265 | CQuickArray<LPCUTF8> &cqaNesterNames, // [IN] Array of Names. |
266 | mdToken tkResolutionScope, // [IN] Resolution scope for the outermost TypeRef. |
267 | mdTypeRef *ptr); // [OUT] Inner most TypeRef token. |
268 | |
269 | static HRESULT FindNestedTypeDef( |
270 | CMiniMdRW *pMiniMd, // [IN] Scope in which to find the TypeRef. |
271 | CQuickArray<LPCUTF8> &cqaNesterNamespaces, // [IN] Array of Namespaces. |
272 | CQuickArray<LPCUTF8> &cqaNesterNames, // [IN] Array of Names. |
273 | mdTypeDef tdNester, // [IN] Enclosing class for the Outermost TypeDef. |
274 | mdTypeDef *ptd); // [OUT] Inner most TypeRef token. |
275 | |
276 | static HRESULT CreateNesterHierarchy( |
277 | CMiniMdRW *pMiniMdEmit, // [IN] Emit scope to create the Nesters in. |
278 | CQuickArray<LPCUTF8> &cqaNesterNamespaces, // [IN] Array of Nester namespaces. |
279 | CQuickArray<LPCUTF8> &cqaNesterNames, // [IN] Array of Nester names. |
280 | mdToken tkResolutionScope, // [IN] ResolutionScope for the innermost TypeRef. |
281 | mdTypeRef *ptr); // [OUT] Token for the innermost TypeRef. |
282 | |
283 | static HRESULT ImportTypeDef( |
284 | CMiniMdRW *pMiniMdAssemEmit, // [IN] Assembly emit scope. |
285 | CMiniMdRW *pMiniMdEmit, // [IN] Module emit scope. |
286 | IMetaModelCommon *pCommonAssemImport, // [IN] Assembly import scope. |
287 | const void *pbHashValue, // [IN] Hash value for import assembly. |
288 | ULONG cbHashValue, // [IN] Size in bytes of hash value. |
289 | IMetaModelCommon *pCommonImport, // [IN] Module import scope. |
290 | mdTypeDef tdImport, // [IN] Imported TypeDef. |
291 | bool bReturnTd, // [IN] If the import and emit scopes are identical, return the TypeDef. |
292 | mdToken *ptkType); // [OUT] Output token for the imported type in the emit scope. |
293 | |
294 | static HRESULT ImportTypeRef( |
295 | CMiniMdRW *pMiniMdAssemEmit, // [IN] Assembly emit scope. |
296 | CMiniMdRW *pMiniMdEmit, // [IN] Module emit scope. |
297 | IMetaModelCommon *pCommonAssemImport, // [IN] Assembly import scope. |
298 | const void *pbHashValue, // [IN] Hash value for import assembly. |
299 | ULONG cbHashValue, // [IN] Size in bytes of hash value. |
300 | IMetaModelCommon *pCommonImport, // [IN] Module import scope. |
301 | mdTypeRef trImport, // [IN] Imported TypeRef. |
302 | mdToken *ptkType); // [OUT] Output token for the imported type in the emit scope. |
303 | |
304 | private: |
305 | /* |
306 | static bool ImportHelper::CompareCustomAttribute( // |
307 | CMiniMdRW *pMiniMd, // [IN] the minimd to lookup |
308 | mdToken tkObj, // [IN] Object with Custom Attribute. |
309 | LPCUTF8 szName, // [IN] Name of desired Custom Attribute. |
310 | ULONG rid); // [IN] the rid of the custom attribute to compare to |
311 | */ |
312 | |
313 | static HRESULT GetTDNesterHierarchy( |
314 | IMetaModelCommon *pCommon, // Scope in which to find the hierarchy. |
315 | mdTypeDef td, // TypeDef whose hierarchy is needed. |
316 | CQuickArray<mdTypeDef> &cqaTdNesters,// Array of Nesters. |
317 | CQuickArray<LPCUTF8> &cqaNamespaces, // Namespaces of the nesters. |
318 | CQuickArray<LPCUTF8> &cqaNames); // Names of the nesters. |
319 | |
320 | static HRESULT GetTRNesterHierarchy( |
321 | IMetaModelCommon *pCommon, // Scope in which to find the hierarchy. |
322 | mdTypeRef tr, // TypeRef whose hierarchy is needed. |
323 | CQuickArray<mdTypeRef> &cqaTrNesters,// Array of Nesters. |
324 | CQuickArray<LPCUTF8> &cqaNamespaces, // Namespaces of the nesters. |
325 | CQuickArray<LPCUTF8> &cqaNames); // Names of the nesters. |
326 | |
327 | static HRESULT CreateModuleRefFromScope( |
328 | CMiniMdRW *pMiniMdEmit, // [IN] Emit scope in which the ModuleRef is to be created. |
329 | IMetaModelCommon *pCommonImport, // [IN] Import scope. |
330 | mdModuleRef *ptkModuleRef); // [OUT] Output token for ModuleRef. |
331 | |
332 | static HRESULT CreateModuleRefFromModuleRef( // S_OK or error. |
333 | CMiniMdRW *pMiniMdEmit, // [IN] Emit scope. |
334 | IMetaModelCommon *pCommon, // [IN] Import scope. |
335 | mdModuleRef tkModuleRef, // [IN] ModuleRef token. |
336 | mdModuleRef *ptkModuleRef); // [OUT] ModuleRef token in the emit scope. |
337 | |
338 | static HRESULT CreateModuleRefFromExportedType( // S_OK, S_FALSE or error. |
339 | CMiniMdRW *pAssemEmit, // [IN] Import assembly scope. |
340 | CMiniMdRW *pMiniMdEmit, // [IN] Emit scope. |
341 | mdExportedType tkExportedType, // [IN] ExportedType token in Assembly emit scope. |
342 | mdModuleRef *ptkModuleRef); // [OUT] ModuleRef token in the emit scope. |
343 | |
344 | // CreateAssemblyRefFromAssembly, CompareAssemblyRefToAssembly are in satellite libs because |
345 | // they are only used in emit cases and need strong-name support in mscorwks.dll. |
346 | |
347 | static HRESULT CreateAssemblyRefFromAssembly( // S_OK or error. |
348 | CMiniMdRW *pMiniMdAssemEmit, // [IN] Emit assembly scope. |
349 | CMiniMdRW *pMiniMdModuleEmit, // [IN] Emit module scope. |
350 | IMetaModelCommon *pCommonAssemImport, // [IN] Assembly import scope. |
351 | const void *pbHashValue, // [IN] Hash Blob for Assembly. |
352 | ULONG cbHashValue, // [IN] Count of bytes. |
353 | mdAssemblyRef *ptkAssemblyRef); // [OUT] AssemblyRef token. |
354 | |
355 | static HRESULT CompareAssemblyRefToAssembly( // S_OK, S_FALSE or error. |
356 | IMetaModelCommon *pCommonAssem1, // [IN] Assembly that defines the AssemblyRef. |
357 | mdAssemblyRef tkAssemRef, // [IN] AssemblyRef. |
358 | IMetaModelCommon *pCommonAssem2); // [IN] Assembly against which the Ref is compared. |
359 | |
360 | static HRESULT CreateAssemblyRefFromAssemblyRef( |
361 | CMiniMdRW *pMiniMdAssemEmit, // [IN] Assembly emit scope. |
362 | CMiniMdRW *pMiniMdModuleEmit, // [IN] Module emit scope |
363 | IMetaModelCommon *pCommonImport, // [IN] Scope to import the assembly ref from. |
364 | mdAssemblyRef tkAssemRef, // [IN] Assembly ref to be imported. |
365 | mdAssemblyRef *ptkAssemblyRef); // [OUT] AssemblyRef in the emit scope. |
366 | }; |
367 | |
368 | #endif // __IMPORTHELPER__h__ |
369 | |