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
6//
7
8#include "common.h"
9#include "reflectclasswriter.h"
10
11// Forward declaration.
12STDAPI GetMetaDataInternalInterfaceFromPublic(
13 IUnknown *pv, // [IN] Given interface.
14 REFIID riid, // [IN] desired interface
15 void **ppv); // [OUT] returned interface
16
17//******************************************************
18//*
19//* constructor for RefClassWriter
20//*
21//******************************************************
22HRESULT RefClassWriter::Init(ICeeGen *pCeeGen, IUnknown *pUnk, LPCWSTR szName)
23{
24 CONTRACT(HRESULT) {
25 NOTHROW;
26 GC_NOTRIGGER;
27 // we know that the com implementation is ours so we use mode-any to simplify
28 // having to switch mode
29 MODE_ANY;
30 INJECT_FAULT(CONTRACT_RETURN(E_OUTOFMEMORY));
31
32 PRECONDITION(CheckPointer(pCeeGen));
33 PRECONDITION(CheckPointer(pUnk));
34
35 POSTCONDITION(SUCCEEDED(RETVAL) ? CheckPointer(m_emitter) : TRUE);
36 POSTCONDITION(SUCCEEDED(RETVAL) ? CheckPointer(m_importer) : TRUE);
37 POSTCONDITION(SUCCEEDED(RETVAL) ? CheckPointer(m_pEmitHelper) : TRUE);
38 POSTCONDITION(SUCCEEDED(RETVAL) ? CheckPointer(m_internalimport) : TRUE);
39 }
40 CONTRACT_END;
41
42 // Initialize the Import and Emitter interfaces
43 m_emitter = NULL;
44 m_importer = NULL;
45 m_internalimport = NULL;
46 m_pCeeFileGen = NULL;
47 m_ceeFile = NULL;
48 m_ulResourceSize = 0;
49 m_tkFile = mdFileNil;
50
51 m_pCeeGen = pCeeGen;
52 pCeeGen->AddRef();
53
54 // Get the interfaces
55 HRESULT hr = pUnk->QueryInterface(IID_IMetaDataEmit2, (void**)&m_emitter);
56 if (FAILED(hr))
57 RETURN(hr);
58
59 hr = pUnk->QueryInterface(IID_IMetaDataImport, (void**)&m_importer);
60 if (FAILED(hr))
61 RETURN(hr);
62
63 hr = pUnk->QueryInterface(IID_IMetaDataEmitHelper, (void**)&m_pEmitHelper);
64 if (FAILED(hr))
65 RETURN(hr);
66
67 hr = GetMetaDataInternalInterfaceFromPublic(pUnk, IID_IMDInternalImport, (void**)&m_internalimport);
68 if (FAILED(hr))
69 RETURN(hr);
70
71 // <TODO> We will need to set this at some point.</TODO>
72 hr = m_emitter->SetModuleProps(szName);
73 if (FAILED(hr))
74 RETURN(hr);
75
76 RETURN(S_OK);
77}
78
79
80//******************************************************
81//*
82//* destructor for RefClassWriter
83//*
84//******************************************************
85RefClassWriter::~RefClassWriter()
86{
87 CONTRACTL {
88 NOTHROW;
89 GC_TRIGGERS;
90 // we know that the com implementation is ours so we use mode-any to simplify
91 // having to switch mode
92 MODE_ANY;
93 FORBID_FAULT;
94 }
95 CONTRACTL_END;
96
97 if (m_emitter) {
98 m_emitter->Release();
99 }
100
101 if (m_importer) {
102 m_importer->Release();
103 }
104
105 if (m_pEmitHelper) {
106 m_pEmitHelper->Release();
107 }
108
109 if (m_internalimport) {
110 m_internalimport->Release();
111 }
112
113 if (m_pCeeGen) {
114 m_pCeeGen->Release();
115 m_pCeeGen = NULL;
116 }
117
118 if (m_pOnDiskEmitter) {
119 m_pOnDiskEmitter->Release();
120 m_pOnDiskEmitter = NULL;
121 }
122}
123