(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.PEToolkit / metadata / MDToken.cs
blobba4bec4a95463f0443664ef32676f9b657062c05
1 /*
2 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
3 */
5 using System;
6 using System.Runtime.InteropServices;
8 namespace Mono.PEToolkit.Metadata {
10 [StructLayoutAttribute(LayoutKind.Sequential)]
11 public struct MDToken {
13 internal int token;
15 /// <summary>
16 /// Creates new token with specified token type and record id.
17 /// </summary>
18 /// <param name="type">Token type.</param>
19 /// <param name="rid">Record IDentifier.</param>
20 public MDToken(TokenType type, int rid)
22 token = (int)type | rid;
25 /// <summary>
26 /// Creates new Nil token of a given type.
27 /// </summary>
28 /// <param name="type"></param>
29 public MDToken(TokenType type) : this(type, 0)
33 /// <summary>
34 /// </summary>
35 /// <param name="tok"></param>
36 public MDToken(MDToken tok) : this(tok.Type, tok.RID)
41 /// <summary>
42 /// Gets or sets metadata token Record IDentifier (RID).
43 /// </summary>
44 public int RID {
45 get {
46 return token & (~(int)TokenType.__mask);
48 set {
49 token &= (int)TokenType.__mask;
50 token |= value;
54 /// <summary>
55 /// Gets or sets metadata token type.
56 /// </summary>
57 public TokenType Type {
58 get {
59 return (TokenType) token & (TokenType.__mask);
61 set {
62 token &= ~(int)TokenType.__mask;
63 token |= (int)value;
67 /// <summary>
68 /// Returns true if this token is a Nil token (it's RID is 0).
69 /// </summary>
70 public bool IsNilToken {
71 get {
72 return (RID == 0);
77 /// <summary>
78 /// Returns token value.
79 /// </summary>
80 /// <returns></returns>
81 public override int GetHashCode ()
83 return token;
88 // See Metadata Unmanaged API doc (10.8)
89 public int Compress(out int len)
91 int res = token;
92 len = 4;
93 int rid = this.RID;
95 // Make room for type bits.
96 rid <<= 2;
98 TokenType type = this.Type;
100 // Token type (table that this token indexes) is encoded
101 // in the least significant 2 bits:
102 // TypeDef = 0
103 // TypeRef = 1
104 // TypeSpec = 2
105 // BaseType = 3
106 switch (type) {
107 case TokenType.TypeDef:
108 break;
109 case TokenType.TypeRef:
110 rid |= 1;
111 break;
112 case TokenType.TypeSpec:
113 rid |= 2;
114 break;
115 case TokenType.BaseType:
116 rid |= 3;
117 break;
118 default:
119 // Invalid operation for this type of token.
120 return res;
123 len = MDUtils.CompressData(rid, out res);
125 return res;
129 unsafe public static int Size {
130 get {
131 return sizeof (int);
135 public static implicit operator MDToken (uint val) {
136 MDToken res = new MDToken();
137 res.token = (int) val;
138 return res;
141 public static implicit operator uint (MDToken tok) {
142 return (uint)tok.token;
145 public override string ToString()
147 if (this.token == 0) return "NULL";
148 return String.Format("{0}[{1}]",
149 ((int)Type >> (int)TokenType.__shift <= (int)TableId.MAX)
150 ? ((TableId)((int)Type >> (int)TokenType.__shift)).ToString()
151 : Type.ToString(), RID);
152 //String.Format ("type = {0}, RID = {1}", Type, RID);