2007-08-05 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / PInvokeInfo.cs
1 //
2 // PInvokeInfo.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace Mono.Cecil {
30
31         public sealed class PInvokeInfo : IReflectionVisitable {
32
33                 MethodDefinition m_meth;
34
35                 PInvokeAttributes m_attributes;
36                 string m_entryPoint;
37                 ModuleReference m_module;
38
39                 public MethodDefinition Method {
40                         get { return m_meth; }
41                 }
42
43                 public PInvokeAttributes Attributes {
44                         get { return m_attributes; }
45                         set { m_attributes = value; }
46                 }
47
48                 public string EntryPoint {
49                         get { return m_entryPoint; }
50                         set { m_entryPoint = value; }
51                 }
52
53                 public ModuleReference Module {
54                         get { return m_module; }
55                         set { m_module = value; }
56                 }
57
58                 #region PInvokeAttributes
59
60                 public bool IsNoMangle {
61                         get { return (m_attributes & PInvokeAttributes.NoMangle) != 0; }
62                         set {
63                                 if (value)
64                                         m_attributes |= PInvokeAttributes.NoMangle;
65                                 else
66                                         m_attributes &= ~PInvokeAttributes.NoMangle;
67                         }
68                 }
69
70                 public bool IsCharSetNotSpec {
71                         get { return (m_attributes & PInvokeAttributes.CharSetMask) == PInvokeAttributes.CharSetNotSpec; }
72                         set {
73                                 PInvokeAttributes masked = (PInvokeAttributes.CharSetMask & PInvokeAttributes.CharSetNotSpec);
74                                 if (value)
75                                         m_attributes |= masked;
76                                 else
77                                         m_attributes &= masked;
78                         }
79                 }
80
81                 public bool IsCharSetAnsi {
82                         get { return (m_attributes & PInvokeAttributes.CharSetMask) == PInvokeAttributes.CharSetAnsi; }
83                         set {
84                                 PInvokeAttributes masked = (PInvokeAttributes.CharSetMask & PInvokeAttributes.CharSetAnsi);
85                                 if (value)
86                                         m_attributes |= masked;
87                                 else
88                                         m_attributes &= masked;
89                         }
90                 }
91
92                 public bool IsCharSetUnicode {
93                         get { return (m_attributes & PInvokeAttributes.CharSetMask) == PInvokeAttributes.CharSetUnicode; }
94                         set {
95                                 PInvokeAttributes masked = (PInvokeAttributes.CharSetMask & PInvokeAttributes.CharSetUnicode);
96                                 if (value)
97                                         m_attributes |= masked;
98                                 else
99                                         m_attributes &= masked;
100                         }
101                 }
102
103                 public bool IsCharSetAuto {
104                         get { return (m_attributes & PInvokeAttributes.CharSetMask) == PInvokeAttributes.CharSetAuto; }
105                         set {
106                                 PInvokeAttributes masked = (PInvokeAttributes.CharSetMask & PInvokeAttributes.CharSetAuto);
107                                 if (value)
108                                         m_attributes |= masked;
109                                 else
110                                         m_attributes &= masked;
111                         }
112                 }
113
114                 public bool SupportsLastError {
115                         get { return (m_attributes & PInvokeAttributes.CharSetMask) == PInvokeAttributes.SupportsLastError; }
116                         set {
117                                 PInvokeAttributes masked = (PInvokeAttributes.CharSetMask & PInvokeAttributes.SupportsLastError);
118                                 if (value)
119                                         m_attributes |= masked;
120                                 else
121                                         m_attributes &= masked;
122                         }
123                 }
124
125                 public bool IsCallConvWinapi {
126                         get { return (m_attributes & PInvokeAttributes.CallConvMask) == PInvokeAttributes.CallConvWinapi; }
127                         set {
128                                 PInvokeAttributes masked = (PInvokeAttributes.CallConvMask & PInvokeAttributes.CallConvWinapi);
129                                 if (value)
130                                         m_attributes |= masked;
131                                 else
132                                         m_attributes &= masked;
133                         }
134                 }
135
136                 public bool IsCallConvCdecl {
137                         get { return (m_attributes & PInvokeAttributes.CallConvMask) == PInvokeAttributes.CallConvCdecl; }
138                         set {
139                                 PInvokeAttributes masked = (PInvokeAttributes.CallConvMask & PInvokeAttributes.CallConvCdecl);
140                                 if (value)
141                                         m_attributes |= masked;
142                                 else
143                                         m_attributes &= masked;
144                         }
145                 }
146
147                 public bool IsCallConvStdCall {
148                         get { return (m_attributes & PInvokeAttributes.CallConvMask) == PInvokeAttributes.CallConvStdCall; }
149                         set {
150                                 PInvokeAttributes masked = (PInvokeAttributes.CallConvMask & PInvokeAttributes.CallConvStdCall);
151                                 if (value)
152                                         m_attributes |= masked;
153                                 else
154                                         m_attributes &= masked;
155                         }
156                 }
157
158                 public bool IsCallConvThiscall {
159                         get { return (m_attributes & PInvokeAttributes.CallConvMask) == PInvokeAttributes.CallConvThiscall; }
160                         set {
161                                 PInvokeAttributes masked = (PInvokeAttributes.CallConvMask & PInvokeAttributes.CallConvThiscall);
162                                 if (value)
163                                         m_attributes |= masked;
164                                 else
165                                         m_attributes &= masked;
166                         }
167                 }
168
169                 public bool IsCallConvFastcall {
170                         get { return (m_attributes & PInvokeAttributes.CallConvMask) == PInvokeAttributes.CallConvFastcall; }
171                         set {
172                                 PInvokeAttributes masked = (PInvokeAttributes.CallConvMask & PInvokeAttributes.CallConvFastcall);
173                                 if (value)
174                                         m_attributes |= masked;
175                                 else
176                                         m_attributes &= masked;
177                         }
178                 }
179
180                 #endregion
181
182                 public PInvokeInfo (MethodDefinition meth)
183                 {
184                         m_meth = meth;
185                 }
186
187                 public PInvokeInfo (MethodDefinition meth, PInvokeAttributes attrs,
188                         string entryPoint, ModuleReference mod) : this (meth)
189                 {
190                         m_attributes = attrs;
191                         m_entryPoint = entryPoint;
192                         m_module = mod;
193                 }
194
195                 public void Accept (IReflectionVisitor visitor)
196                 {
197                         visitor.VisitPInvokeInfo (this);
198                 }
199         }
200 }