Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / emit / localbuilder.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 using System;
8 using System.Reflection;
9 using System.Security.Permissions;
10 using System.Runtime.InteropServices;
11
12 namespace System.Reflection.Emit 
13 {
14     [ClassInterface(ClassInterfaceType.None)]
15     [ComDefaultInterface(typeof(_LocalBuilder))]
16     [System.Runtime.InteropServices.ComVisible(true)]
17     public sealed class LocalBuilder : LocalVariableInfo, _LocalBuilder
18     { 
19         #region Private Data Members
20         private int m_localIndex;
21         private Type m_localType;
22         private MethodInfo m_methodBuilder;
23         private bool m_isPinned;
24         #endregion
25
26         #region Constructor
27         private LocalBuilder() { }
28         internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder) 
29             : this(localIndex, localType, methodBuilder, false) { }
30         internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder, bool isPinned) 
31         {
32             m_isPinned = isPinned;
33             m_localIndex = localIndex;
34             m_localType = localType;
35             m_methodBuilder = methodBuilder;
36         }
37         #endregion
38
39         #region Internal Members
40         internal int GetLocalIndex() 
41         {
42             return m_localIndex;
43         }
44         internal MethodInfo GetMethodBuilder() 
45         {
46             return m_methodBuilder;
47         }
48         #endregion
49
50         #region LocalVariableInfo Override
51         public override bool IsPinned { get { return m_isPinned; } }
52         public override Type LocalType 
53         {
54             get 
55             { 
56                 return m_localType; 
57             }
58         }        
59         public override int LocalIndex { get { return m_localIndex; } }
60         #endregion
61
62         #region Public Members
63         public void SetLocalSymInfo(String name)
64         {
65             SetLocalSymInfo(name, 0, 0);
66         }            
67
68         public void SetLocalSymInfo(String name, int startOffset, int endOffset)
69         {
70             ModuleBuilder dynMod;
71             SignatureHelper sigHelp;
72             int sigLength;
73             byte[] signature;
74             byte[] mungedSig;
75             int index;
76
77             MethodBuilder methodBuilder = m_methodBuilder as MethodBuilder;
78             if (methodBuilder == null) 
79                 // it's a light code gen entity
80                 throw new NotSupportedException();
81             dynMod = (ModuleBuilder) methodBuilder.Module;
82             if (methodBuilder.IsTypeCreated())
83             {
84                 // cannot change method after its containing type has been created
85                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_TypeHasBeenCreated"));
86             }
87     
88             // set the name and range of offset for the local
89             if (dynMod.GetSymWriter() == null)
90             {
91                 // cannot set local name if not debug module
92                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotADebugModule"));
93             }
94     
95             sigHelp = SignatureHelper.GetFieldSigHelper(dynMod);
96             sigHelp.AddArgument(m_localType);
97             signature = sigHelp.InternalGetSignature(out sigLength);
98     
99             // The symbol store doesn't want the calling convention on the
100             // front of the signature, but InternalGetSignature returns
101             // the callinging convention. So we strip it off. This is a
102             // bit unfortunate, since it means that we need to allocate
103             // yet another array of bytes...  
104             mungedSig = new byte[sigLength - 1];
105             Array.Copy(signature, 1, mungedSig, 0, sigLength - 1);
106             
107             index = methodBuilder.GetILGenerator().m_ScopeTree.GetCurrentActiveScopeIndex();
108             if (index == -1)
109             {
110                 // top level scope information is kept with methodBuilder
111                 methodBuilder.m_localSymInfo.AddLocalSymInfo(
112                      name,
113                      mungedSig,
114                      m_localIndex,   
115                      startOffset,
116                      endOffset);
117             }
118             else
119             {
120                 methodBuilder.GetILGenerator().m_ScopeTree.AddLocalSymInfoToCurrentScope(
121                      name,
122                      mungedSig,
123                      m_localIndex,   
124                      startOffset,
125                      endOffset);
126             }
127         }
128         #endregion
129
130 #if !FEATURE_CORECLR
131         void _LocalBuilder.GetTypeInfoCount(out uint pcTInfo)
132         {
133             throw new NotImplementedException();
134         }
135
136         void _LocalBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
137         {
138             throw new NotImplementedException();
139         }
140
141         void _LocalBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
142         {
143             throw new NotImplementedException();
144         }
145
146         void _LocalBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
147         {
148             throw new NotImplementedException();
149         }
150 #endif
151     }
152 }
153